PUBLIC OBJECT

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");`</pre>
[Iterables.getOnlyElement](/2007/09/coding-in-small-with-google-collections_9985.html)<pre class="prettyprint">`assertEquals(jesse, Iterables.getOnlyElement(usersOnDuty));`</pre>
[Comparators.max](/2007/09/coding-in-small-with-google-collections_8582.html)<pre class="prettyprint">`return Comparators.max(perKm, minimumDeliveryCharge);`</pre>
[Objects.equal and hashCode](/2007/09/coding-in-small-with-google-collections_8175.html)<pre class="prettyprint">`return Objects.hashCode(address, targetArrivalDate, lineItems);`</pre>
[Lists.immutableList](/2007/09/coding-in-small-with-google-collections_8786.html)<pre class="prettyprint">`this.steps = Lists.immutableList(steps);`</pre>
[Objects.nonNull](/2007/09/coding-in-small-with-google-collections_15.html)<pre class="prettyprint">`his.familyName = Objects.nonNull(familyName);`</pre>
[Iterables.concat](/2007/09/coding-in-small-with-google-collections_18.html)<pre class="prettyprint">`for (LineItem lineItem : Iterables.concat(getPurchasedItems(), getFreeItems())) { 
  ...
}`</pre>
[Constraints.constrainedList](/2007/09/coding-in-small-with-google-collections_8093.html)<pre class="prettyprint">`public void checkElement(LineItem element) { ... }`</pre>
[Multimap](/2007/09/coding-in-small-with-google-collections_19.html)<pre class="prettyprint">`Multimap&lt;Salesperson, Sale&gt; multimap 
    = new ArrayListMultimap&lt;Salesperson,Sale&gt;();`</pre>
[Join](/2007/09/coding-in-small-with-google-collections_20.html)<pre class="prettyprint">`return Join.join(" and ", items);`</pre>
[Maps, Sets and Lists](/2007/09/coding-in-small-with-google-collections_22.html)<pre class="prettyprint">`Set&lt;String&gt; workdays = Sets.newLinkedHashSet(
    "Monday", "Tuesday", "Wednesday", "Thursday", "Friday");`</pre>
[Comparators.fromFunction](/2007/09/coding-in-small-with-google-collections_24.html)<pre class="prettyprint">`return Comparators.fromFunction(new Function&lt;Product,Money&gt;() { ... });`</pre>
[BiMap](/2007/09/coding-in-small-with-google-collections_26.html)<pre class="prettyprint">`return NUMBER_TO_NAME_BIMAP.inverse().get(elementName);`</pre>
[ClassToInstanceMap](/2007/09/coding-in-small-with-google-collections_28.html)<pre class="prettyprint">`T result = optionalValuesByType.getInstance(type);`</pre>
[ImmutableSet](http://publicobject.com/2008/03/coding-in-small-with-google-collections.html)<pre class="prettyprint">`public static final ImmutableSet&lt;Integer&gt; LUCKY_NUMBERS 
      = ImmutableSet.of(4, 8, 15, 16, 23, 42);`</pre>
[Sets.union, intersection and difference](http://publicobject.com/2008/08/coding-in-small-with-google-collections.html)<pre class="prettyprint">`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