带有泛型的Java 8 Lambdas的使用 [英] Use of Java 8 Lambdas with Generics

查看:307
本文介绍了带有泛型的Java 8 Lambdas的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个客户端类,它利用MathUtility类提供的函数。无论Mathmatical操作如何,它只应在MathUtility类中发生。

  //在客户端
中MathUtility.sum(listOfInts,(Integer i) - > {return(i< 3);});

//在实用程序中
class MathUtility< T extends Number> {
public static< T extends Number> T sumWithCondition(List numbers,Predicate< T> condition){
return numbers.parallelStream()
.filter(condition)
.map(i - > i)
.reduce(0,T :: sum); //编译时错误
}
public static< T extends Number> T avgWithCondition(List< T>数字,Predicate< T>条件){
//另一个函数
}
//很多函数在这里
}

现在失败,出现此错误 - 方法reduce(T,BinaryOperator< T>)in类型Stream< T>不适用于参数(int,T :: sum)



注意:我不想为不同的数字类型编写求和函数

解决方案


有没有办法在不写每个可能类型的<






$ b $正如Aaron Davis在上述评论中指出的那样,您可以将缩减参数传递给方法本身。

  public static< T> T sumWithCondition(List  numbers,Predicate T condition,T identity,BinaryOperator T accumulator){
return numbers.parallelStream()。filter(condition).reduce(identity,accumulator);
}

一个例子是:

 列表<整数> list = Arrays.asList(1,2,3,4,5); 

System.out.println(sumWithCondition(list,i - > i> 1,0,(a,b) - > a + b));

>> 14

列表< BigInteger> list2 = Arrays.asList(BigInteger.ONE,BigInteger.ONE);

System.out.println(sumWithCondition(list2,i - > true,BigInteger.ZERO,(a,b) - > a.add(b)));

>> 2


Is it possible to do this using Predicate interface.

I have a client class that utilizes functions provided by a MathUtility class. Whatever the Mathmatical operation it should happen only within the MathUtility class.

    //in client 
    MathUtility.sum(listOfInts, (Integer i)->{return (i<3);});

   //in utility
    class MathUtility<T extends Number> {
        public static <T extends Number> T sumWithCondition(List<T> numbers, Predicate<T> condition) {
            return numbers.parallelStream()
                    .filter(condition)
                    .map(i -> i)
                    .reduce(0, T::sum); //compile time error
        }
        public static <T extends Number> T avgWithCondition(List<T> numbers, Predicate<T> condition) {
            //another function
        }
        //lot many functions go here
    }

Right now it fails with this error - The method reduce(T, BinaryOperator<T>) in the type Stream<T> is not applicable for the arguments (int, T::sum)

Note: I do not want to write sum functions for different Number types

解决方案

Is there a way to do it without writing a sum function for every possible type of T that i'm expecting?

As Aaron Davis stated in a comment above, you can pass the reduction parameters to the method itself.

public static <T> T sumWithCondition(List<T> numbers, Predicate<T> condition, T identity, BinaryOperator<T> accumulator) {
    return numbers.parallelStream().filter(condition).reduce(identity, accumulator);
}

An example would be:

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

System.out.println(sumWithCondition(list, i -> i > 1, 0, (a, b) -> a + b));

>> 14

List<BigInteger> list2 = Arrays.asList(BigInteger.ONE, BigInteger.ONE);

System.out.println(sumWithCondition(list2, i -> true, BigInteger.ZERO, (a, b) -> a.add(b)));

>> 2

这篇关于带有泛型的Java 8 Lambdas的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