
Best practice to validate null and empty collection in Java
Oct 4, 2012 · 23 Personally, I prefer to use empty collections instead of null and have the algorithms work in a way that for the algorithm it does not matter if the collection is empty or not.
Checking if a collection is empty in Java: which is the best method?
The other advantage of using Apache commons-collections CollectionUtils.isEmpty (or CollectionUtils.isNotEmpty) is that your if condition has less branches, so it is easier to reach a …
A Java collection of value pairs? (tuples?) - Stack Overflow
Eclipse Collections If you're using pairs that contain primitives, Eclipse Collections provides some very efficient primitive pair classes that will avoid all the inefficient auto-boxing and auto …
Java: Get first item from a collection - Stack Overflow
Nov 4, 2009 · If I have a collection, such as Collection<String> strs, how can I get the first item out? I could just call an Iterator, take its first next(), then throw the Iterator away. Is there …
java - Sort a Map<Key, Value> by values - Stack Overflow
With the advent of Java-8, we can use streams for data manipulation in a very easy/succinct way. You can use streams to sort the map entries by value and create a LinkedHashMap which …
How to sort List of objects by some property - Stack Overflow
Collections.sort(list, comparator); In general, it's a good idea to implement Comparable<T> if there's a single "natural" sort order... otherwise (if you happen to want to sort in a particular …
What is the difference between Collection and List in Java?
Jul 23, 2010 · 3 Collection is the main interface of Java Collections hierarchy and List (Sequence) is one of the sub interfaces that defines an ordered collection.
Java Immutable Collections - Stack Overflow
Unmodifiable collections are usually read-only views (wrappers) of other collections. You can't add to, remove from or clear them, but the underlying collection can change. Immutable …
How to filter a Java Collection (based on predicate)?
Assuming that you are using Java 1.5, and that you cannot add Google Collections, I would do something very similar to what the Google guys did. This is a slight variation on Jon's …
How do I join two lists in Java? - Stack Overflow
Oct 10, 2008 · Avoid Apache Commons Collections. It’s not typesafe, there are no generics. Great if you use Java 1.4, but for Java 5 and above, I’d prefer Google Guava.