从Java 6开始,JDK中没有函数的概念。
番石榴具有功能接口,但是该
方法提供了您所需的功能。
Collections2.transform(Collection<E>,Function<E,E2>)
例:
// example, converts a collection of integers to their// hexadecimal string representationsfinal Collection<Integer> input = Arrays.asList(10, 20, 30, 40, 50);final Collection<String> output = Collections2.transform(input, new Function<Integer, String>(){ @Override public String apply(final Integer input){ return Integer.toHexString(input.intValue()); } });System.out.println(output);输出:
[a, 14, 1e, 28, 32]
如今,在Java 8中,实际上已经有一个map函数,因此我可能会以一种更简洁的方式编写代码:
Collection<String> hex = input.stream() .map(Integer::toHexString) .collect(Collectors::toList);



