Skip to content

Instantly share code, notes, and snippets.

@priort
Created December 30, 2018 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save priort/f80a0e90a5e3f65d22ee1e1941b32a33 to your computer and use it in GitHub Desktop.
Save priort/f80a0e90a5e3f65d22ee1e1941b32a33 to your computer and use it in GitHub Desktop.
public static List map(List list, Mapping mapping) {
List result = new ArrayList();
for (T elem : list) {
result.add(mapping.apply(elem));
}
return result;
}
private interface Mapping {
U apply(T element);
}
public static void main (String[] args) {
List inputList =
Arrays.asList("functional", "programming", "with", "closures");
Mapping stringToStringLength =
new Mapping() {
public Integer apply(String element) {
return element.length();
}
};
List mappedList = map(inputList, stringToStringLength);
System.out.println("Initial List: " + inputList);
System.out.println("Mapped List: " + mappedList);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment