话不多说,直接看代码演示

/**
* @description: stream 练习
* @author: hwx
* @date: 2022/02/10
**/
public class stream {
static class Person {
private String name; // 姓名
private int salary; // 薪资
private int age; // 年龄
private String sex; //性别
private String area; // 地区 // 构造方法
public Person(String name, int salary, int age,String sex,String area) {
this.name = name;
this.salary = salary;
this.age = age;
this.sex = sex;
this.area = area;
} public Person(String name, int salary, String sex, String area) {
this.name = name;
this.salary = salary;
this.sex = sex;
this.area = area;
} // 省略了get和set,请自行添加 public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getSalary() {
return salary;
} public void setSalary(int salary) {
this.salary = salary;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public String getArea() {
return area;
} public void setArea(String area) {
this.area = area;
} @Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", salary=" + salary +
", age=" + age +
", sex='" + sex + '\'' +
", area='" + area + '\'' +
'}';
}
} public static void main(String[] args) {
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("Tom", 8900, "male", "New York"));
personList.add(new Person("Tom", 8800, "male", "New York"));
personList.add(new Person("Jack", 7000, "male", "Washington"));
personList.add(new Person("Lily", 7800, "female", "Washington"));
personList.add(new Person("Anni", 8200, "female", "New York"));
personList.add(new Person("Owen", 9500, "male", "New York"));
personList.add(new Person("Alisa", 7900, "female", "New York")); //遍历输出符合条件的元素
personList.stream().filter(e -> e.salary > 8000).forEach(System.out::println);
//筛选薪资大于8000的
List<Person> collect = personList.stream().filter(e -> e.salary > 8000).collect(Collectors.toList());
// List<Person> collect22 = personList.stream().filter(e -> e::getSalary>8000).collect(Collectors.toList());
Set<Person> collect1 = personList.stream().filter(e -> e.salary > 8000).collect(Collectors.toSet());
//去重
Map<String, Integer> collect2 = personList.stream().filter(e -> e.salary > 8000).collect(Collectors.toMap(e -> e.name, e -> e.salary,(a1,a2)->a2));
//.map用法
List<String> collect3 = personList.stream().filter(e -> e.salary > 8000).map(e -> e.name).collect(Collectors.toList()); // 匹配第一个
Optional<Person> first = personList.stream().filter(e -> e.salary > 8000).findFirst();
// 匹配任意(适用于并行流)
Optional<Person> first1 = personList.parallelStream().filter(e -> e.salary > 8000).findFirst();
// 是否包含符合特定条件的元素
boolean b = personList.stream().anyMatch(e -> e.salary > 8000); System.out.println(collect);
System.out.println(collect1);
System.out.println(collect2);
System.out.println(collect3);
System.out.println("匹配第一个"+first);
System.out.println("匹配任意(适用于并行流)"+first1);
System.out.println("是否包含符合特定条件的元素"+b); //筛选出名字最长的
Optional<Person> max = personList.stream().max(Comparator.comparing(e -> e.name.length()));
//筛选出工资最高的
Optional<Person> max1 = personList.stream().max(Comparator.comparing(e -> e.salary));
System.out.println("筛选出名字最长的"+max);
System.out.println("筛选出工资最高的"+max1); //筛选出工资大于8000的总人数
long count = personList.stream().filter(e -> e.salary > 8000).count();
System.out.println("筛选出工资大于8000的总人数"+count); String[] strArr = { "abcd", "bcdd", "defde", "fTr" };
//英文字符串数组的元素全部改为大写
List<String> collect4 = Arrays.stream(strArr).map(String::toUpperCase).collect(Collectors.toList());
System.out.println("英文字符串数组的元素全部改为大写"+collect4); List<Integer> intList = Arrays.asList(1, 3, 5, 7, 9, 11);
//整数数组每个元素+3
List<Integer> collect5 = intList.stream().map(e -> e + 3).collect(Collectors.toList());
System.out.println("整数数组每个元素+3"+collect5); //将员工的薪资全部增加1000。
List<Integer> collect6 = personList.stream().map(e -> e.salary + 1000).collect(Collectors.toList());
System.out.println("将员工的薪资全部增加1000"+collect6);
List<Person> collect7 = personList.stream().map(e -> {
Person person = new Person(e.name, e.salary, e.sex, e.area);
person.setSalary(person.salary + 1000);
return person;
}).collect(Collectors.toList());
System.out.println("(不改变原先结构)将员工的薪资全部增加1000"+collect7);
/* List<Person> collect8 = personList.stream().map(e -> {
e.setSalary(e.salary + 1000);
return e;
}).collect(Collectors.toList());
System.out.println("(不改变原先结构)将员工的薪资全部增加1000(原先的list被改变)"+collect8);*/ //将两个字符数组合并成一个新的字符数组
List<String> list = Arrays.asList("m,k,l,a", "1,3,5,7");
List<String> collect9 = list.stream().flatMap(e -> {
// 将每个元素转换成一个stream
String[] split = e.split(",");
Stream<String> s2 = Arrays.stream(split);
return s2;
}).collect(Collectors.toList());
System.out.println("将两个字符数组合并成一个新的字符数组"+collect9); //求所有员工的工资之和
Optional<Integer> reduce = personList.stream().map(Person::getSalary).reduce(Integer::sum);
System.out.println("求所有员工的工资之和"+reduce);
Integer reduce1 = personList.stream().reduce(0, (sum, p) -> sum += p.getSalary(), (sum1, sum2) -> sum1 + sum2);
System.out.println("求所有员工的工资之和"+reduce1);
Integer sumSalary3 = personList.stream().reduce(0, (sum, p) -> sum += p.getSalary(), Integer::sum);
System.out.println("求所有员工的工资之和"+sumSalary3); // 求员工总数
Long collect10 = personList.stream().collect(Collectors.counting());
System.out.println("求员工总数"+collect10);
// 求平均工资
Double collect11 = personList.stream().collect(Collectors.averagingDouble(Person::getSalary));
System.out.println("求平均工资"+collect11);
// 求最高工资
Optional<Integer> collect12 = personList.stream().map(Person::getSalary).collect(Collectors.maxBy(Integer::compare));
System.out.println("求最高工资"+collect12);
// 求工资之和
Integer sum = personList.stream().collect(Collectors.summingInt(Person::getSalary));
System.out.println("员工工资总和:" + sum);
// 一次性统计所有信息
DoubleSummaryStatistics collect13 = personList.stream().collect(Collectors.summarizingDouble(Person::getSalary));
System.out.println("员工工资所有统计:" + collect13); //分组
// 将员工按薪资是否高于8000分组
Map<Boolean, List<Person>> collect8 = personList.stream().collect(Collectors.partitioningBy(e -> e.getSalary() > 8000));
System.out.println("将员工按薪资是否高于8000分组"+collect8);
// 将员工按性别分组
Map<String, List<Person>> collect14 = personList.stream().collect(Collectors.groupingBy(Person::getSex));
System.out.println("将员工按性别分组"+collect14);
// 将员工先按性别分组,再按地区分组
Map<String, Map<String, List<Person>>> group2 = personList.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(Person::getArea)));
System.out.println("将员工先按性别分组,再按地区分组"+group2); //接合
String names = personList.stream().map(p -> p.getName()).collect(Collectors.joining(","));
System.out.println("所有员工的姓名:" + names); //排序
// 按工资升序排序(自然排序)
List<String> newList = personList.stream().sorted(Comparator.comparing(Person::getSalary)).map(Person::getName)
.collect(Collectors.toList());
System.out.println("按工资升序排序(自然排序)"+newList);
// 按工资倒序排序
List<String> newList2 = personList.stream().sorted(Comparator.comparing(Person::getSalary).reversed())
.map(Person::getName).collect(Collectors.toList());
System.out.println("按工资倒序排序"+newList);
// 先按工资再按年龄升序排序
List<String> newList3 = personList.stream()
.sorted(Comparator.comparing(Person::getSalary).thenComparing(Person::getAge)).map(Person::getName)
.collect(Collectors.toList());
System.out.println("先按工资再按年龄升序排序:" + newList3);
// 先按工资再按年龄自定义排序(降序)
List<String> newList4 = personList.stream().sorted((p1, p2) -> {
if (p1.getSalary() == p2.getSalary()) {
return p2.getAge() - p1.getAge();
} else {
return p2.getSalary() - p1.getSalary();
}
}).map(Person::getName).collect(Collectors.toList());
System.out.println("先按工资再按年龄自定义降序排序:" + newList4); String[] arr1 = { "a", "b", "c", "d" };
String[] arr2 = { "d", "e", "f", "g" }; Stream<String> stream1 = Stream.of(arr1);
Stream<String> stream2 = Stream.of(arr2);
// concat:合并两个流 distinct:去重
List<String> newList8 = Stream.concat(stream1, stream2).distinct().collect(Collectors.toList());
// limit:限制从流中获得前n个数据
List<Integer> collect88 = Stream.iterate(1, x -> x + 2).limit(10).collect(Collectors.toList());
// skip:跳过前n个数据
List<Integer> collect888 = Stream.iterate(1, x -> x + 2).skip(1).limit(5).collect(Collectors.toList()); System.out.println("流合并:" + newList8);
System.out.println("limit:" + collect88);
System.out.println("skip:" + collect888); }
}

