guava(或另一个Java库)是否有类似Python中的reduce()函数?
我正在寻找类似于这个http://docs.python.org/library/functions.html#reduce的东西。
guava(或另一个Java库)是否有类似Python中的reduce()函数?
我正在寻找类似于这个http://docs.python.org/library/functions.html#reduce的东西。
@CurtainDog贡献了一个lambdaj的链接。那个链接可以做到OP想要的事情(虽然没有特别被称为reduce
的方法)。但是它说明了我所说的样板代码。请注意,许多高阶操作涉及创建扩展一个或另一个Closure
类的类。
(顺便说一下,我认为Lambda.aggregate(...)
方法是reduce
的lambdaj模拟。)
Java 8流允许您实现此操作。
mylist.stream().map((x) -> x + 1).reduce((a,b) -> a + b)
更多信息: http://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
意思是想获取更多相关信息,请访问上述链接,该链接提供了有关Java 8中Stream包的概述和详细信息。我最近提交了一个问题,在这个问题中我请求/讨论了类似的事情。这就是我实现所需要的。
/**
* Aggregate the selected values from the supplied {@link Iterable} using
* the provided selector and aggregator functions.
*
* @param <I>
* the element type over which to iterate
* @param <S>
* type of the values to be aggregated
* @param <A>
* type of the aggregated value
* @param data
* elements for aggregation
* @param selectorFunction
* a selector function that extracts the values to be aggregated
* from the elements
* @param aggregatorFunction
* function that performs the aggregation on the selected values
* @return the aggregated value
*/
public static <I, S, A> A aggregate(final Iterable<I> data,
final Function<I, S> selectorFunction,
final Function<Iterable<S>, A> aggregatorFunction){
checkNotNull(aggregatorFunction);
return aggregatorFunction.apply(
Iterables.transform(data, selectorFunction)
);
}
/** A Function that returns the average length of the Strings in an Iterable. */
public static Function<Iterable<String>,Integer> averageLength()
/** A Function that returns a BigDecimal that corresponds to the average
of all numeric values passed from the iterable. */
public static Function<Iterable<? extends Number>,BigDecimal> averageOfFloats()
/** A Function that returns a BigInteger that corresponds to the average
of all numeric values passed from the iterable. */
public static Function<Iterable<? extends Number>,BigInteger> averageOfIntegers()
/** A Function that returns the length of the longest String in an Iterable. */
public static Function<Iterable<String>,Integer> maxLength()
/** A Function that returns the length of the shortest String in an Iterable. */
public static Function<Iterable<String>,Integer> minLength()
/** A Function that returns a BigDecimal that corresponds to the sum of all
numeric values passed from the iterable. */
public static Function<Iterable<? extends Number>,BigDecimal> sumOfFloats()
/** A Function that returns a BigInteger that corresponds to the integer sum
of all numeric values passed from the iterable. */
public static Function<Iterable<? extends Number>,BigInteger> sumOfIntegers()
您可以在问题中看到我的示例实现。
这样,您就可以做出像这样的事情:
int[] numbers = { 1, 5, 6, 9, 11111, 54764576, 425623 };
int sum = Aggregators.sumOfIntegers().apply(Ints.asList(numbers)).intValue();
我知道这不是你要求的内容,但在很多情况下,这会让生活更轻松,并且与你的请求有重叠之处(即使方法不同)。
int sum = dogs.filter(new Predicate<Arguments2<Dog, Integer>>() {
@Override
public Boolean invoke(Arguments2<Dog, Integer> arguments) {
// filter on male
return arguments.getArgument1().getGender() == Dog.Gender.MALE;
}
}).<Integer>map(new Function<Integer, Arguments2<Dog, Integer>>() {
@Override
public Integer invoke(Arguments2<Dog, Integer> arguments) {
// get ages
return arguments.getArgument1().getAge();
}
}).reduce(new Function<Integer, Arguments2<Integer, Integer>>() {
@Override
public Integer invoke(Arguments2<Integer, Integer> arguments) {
// sum âges
return arguments.getArgument1() + arguments.getArgument2();
}
});
System.out.println("Le cumul de l'âge des mâles est de : " + sum + " ans");
Collections.reduce
。顺便说一下,Python 3鼓励使用显式的for循环代替reduce;reduce已从内置函数移动到functools模块中。 - ideCollection.reduce
方法,因为会有扩展方法。我知道他们计划添加像filter
和map
这样的扩展方法。 - ColinD