Skip to content

Instantly share code, notes, and snippets.

@vladimirdlc
Created November 7, 2012 18:36
Show Gist options
  • Save vladimirdlc/4033473 to your computer and use it in GitHub Desktop.
Save vladimirdlc/4033473 to your computer and use it in GitHub Desktop.
Transform a List from a type into another type List using Guava. Practical example.
final List<State> states = new ArrayList<State>();
final int n = 10;
for (int i = 0; i < n; i++) {
final State state = new State(i, DeliveryReceiptState.ACCEPTD);
states.add(state);
}
final List<Status<DeliveryReceiptState>> statuses = Lists.transform(
states, new Function<State, Status<DeliveryReceiptState>>() {
@Override
public Status<DeliveryReceiptState> apply(final State input) {
final Status<DeliveryReceiptState> status = new Status<DeliveryReceiptState>(
input.getId(), input.getDeliveryReceiptState());
return status;
}
});
for (final Status<DeliveryReceiptState> status : statuses) {
System.out.println(status);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment