Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.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_Java 8_Java Stream - Fatal编程技术网

使用java流获取求和、平均值和排序

使用java流获取求和、平均值和排序,java,java-8,java-stream,Java,Java 8,Java Stream,我正在学习java流,很难解决下面的问题。我陷入困境的原因是我不知道如何处理流 通过执行list.stream().count(),我偶然发现了“count”的解决方案,但除此之外,我无法继续。请帮助我如何处理这些问题,并告诉我为什么list.stream().count()在这种情况下有效。到目前为止,我已经尝试了我所学到的一切 public class Question { public static void main(String[] args) { List&l

我正在学习java流,很难解决下面的问题。我陷入困境的原因是我不知道如何处理

通过执行
list.stream().count()
,我偶然发现了“count”的解决方案,但除此之外,我无法继续。请帮助我如何处理这些问题,并告诉我为什么
list.stream().count()
在这种情况下有效。到目前为止,我已经尝试了我所学到的一切

public class Question {
    public static void main(String[] args) {
        List<Integer>list = Arrays.asList(5,3,4,1,2);
        System.out.println("sum by using Stream : " + sum);
        System.out.println("count by using Stream: " + count);
        System.out.println("average by using Stream : " + avg);
        System.out.println("sort by using Stream");
    }
}
公开课问题{
公共静态void main(字符串[]args){
Listlist=Arrays.asList(5,3,4,1,2);
System.out.println(“使用流求和:+sum”);
System.out.println(“使用流进行计数:+count”);
System.out.println(“使用流的平均值:+avg”);
System.out.println(“使用流排序”);
}
}

您可以这样做:

List<Integer> list = Arrays.asList(5,3,4,1,2);
        System.out.println("sum by using Stream : " + list.stream().mapToInt(Integer::intValue).sum());
        System.out .println("count by using Stream: " + list.stream().mapToInt(Integer::intValue).count());
        System.out.println("average by using Stream : " + list.stream().mapToInt(Integer::intValue).average());
        System.out.println("sort by using Stream: " + list.stream().sorted().collect(Collectors.toList()));
List List=Arrays.asList(5,3,4,1,2);
System.out.println(“使用流求和:”+list.Stream().mapToInt(Integer::intValue.sum());
System.out.println(“使用流进行计数:+list.Stream().mapToInt(Integer::intValue.count());
System.out.println(“使用流进行平均:”+list.Stream().mapToInt(Integer::intValue.average());
System.out.println(“使用流进行排序:+list.Stream().sorted().collect(Collectors.toList()));

对于已排序的,您必须再次流式处理。

您应该使用
mapToInt
mapToDouble

您可以通过
forEach(System.out::println)

List List=Arrays.asList(5,3,4,1,2);
System.out.println(“使用流求和:”+list.Stream().mapToInt(Integer::intValue.sum());
System.out.println(“使用流进行计数:+list.Stream().count());
System.out.println(“使用流进行平均:”+list.Stream().mapToInt(Integer::intValue.average());
System.out.println(“使用流进行排序:”);
list.stream().sorted().forEach(System.out::println);

之所以
list.stream().count()
有效,但
list.stream().sum()
无效,是因为
list.stream()
返回一个,并且有一个方法,但没有
stream::sum
stream::average

要首先获得总和和平均值,您必须将执行
list.Stream()
时得到的
流中的每个整数值映射到一个值。这是流的int原语专门化。这可以使用以下方法完成:

为此,您可以使用以下方法和步骤:

或者更好的是,您可以使用来获得总和、计数和平均值(以及最小值和最大值):

要对值进行排序,可以使用以下方法:


这是一篇很好的帖子,可以帮助您理解如何使用Java8流Api。

是我的问题还是您的代码不完整?
sum
count
avg
是如何初始化的?您看过Streams API了吗?它告诉你存在什么方法。Oracle站点上有很多很多信息。同样,来自streamable的信息会提供该流中元素的数量。它是不完整的,所以我需要几行代码来计算那些system.out.println(),我不知道如何获得求和、平均值和排序。从这里开始:这个示例甚至显示了求和的示例代码。或者
Arrays.asList(1,2,3,4).stream().collect(Collectors.summaringit(Integer::intValue))
@Holger是否有办法知道何时选择一种实现或另一种实现?我的意思是,很多时候,可以使用流中间操作或收集器来完成操作,即
stream.map
vs
collector.mapping
、reduce、fitlering、flatmapping和summary,etc@FedericoPeraltaSchaffn呃,对于这个特定的情况,这并不重要,选择权在你。两者都值得了解,因为当你想扩展代码时,可能会有所不同,请参阅以了解详细信息。而且。@Eugene IntSummaryStatistics的想法最后是最近的记忆:)
IntSummaryStatistics stats = Arrays.asList(1,2,3,4)
    .stream()
    .mapToInt(Integer::intValue)
    .summaryStatistics();

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

System.out.println("sum by using Stream : " + list.stream().mapToInt(Integer::intValue).sum());

System.out .println("count by using Stream: " + list.stream().count());

System.out.println("average by using Stream : " + list.stream().mapToInt(Integer::intValue).average());

System.out.println("sort by using Stream: " );

list.stream().sorted().forEach(System.out::println);
list.stream().mapToInt(Integer::intValue)
System.out.println("sum by using Stream : " + list.stream().mapToInt(Integer::intValue).sum());
System.out.println("average by using Stream : " + list.stream().mapToInt(Integer::intValue).average());
System.out.println("sum, count, avg, min and max using Stream : " + list.stream().mapToInt(Integer::intValue).summaryStatistics());
System.out.println("sort by using Stream: " + list.stream().sorted().collect(Collectors.toList()));