On Github csabapalfi / guava-slides
String joined = Joiner.on(":").join("A", "B", "C"); assertEquals("A:B:C", joined);
",a,,b,".split(",") returns...?
String toSplit = ": A::: B : C :::"; Iterable<String> parts = Splitter.on(":") .omitEmptyStrings().trimResults().split(toSplit); String backTogether = Joiner.on(":").join(parts); assertEquals("A:B:C", backTogether);
before
Set<Integer> someSet = new HashSet<Integer>();
after
Set<Integer> someSet = new Sets.newHashSet();
also
.newHashSet(E...elements); .newHashSet(Iterable<? extends E> elements); .newHashSet(Iterator<? extends E> elements);
before
public static final Set<Integer> LUCKY_NUMBERS = Collections.unmodifiableSet( new LinkedHashSet<Integer>( Arrays.asList(4, 8, 15, 16, 23, 42)));
after
public static final ImmutableSet<Integer> LUCKY_NUMBERS = ImmutableSet.of(4, 8, 15, 16, 23, 42);
before
public static final Map<String, Integer> ENGLISH_TO_INT; static { Map<String, Integer> map = new LinkedHashMap<String, Integer>(); map.put("four", 4); map.put("forty-two", 42); ENGLISH_TO_INT = Collections.unmodifiableMap(map); }
after
public static final ImmutableMap<String, Integer> ENGLISH_TO_INT = ImmutableMap.builder() .put("four", 4) .put("forty-two", 42) .build();
does it need to be mutable?
ImmutableSet.copyOf(someIterable); ImmutableMap.copyOf(someMap);
before
Map<Salesperson, List<Sale>> map = new HashMap<Salesperson, List<Sale>>(); public void makeSale(Salesperson salesPerson, Sale sale) { List<Sale> sales = map.get(salesPerson); if (sales == null) { sales = new ArrayList<Sale>(); map.put(salesPerson, sales); } sales.add(sale); }
after
Multimap<Salesperson, Sale> multimap = ArrayListMultimap.create(); public void makeSale(Salesperson salesPerson, Sale sale) { multimap.put(salesPerson, sale); }
Sets
Set intersection(Set, Set) Set<E> difference(Set<E>, Set<E>) Set<E> union(Set<E>, Set<E>)
Iterables
Iterable cycle(Iterable) T getOnlyElement(Iterable<T>) Iterable<T> reverse(List<T>)
Function<F, T> { T apply(F input); } Predicate<T>{ boolean apply(T input); } Iterable filter(Iterable, Predicate) Iterable transform(Iterable, Function)
before
List<Integer> lengths = Lists.newLinkedList(); for (String string : strings) { if (string.startsWith("S")) { lengths.add(string.length()); } }
after
Function<String, Integer> lengthFunction = new Function<String, Integer>() { public Integer apply(String string) { return string.length(); } }; Predicate<String> startsWithS = new Predicate<String>() { public boolean apply(String string) { return string.startsWith("S") } }; List<Integer> lengths = Lists.newLinkedList( Iterables.transform( Iterables.filter(strings, startsWithS), lengthFunction));
replacing a nullable T reference with a non-null value
Optional<Integer> possible = Optional.of(5); possible.isPresent(); // returns true possible.get(); // returns 5
Ivy:
<dependency name="guava" org="com.google.guava" rev="13.0.1" />
Maven:
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>13.0.1</version> </dependency>