一、java8自带常用的函数式接口Predicate boolean test(T t) 传入一个参数返回boolean值
Consumer void accept(T t) 传入一个参数,无返回值
Function R apply(T t) 传入一个参数,返回另一个类型
准备数据//计算机俱乐部
private static List computerClub = Arrays.asList(
new Student("2015134001", "小明", 15, "1501"),
new Student("2015134003", "小王", 14, "1503"),
new Student("2015134006", "小张", 15, "1501"),
new Student("2015134008", "小梁", 17, "1505")
);
//篮球俱乐部
private static List basketballClub = Arrays.asList(
new Student("2015134012", "小c", 13, "1503"),
new Student("2015134013", "小s", 14, "1503"),
new Student("2015134015", "小d", 15, "1504"),
new Student("2015134018", "小y", 16, "1505")
);
//乒乓球俱乐部
private static List pingpongClub = Arrays.asList(
new Student("2015134022", "小u", 16, "1502"),
new Student("2015134021", "小i", 14, "1502"),
new Student("2015134026", "小m", 17, "1504"),
new Student("2015134027", "小n", 16, "1504")
);
private static List> allClubStu = new ArrayList<>();
allClubStu.add(computerClub);
allClubStu.add(basketballClub);
allClubStu.add(pingpongClub);复制代码
二、常用的stream三种创建方式集合 Collection.stream()
静态方法 Stream.of
数组 Arrays.stream//1.集合
Stream stream = basketballClub.stream();
//2.静态方法
Stream stream2 = Stream.of("a", "b", "c");
//3.数组
String[] arr = {"a","b","c"};
Stream stream3 = Arrays.stream(arr);复制代码
三、Stream的终止操作foreach(Consumer c) 遍历操作
collect(Collector) 将流转化为其他形式
max(Comparator) 返回流中最大值
min(Comparator) 返回流中最小值
count 返回流中元素综述
四、Collectors 具体方法toList List 把流中元素收集到List
toSet Set 把流中元素收集到Set
toCollection Coolection 把流中元素收集到Collection中
groupingBy Map 根据K属性对流进行分组
partitioningBy Map 根据boolean值进行分组//此处只是演示 此类需求直接用List构造器即可
List collect = computerClub.stream().collect(Collectors.toList());
Set collect1 = pingpongClub.stream().collect(Collectors.toSet());
//注意key必须是唯一的 如果不是唯一的会报错而不是像普通map那样覆盖
Map collect2 = pingpongClub.stream()
.collect(Collectors.toMap(Student::getIdNum, Student::getName));
//分组 类似于数据库中的group by
Map> collect3 = pingpongClub.stream()
.collect(Collectors.groupingBy(Student::getClassNum));
//字符串拼接 第一个参数是分隔符 第二个参数是前缀 第三个参数是后缀
String collect4 = pingpongClub.stream().map(Student::getName).collect(Collectors.joining(",", "【", "】"));
//【小u,小i,小m,小n】
//三个俱乐部符合年龄要求的按照班级分组
Map> collect5 = Stream.of(basketballClub, pingpongClub, computerClub)
.flatMap(e -> e.stream().filter(s -> s.getAge()
.collect(Collectors.groupingBy(Student::getClassNum));
//按照是否年龄>16进行分组 key为true和false
ConcurrentMap> collect6 = Stream.of(basketballClub, pingpongClub, computerClub)
.flatMap(Collection::stream)
.collect(Collectors.groupingByConcurrent(s -> s.getAge() > 16));复制代码
五、Stream的中间操作
1. filter(Predicate) 筛选流中某些元素//筛选1501班的学生
computerClub.stream().filter(e -> e.getClassNum().equals("1501")).forEach(System.out::println);
//筛选年龄大于15的学生
List collect = computerClub.stream().filter(e -> e.getAge() > 15).collect(Collectors.toList());复制代码
2. map(Function f) 接收流中元素,并且将其映射成为新元素,例如从student对象中取name属性//篮球俱乐部所有成员名 + 暂时住上商标^_^,并且获取所有队员名
List collect1 = basketballClub.stream()
.map(e -> e.getName() + "^_^")
.collect(Collectors.toList());
collect1.forEach(System.out::println);
//小c^_^^_^
//小s^_^^_^
//小d^_^^_^
//小y^_^^_^复制代码
3. flatMap(Function f) 将所有流中的元素并到一起连接成一个流//获取年龄大于15的所有俱乐部成员
List collect2 = Stream.of(basketballClub, computerClub, pingpongClub)
.flatMap(e -> e.stream().filter(s -> s.getAge() > 15))
.collect(Collectors.toList());
collect2.forEach(System.out::println);
//用双层list获取所有年龄大于15的俱乐部成员
List collect3 = allClubStu.stream()
.flatMap(e -> e.stream().filter(s -> s.getAge() > 15))
.collect(Collectors.toList());
collect3.forEach(System.out::println);复制代码
4. peek(Consumer c) 获取流中元素,操作流中元素,与foreach不同的是不会截断流,可继续操作流//篮球俱乐部所有成员名 + 赞助商商标^_^,并且获取所有队员详细内容
List collect = basketballClub.stream()
.peek(e -> e.setName(e.getName() + "^_^"))
.collect(Collectors.toList());
collect.forEach(System.out::println);
//Student{idNum='2015134012', name='小c^_^', age=13, classNum='1503'}
//Student{idNum='2015134013', name='小s^_^', age=14, classNum='1503'}
//Student{idNum='2015134015', name='小d^_^', age=15, classNum='1504'}
//Student{idNum='2015134018', name='小y^_^', age=16, classNum='1505'}复制代码
5. distinct() 通过流所生成元素的equals和hashCode去重
6. limit(long val) 截断流,取流中前val个元素
7. sorted(Comparator) 产生一个新流,按照比较器规则排序
8. sorted() 产生一个新流,按照自然顺序排序List list = Arrays.asList("b","b","c","a");
list.forEach(System.out::print); //bbca
List collect = list.stream().distinct().sorted().collect(Collectors.toList());
collect.forEach(System.out::print);//abc
//获取list中排序后的top2 即截断取前两个
List collect1 = list.stream().distinct().sorted().limit(2).collect(Collectors.toList());
collect1.forEach(System.out::print);//ab复制代码
六、匹配booelan allMatch(Predicate) 都符合
boolean anyMatch(Predicate) 任一元素符合
boolean noneMatch(Predicate) 都不符合boolean b = basketballClub.stream().allMatch(e -> e.getAge()
boolean b1 = basketballClub.stream().anyMatch(e -> e.getAge()
boolean b2 = basketballClub.stream().noneMatch(e -> e.getAge()
七、寻找元素findFirst——返回第一个元素
findAny——返回当前流中的任意元素Optional first = basketballClub.stream().findFirst();
if (first.isPresent()) {
Student student = first.get();
System.out.println(student);
}
Optional any = basketballClub.stream().findAny();
if (any.isPresent()) {
Student student2 = any.get();
System.out.println(student2);
}
Optional any1 = basketballClub.stream().parallel().findAny();
System.out.println(any1);复制代码
八、计数和极值count——返回流中元素的总个数
max——返回流中最大值
min——返回流中最小值long count = basketballClub.stream().count();
Optional max = basketballClub.stream().max(Comparator.comparing(Student::getAge));
if (max.isPresent()) {
Student student = max.get();
}
Optional min = basketballClub.stream().min(Comparator.comparingInt(Student::getAge));
if (min.isPresent()) {
Student student = min.get();
}复制代码
九、总结
相信xdm已经懂得了如何使用java的流式编程,点关注,不迷路,关注程序员曾曾,每天分享不同的Java知识,如果想要知道更多的有关Java的知识以及面试我这边整理了一个我自己的GitHub,大家如果有需要可以自行查看