Google Guava
Create a transformed view of a collection
Object - {equals, coalesce}
Equals (null safe)
Coalesce
Objects.firstNonNull(obj, default)
Object - ComparisonChain
CompareTo method:
Immutable - why
Why?
· + Your life is easy (no trust problems)
· + Free thread safety
· + Can be used as a constant
· + Uses less memory
· - Rejects null values
Compared to Collections.unmodifiable?
· - overhead
· - unsafe
· - verbose
Immutable - how
Create a set of elements
ImmutableSet.of("a", "b", "c", "a", "d", "b")
Using a builder
public static final ImmutableSet GOOGLE_COLORS =
ImmutableSet.builder()
.addAll(WEBSAFE_COLORS)
.add(new Color(0, 191, 255))
.build();
Creating a defencive copy
ImmutableList.copyOf(collection);
Immutable - collections
JDK Guava
ImmutableCollection ImmutableMultiset
ImmutableSortedMultiset
ImmutableList