Posts Tagged: collections


Java, Map and Optional

Posted by & filed under , .

I do like the Optional class in Java — it is a long awaited elegant replacement for returning null whenever something isn’t there then relying on if( x != null ) checks or using ?: in the format (x == null) ? “foo” : “bar”. It works great also with the new Java stream classes […]

Collection Sorting — Java vs Groovy

Posted by & filed under , .

With the introduction of lambdas in Java (not so) recently, some argue that Groovy lost some of its thunder, as closures are now first class citizens in the JDK. However, as I’m about to show, while lambda’s pushed the Java language a great deal forward, Groovy still makes a lot of things incredibly easy (and […]

Parallel : Groovy and Java Streams

Posted by & filed under .

This is something that every now and then I have to do: check whether either one or all elements of a collection meet a certain criteria. The standard code initially in Java involved a for loop and iterating through the collection explicitly and checking the condition at each step. Then Apache Commons came on with their […]

Java Map and Subtleties of getOrDefault vs computeIfAbsent

Posted by & filed under , .

I wrote recently about the new niceties in the Map interface that Java 8 brought to light where I’m highlighting in particular 2 new methods: getOrDefault and computeIfAbsent (see my previous post here about it). These provide a cleaner (and as it turns out faster too!) way of retrieving values from a Map instance. However, […]

Convenience Factory Methods for Collections in Java 9

Posted by & filed under , .

Looking the other day through the JDK Enhancements Proposals (aka “JEP“) I came across JEP-269: “Convenience Factory Methods for Collections”. Oh hello, where have you been all these previous JDK releases? 🙂 It seems finally the Java world has woken up to what other JVM languages (and not only!) have been offering for a while: […]

Java 8 Accumulators and Adders

Posted by & filed under , , , .

If you’ve been using Java for a while now, then like me, you must have saluted the move quite a while back in JDK 1.5 to introduce the atomic classes (AtomicLong, AtomicBoolean and so on). They were a big step forward, away from the clunkyness of having to create ridiculous bottlenecks in the code for […]

Data Validation inside a List

Posted by & filed under , , .

Felt like sharing this, since there might be some other “dinosaurs” like myself (i.e. slow in their using the Apache Commons Collections) — and those guys might find this useful 🙂 Occasionally you find yourself providing components that will be used by various modules in a project, and as such you have to employ strict […]

Neat Trick for Returning an Empty List in Java

Posted by & filed under , , , .

I’ve found this post actually burried in my “Drafts” — it’s been there for sometime it seems. I recall writing about it on my mobile WordPress application, so I’m guessing I’ve saved it as draft on the blog with the view to apply the code pretty-printing and forgot about it quite likely 🙂 So I […]