6.8 并行流 parallelStream
parallelStream 会充分利用我们计算机多线程,使程序执行更快,耗时更短。用法上与 stream 没有区别。
实例代码:
package cn.zxhysy.jdk8.steam;
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<String> list = new ArrayList<>(5000000);
for(int i = 0; i < 5000000; ++i){
list.add(UUID.randomUUID().toString());
}
System.out.println("开始排序:");
long startTime = System.nanoTime();
// list.stream().sorted().count();
list.parallelStream().sorted().count();
long endTime = System.nanoTime();
long millis = TimeUnit.NANOSECONDS.toMillis(endTime - startTime);
System.out.println("排序耗时:" + millis);
// stream 7927 parallelStream 4433
}
}
不同计算机上,耗时是不一样的,但还是可以看出的大概,在排序是,parallelStream 耗时比较短。
6.9 流的短路操作
流的执行顺序是,对源的第一个元素进行一些列操作之后再进行第二个元素的操作,以此类推,且在操作中存在短路操作,只要找到条件的,后面的元素就不会再执行了
package cn.zxhysy.jdk8.steam;
import java.util.Arrays;
import java.util.List;
public class StreamTest10 {
public static void main(String[] args) {
List<String> list = Arrays.asList("hello1", "world", "helloworld");
//打印长度为5的第一个单词
// list.stream().mapToInt(item -> item.length())
// .filter(length -> length==5)
// .findFirst().ifPresent(System.out::println);
// 流的执行顺序是,对源的第一个元素进行一些列操作之后再进行第二个元素的操作,以此类推
// 且在操作中存在 短路 操作
list.stream().mapToInt(item -> {
int length = item.length();
//Arrays.asList("hello", "world", "helloworld"); 输出 hello
//Arrays.asList("hello1", "world", "helloworld"); 输出 hello1 world
//Arrays.asList("hello1", "world1", "helloworld"); 输出 hello1 world1 helloworld
System.out.println(item);
return length;
}).filter(length -> length == 5).findFirst().ifPresent(System.out::println);
}
}
6.10 流的分组和分区
给数据分组。分区是一种特殊的分组,只有两个组,分区,只分为两个区,在程序中即true和false
- 分组:group by
- 分区:partition by
试例代码:
package cn.zxhysy.jdk8.steam;
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<Student> students = Arrays.asList(student1, student2, student3, student4);
// 对学生姓名进行分组
// java8前,我们需要循环列表,取出学生名字,
// 检查map中是否存在key为名字,存在则取出list添加进去,不存在则创建list集合添加的map中
// 最后返回 map<name,list>
// stream 流写法
// Map<String, List<Student>> map = students.stream().collect(Collectors.groupingBy(Student::getName));
// System.out.println(map);
// 根据名字分组统计个数
// Map<String, Long> map = students.stream().collect(Collectors.groupingBy(Student::getName, Collectors.counting()));
// System.out.println(map);
// 根据名字分组求分数平均值
// Map<String, Double> map = students.stream()
// .collect(Collectors.groupingBy(Student::getName, Collectors.averagingDouble(Student::getScore)));
// System.out.println(map);
// 分区,只分为两个区,即true和false
// 判断分数大于90的
Map<Boolean, List<Student>> map = students.stream()
.collect(Collectors.partitioningBy(student -> student.getScore() >= 90));
System.out.println(map);
}
}