Java 无法将结果保存到列表<;长期>;在操纵两条流之后 我通过两个流进行过滤,在中间做一个除法,但最后我不能把结果收集到一个列表中。你能告诉我我做错了什么吗

Java 无法将结果保存到列表<;长期>;在操纵两条流之后 我通过两个流进行过滤,在中间做一个除法,但最后我不能把结果收集到一个列表中。你能告诉我我做错了什么吗,java,lambda,collections,Java,Lambda,Collections,这是我的密码 List<Long> average_population = total_population.stream() .flatMapToLong( a-> number_of_cities.stream().mapToLong( b-> b/a )) .collect(null, Collectors.toList() ); <- error List average_population=总_population.stream

这是我的密码

List<Long> average_population = total_population.stream() 
    .flatMapToLong( a-> number_of_cities.stream().mapToLong( b-> b/a ))
    .collect(null, Collectors.toList() );   <- error
List average_population=总_population.stream()
.flatMapToLong(a->城市数量.stream().mapToLong(b->b/a))
.collect(null,Collectors.toList());)
类型不匹配:无法从收集器>转换为ObjLongConsumer

需要3个参数。 您可能正在寻找以下内容:

List<Long> average_population =
  total_population.stream()
    .flatMapToLong(a -> number_of_cities.stream().mapToLong(b -> b / a))
    .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
需要3个参数。 您可能正在寻找以下内容:

List<Long> average_population =
  total_population.stream()
    .flatMapToLong(a -> number_of_cities.stream().mapToLong(b -> b / a))
    .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);

如果要在
列表中收集结果
,则需要将值框起来。
flatMapToLong
给出了一个
LongStream
,它给出了原语
long
,而不是装箱
long
。您可以使用.boxed()操作符从长流生成已装箱的对象

LongStream.of(1l, 2l, 3l).boxed().collect(Collectors.toList());
所以我想它会变成:

List<Long> average_population = total_population.stream()
      .flatMapToLong(a -> number_of_cities.stream().mapToLong(b -> b / a))
      .boxed()
      .collect(Collectors.toList());
List average_population=总_population.stream()
.flatMapToLong(a->城市数量.stream().mapToLong(b->b/a))
.boxed()
.collect(Collectors.toList());

如果要在
列表中收集结果,需要将值框起来。
flatMapToLong
给出了一个
LongStream
,它给出了原语
long
,而不是装箱
long
。您可以使用.boxed()操作符从长流生成已装箱的对象

LongStream.of(1l, 2l, 3l).boxed().collect(Collectors.toList());
所以我想它会变成:

List<Long> average_population = total_population.stream()
      .flatMapToLong(a -> number_of_cities.stream().mapToLong(b -> b / a))
      .boxed()
      .collect(Collectors.toList());
List average_population=总_population.stream()
.flatMapToLong(a->城市数量.stream().mapToLong(b->b/a))
.boxed()
.collect(Collectors.toList());