Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用流拆分集合中的奇数和偶数以及奇数和偶数之和_Java_Collections_Lambda_Java 8_Java Stream - Fatal编程技术网

Java 如何使用流拆分集合中的奇数和偶数以及奇数和偶数之和

Java 如何使用流拆分集合中的奇数和偶数以及奇数和偶数之和,java,collections,lambda,java-8,java-stream,Java,Collections,Lambda,Java 8,Java Stream,如何使用Java8的流方法在集合中拆分奇数和偶数并求和 public class SplitAndSumOddEven { public static void main(String[] args) { // Read the input try (Scanner scanner = new Scanner(System.in)) { // Read the number of inputs needs to read.

如何使用Java8的流方法在集合中拆分奇数和偶数并求和

public class SplitAndSumOddEven {

    public static void main(String[] args) {

        // Read the input
        try (Scanner scanner = new Scanner(System.in)) {

            // Read the number of inputs needs to read.
            int length = scanner.nextInt();

            // Fillup the list of inputs
            List<Integer> inputList = new ArrayList<>();
            for (int i = 0; i < length; i++) {
                inputList.add(scanner.nextInt());
            }

            // TODO:: operate on inputs and produce output as output map
            Map<Boolean, Integer> oddAndEvenSums = inputList.stream(); // Here I want to split odd & even from that array and sum of both

            // Do not modify below code. Print output from list
            System.out.println(oddAndEvenSums);
        }
    }
}
公共类拆分和SUMODDEN{
公共静态void main(字符串[]args){
//读取输入
try(扫描器=新扫描器(System.in)){
//读取需要读取的输入数。
int length=scanner.nextInt();
//填写输入列表
List inputList=新建ArrayList();
for(int i=0;i
在两个单独的流操作中执行此操作最简单(也是最干净的),例如:

public class OddEvenSum {

  public static void main(String[] args) {

    List<Integer> lst = ...; // Get a list however you want, for example via scanner as you are. 
                             // To test, you can use Arrays.asList(1,2,3,4,5)

    Predicate<Integer> evenFunc = (a) -> a%2 == 0;
    Predicate<Integer> oddFunc = evenFunc.negate();

    int evenSum = lst.stream().filter(evenFunc).mapToInt((a) -> a).sum();
    int oddSum = lst.stream().filter(oddFunc).mapToInt((a) -> a).sum();

    Map<String, Integer> oddsAndEvenSumMap = new HashMap<>();
    oddsAndEvenSumMap.put("EVEN", evenSum);
    oddsAndEvenSumMap.put("ODD", oddSum);

    System.out.println(oddsAndEvenSumMap);
  }
}
公共类OddEvenSum{
公共静态void main(字符串[]args){
List lst=…;//以您想要的方式获取列表,例如通过扫描仪按原样获取列表。
//要进行测试,可以使用Arrays.asList(1,2,3,4,5)
谓词evenFunc=(a)->a%2==0;
谓词oddFunc=evenFunc.negate();
int evenSum=lst.stream().filter(evenFunc.mapToInt((a)->a.sum();
int oddSum=lst.stream().filter(oddFunc).mapToInt((a)->a).sum();
Map oddsAndEvenSumMap=新HashMap();
oddsAndEvenSumMap.put(“偶数”,evenSum);
oddsandevensumap.put(“奇数”,oddSum);
System.out.println(oddsandevensumap);
}
}
我做的一个更改是将结果映射设置为
Map
,而不是
Map
。在后一个映射中,
true
的键代表什么还不清楚,而字符串键则稍微有效一些。不清楚您为什么需要一张地图,但我假设这将继续到问题的后面部分。

试试这个

    List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
    int[] a = list.stream()
        .map(n -> n % 2 == 0 ? new int[] {n, 0} : new int[] {0, n})
        .reduce(new int[] {0, 0}, (x, y) -> new int[] {x[0] + y[0], x[1] + y[1]});
    System.out.println("even sum = " + a[0]);   // -> even sum = 20
    System.out.println("odd sum = " + a[1]);    // -> odd sum = 25
List List=Arrays.asList(1,2,3,4,5,6,7,8,9);
int[]a=list.stream()
.map(n->n%2==0?新int[]{n,0}:新int[]{0,n})
.reduce(新int[]{0,0},(x,y)->新int[]{x[0]+y[0],x[1]+y[1]});
System.out.println(“偶数和=“+a[0]);/>偶数和=20
System.out.println(“奇数和=“+a[1]);/>奇数和=25
您可以使用以下功能:

Map<Boolean, Integer> result = inputList.stream().collect(
       Collectors.partitioningBy(x -> x%2 == 0, Collectors.summingInt(Integer::intValue)));
Map result=inputList.stream().collect(
Collectors.partitionBy(x->x%2==0,Collectors.summingit(Integer::intValue));

生成的映射包含
true
键中的偶数和
false
键中的奇数和。

请提供一些代码来说明您迄今为止所做的工作。你尝试了什么。你现在在哪里?请看更新的问题@MarquisBlountAn
enum
甚至比字符串更好。如何更改它以返回奇数对偶数的计数而不是总和?Collectors.counting()似乎不起作用。@NearoUser counting()返回long,因此您需要更改为
Map