我可以想到两种方法:
public static IntStream foo(List<Integer> list)
{
return list.stream().mapToInt(Integer::valueOf);
}
public static IntStream bar(List<Integer> list)
{
return list.stream().mapToInt(x -> x);
}
惯用的方式是什么?也许已经有一个库函数可以完全满足我的要求?
我想(或者至少是一种替代方案)这种方式性能更高:
public static IntStream baz(List<Integer> list)
{
return list.stream().mapToInt(Integer::intValue);
}
由于函数Integer::intValue http://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#intValue--完全兼容ToIntFunction http://docs.oracle.com/javase/8/docs/api/index.html?java/util/function/ToIntFunction.html因为它需要一个Integer
它返回一个int
. No 自动装箱 http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html被执行。
我也在寻找相当于Function::identity http://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html#identity--,我希望写出相当于你的bar
方法 :
public static IntStream qux(List<Integer> list)
{
return list.stream().mapToInt(IntFunction::identity);
}
但他们没有提供这个identity
方法。不知道为什么。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)