Java 8 stream的详细用法的更多相关文章

  1. Java中stream的详细用法

    来自于:Java 8 stream的详细用法_旅行者-CSDN博客_java stream 一.概述 Stream 是 Java8 中处理集合的关键抽象概念,它可以指定你希望对集合进行的操作,可以执行 ...

  2. Java中System的详细用法

    System.arraycopy System.arraycopy的函数原型是: public static void arraycopy(Object src, int srcPos, Object ...

  3. Java中TreeSet的详细用法

    第1部分 TreeSet介绍 TreeSet简介 TreeSet 是一个有序的集合,它的作用是提供有序的Set集合.它继承于AbstractSet抽象类,实现了NavigableSet, Clonea ...

  4. Java中Collections类详细用法

    1.sort(Collection)方法的使用(含义:对集合进行排序). 例:对已知集合c进行排序? public class Practice { public static void main(S ...

  5. Java中继承的详细用法

    关于上一篇构造方法后的继承方法 构造方法链接 extends是继承的关键字 例: 下面的代码BB和CC就是AA的子类 允许一个父类有多个子类,但不允许一个子类有多个父类 /*final*/ class ...

  6. 讲透JAVA Stream的collect用法与原理,远比你想象的更强大

    大家好,又见面了. 在我前面的文章<吃透JAVA的Stream流操作,多年实践总结>中呢,对Stream的整体情况进行了细致全面的讲解,也大概介绍了下结果收集器Collectors的常见用 ...

  7. 【转】java.util.vector中的vector的详细用法

    [转]java.util.vector中的vector的详细用法 ArrayList会比Vector快,他是非同步的,如果设计涉及到多线程,还是用Vector比较好一些 import java.uti ...

  8. Java 8 Stream 用法

    一.Stream是什么 Stream 不是集合元素,它不是数据结构并不保存数据,它是有关算法和计算的,它更像一个高级版本的 Iterator.原始版本的 Iterator,用户只能显式地一个一个遍历元 ...

  9. java集合 stream 相关用法(1)

    java8新增一种流式数据,让操作集合数据更简单方便. 定义基本对象: public class Peo { private String name; private String id; publi ...

随机推荐

  1. 【Java】包装类

    文章目录 包装类 什么是包装类 基本数据类型-->包装类 包装类-->基本数据类型 自动装箱与自动拆箱 基本数据类型.包装类与String的转换 基础数据类型.包装类-->Strin ...

  2. uboot无法通过nfs加载ubuntu18.04中的文件(转)

    问题描述: i.mx6ull开发板,采用alientek官方维护的uboot,使用ubuntu18.04 lts作为nfs server,导致开发板uboot上nfs命令无法加载网络文件系统. 解决: ...

  3. HTML+CSS+Javascript实现轮播图效果

    HTML+CSS+Javascript实现轮播图效果 注意:根据自己图片大小来更改轮播图大小. <!doctype html> <html> <head> < ...

  4. Solon Web 开发,十一、国际化

    Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...

  5. 【笔记】thanos ruler组件

    阅读官网文档后的笔记:https://thanos.io/tip/components/rule.md/ 感受 官网第一个话就强调风险,看来坑很多,能不用尽量不用 recording rule &am ...

  6. 【记录一个问题】thanos receiver在更换tsdb文件后,内存并未显著下降

    在16:14,切换了TSDB 在16::15分切换了TSDB,并且上游未写入数据 容器的内存并未在这个时刻明显下降 疑惑: 如果thanos receiver的内存占用不与time series数量正 ...

  7. golang中字符串的底层实现原理和常见功能

    1. 字符串的底层实现原理 package main import ( "fmt" "strconv" "unicode/utf8" ) f ...

  8. find -or 用法

    find /opt/IBM/WebSphere85/ -name *loggeter* - or -name *loggetter* | xargs rm -rf

  9. 分享一个基于 ABP(.NET 5.0) + vue-element-admin 管理后台

    1.前言 分享一个基于ABP(.NET 5.0) + vue-element-admin项目.希望可以降低新手对于ABP框架的学习成本,感兴趣的同学可以下载项目启动运行一下.对于想选型采用ABP框架的 ...

  10. 拒绝编译等待 - 动态研发模式 ARK

    作者:字节跳动终端技术--徐纪光 背景 iOS 业界研发模式多为 CocoaPods + Xcode + Git 的多仓组件化开发模型.为追求极致的研发体验.提升研发效率,对该研发模式进行了大量优化, ...