Atom Feed SITE FEED   ADD TO GOOGLE READER

Series Recap: Coding in the small with Google Collections

Robert Konigsberg, Jerome Mourits and myself have written several snippets that highlight the carefully designed Google Collections Library:

Preconditions
Preconditions.checkNotNull(order.getAddress(), "order address");

Iterables.getOnlyElement
assertEquals(jesse, Iterables.getOnlyElement(usersOnDuty));

Comparators.max
return Comparators.max(perKm, minimumDeliveryCharge);

Objects.equal and hashCode
return Objects.hashCode(address, targetArrivalDate, lineItems);

Lists.immutableList
this.steps = Lists.immutableList(steps);

Objects.nonNull
his.familyName = Objects.nonNull(familyName);

Iterables.concat
for (LineItem lineItem : Iterables.concat(getPurchasedItems(), getFreeItems())) { 
...
}

Constraints.constrainedList
public void checkElement(LineItem element) { ... }

Multimap
Multimap<Salesperson, Sale> multimap 
= new ArrayListMultimap<Salesperson,Sale>();

Join
return Join.join(" and ", items);

Maps, Sets and Lists
Set<String> workdays = Sets.newLinkedHashSet(
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday");

Comparators.fromFunction
return Comparators.fromFunction(new Function<Product,Money>() { ... });

BiMap
return NUMBER_TO_NAME_BIMAP.inverse().get(elementName);

ClassToInstanceMap
T result = optionalValuesByType.getInstance(type);

ImmutableSet
public static final ImmutableSet<Integer> LUCKY_NUMBERS 
= ImmutableSet.of(4, 8, 15, 16, 23, 42);

Sets.union, intersection and difference
Sets.difference(requestParameters.keySet(), LEGAL_PARAMETERS)


Get it


Consider downloading the library to put to use in your project. It will make your code more expressive!
Project downloads

More Documentation


Javadoc API
Faq
Users list
Developer list
I hope this will make way into JDK 7.