5

Weird title, I know, let me explain.

I am a developer most familiar with C# and Javascript. I am completely sunk into those semi-functional worlds to the point that most of my code is about mapping/reducing/filtering collections. In C# that means I use LINQ just about everywhere, in Javascript it's Underscore.js and jQuery.

I have currently been assigned to an ongoing Java project and am feeling rather stifled. I simply do not think in terms of "create an array, shuffle stuff from one to another". I can (and did) create my own versions of the main map/reduce functions using anonymous types implementing interfaces but why re-invent the wheel? The project I am currently on already has commons-collections-3.1.jar and looking through the classes contained it seems like it likely can do everything that I want and more.

For the life of me, I can't find how to actually use it. Looking through the dozens of classes therein is not very helpful and the only thing I can google up is the api doc which is equally as helpful.

How do you use it to Map/Select, Filter/Where, Reduce/Aggregate? Is there anywhere that gives an actual tutorial on this library?

skaffman
  • 398,947
  • 96
  • 818
  • 769
George Mauer
  • 117,483
  • 131
  • 382
  • 612

3 Answers3

4

(Comment as answer for formatting purposes.)

Not so much, other than the limited user guide.

That said, I'm not sure where specifically you're having problems--filtering and selecting is mostly wrapped up in the functors package, and utilized by the CollectionUtils class.

While you're not looking for a replacement, you might find things like Guava or Lambda4J a bit more similar to what you're used to (within Java's constraints), and they're a bit less verbose.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Ah CollectionUtils is what I was looking for. That should be enough to get me started but holy god these guys need to get their documentation into an actually helpful state. Even that link to the direct class I want contains no examples of usage, I just got lucky that I recognized some of the method signatures as resembling ones that I had implemented myself. – George Mauer Dec 28 '11 at 14:41
  • @GeorgeMauer I think it's one of those things where it's been around so long it's assumed everyone has just learned it through osmosis ;) – Dave Newton Dec 28 '11 at 14:49
  • I get the feel all of Java is like that :) – George Mauer Dec 28 '11 at 14:54
  • @GeorgeMauer It can be at times--without a "governing body", so to speak, information can be a bit scattered at times. It's also pretty old, which is why I pointed at Guava/Lambda4J; it's just kind of accreted material over the years w/o corresponding documentation. Maybe I'll write something up though. – Dave Newton Dec 28 '11 at 15:02
  • Yeah, now time to run up the chain of command and try to convince them to include one of those libraries. Wheee. – George Mauer Dec 28 '11 at 15:08
  • 3
    @GeorgeMauer Point out that Guava supports generics, and looks more like modern Java than the aged collections library--shouldn't be a hard sell. Show a couple of side-by-sides. Otherwise... cast away. – Dave Newton Dec 28 '11 at 15:10
  • +1 for advocating Guava which is *definitely* the way to go these days. [Good pointers to Guava learning resources here.](http://stackoverflow.com/a/132639/56285) Also, for people learning Guava who are familiar with Commons Collections, [this page is useful](http://code.google.com/p/guava-libraries/wiki/ApacheCommonCollectionsEquivalents). Finally, check this out: [Expressive code: combining Guava, LambdaJ and Hamcrest](http://blog.cuttleworks.com/2011/10/expressive-code-guava-lambdaj-hamcrest/) @George, you should seriously dump Commons Collections now and move on to Guava. – Jonik Dec 28 '11 at 19:27
  • @GeorgeMauer: I guess one difference between C# & Java worlds is that there are way more 3rd party OSS libs and tools that enjoy widespread usage—or even a de facto status—on the Java side. (In some cases there are too many options and it's hard to choose; e.g. which web framework to use.) In any case, adding a new lib to your project should *not* be a big issue that requires battling bureaucracy. (If it is, I seriously suggest finding a workplace where (smart) tech people get to decide on tech issues.) – Jonik Dec 28 '11 at 20:06
  • @Jonik if this was a constant thing I would agree however being brought on to bolster manpower on an existing Java project (when 90% of our projects are .Net) for a few months calls more for the just bite-your-tongue-and-try-to-learn-something approach. – George Mauer Dec 28 '11 at 21:08
  • @GeorgeMauer That kind if project is a pain when you don't have enough ppl with the long-term Java experience :/ Good luck! – Dave Newton Dec 28 '11 at 21:27
1

Try these links :

http://commons.apache.org/collections/userguide.html (basic tutorial) http://larvalabs.com/collections/tutorial.html (advanced tutorial with generic)

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
  • These links contain only standard collection stuff but nothing about the closure/filter/transformer stuff in commons-collections. – A.H. Dec 28 '11 at 14:34
1

@george-mauer, you might have to rely on articles like this or a book like Jakarta Commons Cookbook. I have also found it rather useful to learn by creating samples of my own.

smooth reggae
  • 2,189
  • 13
  • 14
  • Hmm, that link simply talks about anonymous interface implementors and I'm certainly not going to get/read a book for a several month project. what a PITA – George Mauer Dec 28 '11 at 14:47