Skip to content

Instantly share code, notes, and snippets.

@skamille
Created June 1, 2012 01:51
Show Gist options
  • Save skamille/2848015 to your computer and use it in GitHub Desktop.
Save skamille/2848015 to your computer and use it in GitHub Desktop.
Lists.transform
transform
public static <F,T> List<T> transform(List<F> fromList,
Function<? super F,? extends T> function)
Returns a list that applies function to each element of fromList. The returned list is a transformed view of fromList; changes to fromList will be reflected in the returned list and vice versa.
Since functions are not reversible, the transform is one-way and new items cannot be stored in the returned list. The add, addAll and set methods are unsupported in the returned list.
The function is applied lazily, invoked when needed. This is necessary for the returned list to be a view, but it means that the function will be applied many times for bulk operations like List.contains(java.lang.Object) and List.hashCode(). For this to perform well, function should be fast. To avoid lazy evaluation when the returned list doesn't need to be a view, copy the returned list into a new list of your choosing.
If fromList implements RandomAccess, so will the returned list. The returned list always implements Serializable, but serialization will succeed only when fromList and function are serializable. The returned list is threadsafe if the supplied list and function are.
If only a Collection or Iterable input is available, use Collections2.transform(java.util.Collection, com.google.common.base.Function) or Iterables.transform(java.lang.Iterable, com.google.common.base.Function).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment