博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
stream
阅读量:5816 次
发布时间:2019-06-18

本文共 4395 字,大约阅读时间需要 14 分钟。

 
import java.util.ArrayList;import java.util.List;import java.util.UUID;import java.util.concurrent.TimeUnit;public class StreamTest9 {    public static void main(String[] args) {        List
list = new ArrayList<>(5000000); for(int i = 0; i < 5000000; ++i) { list.add(UUID.randomUUID().toString()); } System.out.println("开始排序"); long startTime = System.nanoTime(); // 为何提示sorted方法是不必要的? list.parallelStream().sorted().count(); long endTime = System.nanoTime(); long millis = TimeUnit.NANOSECONDS.toMillis(endTime - startTime); System.out.println("排序耗时:" + millis); }}
import java.util.Arrays;import java.util.List;public class StreamTest10 {    public static void main(String[] args) {        List
list = Arrays.asList("hello1", "world", "hello world");// list.stream().mapToInt(item -> item.length()).filter(length -> length == 5).// findFirst().ifPresent(System.out::println); list.stream().mapToInt(item -> { int length = item.length(); System.out.println(item); return length; }).filter(length -> length == 5).findFirst().ifPresent(System.out::println); }}
import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;public class StreamTest11 {    public static void main(String[] args) {        List
list = Arrays.asList("hello welcome", "world hello", "hello world hello", "hello welcome");// List
result = list.stream().map(item -> item.split(" ")).distinct().// collect(Collectors.toList());// result.forEach(item -> Arrays.asList(item).forEach(System.out::println)); List
result = list.stream().map(item -> item.split(" ")).flatMap(Arrays::stream).distinct(). collect(Collectors.toList()); result.forEach(System.out::println); }}
import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;public class StreamTest12 {    public static void main(String[] args) {        List
list1 = Arrays.asList("Hi", "Hello", "你好"); List
list2 = Arrays.asList("zhangsan", "lisi", "wangwu", "zhaoliu"); List
result = list1.stream().flatMap(item -> list2.stream().map(item2 -> item + " " + item2)). collect(Collectors.toList()); result.forEach(System.out::println); }}
import java.util.Arrays;import java.util.List;import java.util.Map;import java.util.stream.Collectors;public class StreamTest13 {    public static void main(String[] args) {        Student student1 = new Student("zhangsan", 100, 20);        Student student2 = new Student("lisi", 90, 20);        Student student3 = new Student("wangwu", 90, 30);        Student student4 = new Student("zhangsan", 80, 40);        List
students = Arrays.asList(student1, student2, student3, student4);// Map
> map = students.stream().// collect(Collectors.groupingBy(Student::getName));// Map
> map = students.stream().// collect(Collectors.groupingBy(Student::getScore));// Map
map = students.stream().// collect(Collectors.groupingBy(Student::getName, Collectors.counting()));// Map
map = students.stream().// collect(Collectors.groupingBy(Student::getName, Collectors.averagingDouble(Student::getScore)));// Map
> map = students.stream().// collect(Collectors.partitioningBy(student -> student.getScore() >= 90));// System.out.println(map); }}
public class Student {    private String name;    private int score;    private int age;    public Student(String name, int score, int age) {        this.name = name;        this.score = score;        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getScore() {        return score;    }    public void setScore(int score) {        this.score = score;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}

 

转载于:https://www.cnblogs.com/koushr/p/5954679.html

你可能感兴趣的文章
FCN图像分割
查看>>
ios xmpp demo
查看>>
python matplotlib 中文显示参数设置
查看>>
数据库事务隔离级别
查看>>
os模块大全详情
查看>>
【ros】Create a ROS package:package dependencies报错
查看>>
kali linux 更新问题
查看>>
HDU1576 A/B【扩展欧几里得算法】
查看>>
廖雪峰javascript教程学习记录
查看>>
WebApi系列~目录
查看>>
Java访问文件夹中文件的递归遍历代码Demo
查看>>
项目笔记:测试类的编写
查看>>
通过容器编排和服务网格来改进Java微服务的可测性
查看>>
re:Invent解读:没想到你是这样的AWS
查看>>
PyTips 0x02 - Python 中的函数式编程
查看>>
阿里云安全肖力:安全基础建设是企业数字化转型的基石 ...
查看>>
使用《Deep Image Prior》来做图像复原
查看>>
如何用纯 CSS 为母亲节创作一颗像素画风格的爱心
查看>>
Linux基础命令---rmdir
查看>>
iOS sqlite3(数据库)
查看>>