本文介绍了Collectors.toList()显示错误"Expected 3 arguments but found 1".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的收藏夹Collectors.toList()显示此错误:

Why is my collect Collectors.toList() showing this error:

package com.knoldus;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;


interface Cityeration<T> {
    public abstract List<T> Cityeration(List<T> first, List<T> Second);
}

public class ListMultiplication {
    public static void main(String s[]) {
        List firstList = Arrays.asList(1, 2, 3, 4, 5);
        List secondList = Arrays.asList(1, 2, 3, 4, 5);

        Cityeration c = (first, second) -> IntStream.range(0, first.size())
                        .map(i -> first.get(i) * second.get(i))
                        .collect(Collectors.toList());
    }
}

推荐答案

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

interface Cityeration<T> {
    public abstract List<T> cityeration(List<T> first, List<T> Second);
}

public class ListMultiplication {
    public static void main(String s[]) {
        List<Integer> firstList = Arrays.asList(1, 2, 3, 4, 5, 6);
        List<Integer> secondList = Arrays.asList(1, 2, 3, 4, 5);

        Cityeration<Integer> c = (first, second) -> IntStream
                .range(0, first.size() <= second.size() ? first.size() : second.size())
                .map(i -> first.get(i) * second.get(i)).boxed().collect(Collectors.toList());
        System.out.println(c.cityeration(firstList, secondList));
    }
}

输出:

[1, 4, 9, 16, 25]

注意:请确保

  1. 使用通用类型而不是原始类型.
  2. 比较列表的大小,以避免ArrayIndexOutOfBoundsException.

这篇关于Collectors.toList()显示错误"Expected 3 arguments but found 1".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 07:24