5

I've inherited a team and a (Java) code base.

The code base makes use of a lot of explicit for each loops. I'd like to replace those (edit: in future code) with something like Commons Collections Transformers and Predicates and its collect and transform methods.

However, Commons Collection isn't generic. The rest of my code base, whatever else it lacks, is, and I don't want to introduce a lot of casting.

So I'm looking at the Commons-Collections with Generics (http://sourceforge.net/projects/collections/) and at Guava (formerly Google Collections) (http://code.google.com/p/guava-libraries/).

Question one: is either or both of those libraries Generally Recognized As Safe for production use? Do you use either in production?

Question two: of the two, which do you recommend? Answers from anyone who has used either are great, answers from anyone who has used both even better!

Finally, my team includes a mix of contractors who are mid- to senior-level, and employees who are mid- and junior-level in Java (edit: but with multi-year experience in non-OO programming) . So I don't want to introduce more than one collections library, and I do want one that won't be too hard for any of my team to use.

Thanks!

Edit: I want to replace explicit loops because the amount of boilerplate obscures and overwhelms the actual business code. I've got nested loops calling functions with nested loops, all to do things that are one-liners. With the proper introduction and training, I feel my team will find a Collections/Transformer/Predicate approach cleaner, clearer, faster to write, and easier to read.

tpdi
  • 34,554
  • 11
  • 80
  • 120
  • 1
    Commons Collections really needs to come up with a Java5 version if they do not want everyone to jump ship to Guava. I do not much mind not having generics and varargs in Commons Lang, but for a collections library, it is essential. – Thilo Oct 13 '10 at 03:07
  • 3
    Related: [Commons Collections vs Google Collections](http://stackoverflow.com/questions/1444437/apache-commons-vs-google-collections). Still actual IMO. Also look [this movie](http://www.youtube.com/watch?v=ZeO_J2OcHYM). – BalusC Oct 13 '10 at 03:08
  • 2
    Are you replacing code for the sake of replacing code or because something is lacking in the `for each` style you have today? – matt b Oct 13 '10 at 03:09
  • 4
    Why are you doing this? What value does it add to your code base? Sure, the code will looks nicer (to you), but you have to train your developers to understand the new library ... and you have the cost of doing the conversion, and deal with the resulting bugs to deal with. – Stephen C Oct 13 '10 at 03:11
  • @matt b and @Stephen C: Good point, especially when "junior-level" coders are involved who may not be too comfortable with predicates and transformations. – Thilo Oct 13 '10 at 03:11
  • @Thilo - a lot of senior level developers might not be comfortable either. Especially those who didn't do functional programming at Uni. – Stephen C Oct 13 '10 at 03:14
  • c'mon, functions and predicates can't be *that* hard to understand. I'd be worried about a developer who couldn't pick up a new library with such simple concepts like this. – matt b Oct 13 '10 at 03:21
  • @matt b: I agree. Junior devs should be able to pick up new concepts quickly, and I certainly hope that senior devs have done functional programming at some time. Also, my vote goes to Guava; AFAIK some of it's designers also worked on the Java Collections API, learned from their mistakes, and did it better. – ide Oct 13 '10 at 03:29
  • 2
    @matt b - so would I ... but developers like that definitely do exist. And my original point still stands - what is the "value proposition"? – Stephen C Oct 13 '10 at 03:34
  • 1
    @ide true that, Joshua Bloch has been a very frequent advisor on our designs. – Kevin Bourrillion Oct 19 '10 at 08:02
  • 1
    Is Guava "Generally Recognized As Safe for production use?" Well, you can check and see if gmail is up. :-) – Kevin Bourrillion Oct 19 '10 at 08:03

3 Answers3

4

I use Google collections in production and have never had a problem. Makes you wish java had function pointers, but he design is well thought out. Even he little things like Lists.newArrayList become the.standard way to new up a list.

Nathan Feger
  • 19,122
  • 11
  • 62
  • 71
  • "Makes you wish Java had function pointers". Java 8. Fingers crossed. – Thilo Oct 13 '10 at 03:13
  • 1
    @Thilo: maybe Java 10. Just maybe. – Matt Ball Oct 13 '10 at 03:32
  • JDK7 (now 8, I guess) "includes a new package, java.dyn, that contains the classes associated with dynamic language support in the Java platform. One of the classes in the package is MethodHandle. A method handle is a simple object of type java.dyn.MethodHandle that anonymously encapsulates a reference to a JVM method. A method handle is callable, just like a named reference to a method." http://www.oracle.com/technetwork/issue-archive/2010/10-may/o30java-099612.html – Thilo Oct 16 '10 at 05:42
  • @Thilo: I'm pretty sure `MethodHandle` and friends are going to be in JDK7. But I don't know that there's going to be a lot of use for them in Java itself until JDK8 when lambdas and method references are added. Initially, I think it'll mostly be used for dynamic languages on the JVM. – ColinD Oct 17 '10 at 01:23
2

It seems that you are not looking for a Collection library, but rather a library for working with existing collections in a functionnal way.

I wrote such a library myself, but it is not open source (it is only used internally in my company), and it may not be the best designed one.

A library with the same goal seems promising : lambdaj. I haven't used it myself, but I think I wouldn't have written my library if I was aware of the existence of lambdaj, at that time.

barjak
  • 10,842
  • 3
  • 33
  • 47
2

I suggest to give a look at lambdaj either. Here you can find an overview of the main features: http://code.google.com/p/lambdaj/wiki/LambdajFeatures

Mario Fusco
  • 13,548
  • 3
  • 28
  • 37