> 文档中心 > Stream流简介

Stream流简介


Stream流简介

1.Stream流的获取

public class StreamDemo {    public static void main(String[] args) { Collection<String> lis=new ArrayList<>(); Stream<String> listStream=lis.stream(); Map<String,Integer> map=new HashMap<>(); /*获取map集合的key的stream流*/ Stream<String> mapKeyStream=map.keySet().stream(); /*获取map集合的values的stream流*/ Stream<Integer> mapValuesStream=map.values().stream(); /*获取map集合的键和值的Stream流*/ Stream<Map.Entry<String,Integer>>keyAndValuesStream=map.entrySet().stream(); /*数组的Stream流的获取方式*/ String[] names={"张三","李四","王五"}; Stream<String> array=Arrays.stream(names); Stream<String> arrays=Stream.of(names);    }}

2.Stream流常见的API

public class StreamDemo1 {    /*Stream流的实例*/    public static void main(String[] args) { List<String> list=new ArrayList<>(); list.add("张十三"); list.add("张三"); list.add("李四四"); list.add("王五"); list.add("赵六"); list.add("张无忌"); list.add("张无忌"); /*取出姓张的*/ list.stream().filter(s->s.startsWith("张")).forEach(s-> System.out.println(s)); System.out.println("------------------------------"); /*取出长度为3的*/ list.stream().filter(s->s.length()==3).forEach(s-> System.out.println(s)); System.out.println("------------------------------"); /*取出姓张的前两个*/ list.stream().filter(s->s.startsWith("张")).limit(2).forEach(s-> System.out.println(s)); System.out.println("------------------------------"); /*取出姓张的并且跳过前两个*/ list.stream().filter(s->s.startsWith("张")).skip(2).forEach(s-> System.out.println(s)); System.out.println("------------------------------"); /*map加工方法,可以给集合中的每一个元素进行加工*/ list.stream().map(s->"电视剧:"+s).forEach(s-> System.out.println(s)); System.out.println("------------------------------"); /*将集合中的员工加工成一个对象(得有这个类)*/ list.stream().map(s->new Student(s)).forEach(s -> System.out.println(s)); System.out.println("------------------------------"); /*当然,如果其中拉姆达前后的参数一样也可以有另一种书写方式*/ list.stream().map(Student::new).forEach(System.out::println); System.out.println("------------------------------"); /*合并流*/ Stream<String> s1=list.stream().filter(s->s.startsWith("张")); Stream<String> s2=Stream.of("javaSE","javaEE"); Stream<String> s3=Stream.concat(s1,s2); s3.forEach(System.out::println); System.out.println("------------------------------"); /*当然也可以合并不同数据类型的流,只不过新的流要使用Object来接收*/ Stream<String> ss1=list.stream().filter(s->s.startsWith("张")); Stream<Integer>ss2=Stream.of(2022,4,15); Stream<Object> ss3=Stream.concat(ss1,ss2); ss3.forEach(System.out::println); System.out.println("------------------------------"); /*去除Stream流中重复的元素依靠 hashCode() 和 equals() 方法*/ list.stream().distinct().forEach(System.out::println);    }}

运行结果

在这里插入图片描述

3.Stream流案例

在这里插入图片描述

Employee类

public class Employee {    private String name;    private char sex;    private double salary;    private double bonus;    private String punish; // 处罚信息    public Employee(){    }    public Employee(String name, char sex, double salary, double bonus, String punish) { this.name = name; this.sex = sex; this.salary = salary; this.bonus = bonus; this.punish = punish;    }    public String getName() { return name;    }    public void setName(String name) { this.name = name;    }    public char getSex() { return sex;    }    public void setSex(char sex) { this.sex = sex;    }    public double getSalary() { return salary;    }    public void setSalary(double salary) { this.salary = salary;    }    public double getBonus() { return bonus;    }    public void setBonus(double bonus) { this.bonus = bonus;    }    public String getPunish() { return punish;    }    public void setPunish(String punish) { this.punish = punish;    }    public double getTotalSalay(){ return salary * 12 + bonus;    }    @Override    public String toString() { return "Employee{" +  "name='" + name + '\'' +  ", sex=" + sex +  ", salary=" + salary +  ", bonus=" + bonus +  ", punish='" + punish + '\'' +  '}'+"\n";    }}

Topperformer类

public class Topperformer {    private String name;    private double money; // 月薪    public Topperformer() {    }    public Topperformer(String name, double money) { this.name = name; this.money = money;    }    public String getName() { return name;    }    public void setName(String name) { this.name = name;    }    public double getMoney() { return money;    }    public void setMoney(double money) { this.money = money;    }    @Override    public String toString() { return "Topperformer{" +  "name='" + name + '\'' +  ", money=" + money +  '}';    }}

主要代码

public class StreamDemo3 { public static double allMoney ; public static double allMoney2 ; // 2个部门去掉最高工资,最低工资的总和 public static void main(String[] args) {     List<Employee> one = new ArrayList<>();//开发部一     one.add(new Employee("张三",'男',30000 , 25000, null));     one.add(new Employee("李四",'男',25000 , 1000, "顶撞上司"));     one.add(new Employee("王五",'男',20000 , 20000, null));     one.add(new Employee("赵六",'男',20000 , 25000, null));     List<Employee> two = new ArrayList<>();//开发部二     two.add(new Employee("田七",'男',15000 , 9000, null));     two.add(new Employee("李逵",'男',20000 , 10000, null));     two.add(new Employee("西门庆",'男',50000 , 100000, "被打"));     two.add(new Employee("潘金莲",'女',3500 , 1000, "被打"));     two.add(new Employee("武大郎",'女',20000 , 0, "下毒"));     //开发一部员工最高     Topperformer t=one.stream().max((e1, e2) -> Double.compare(e1.getSalary() + e1.getBonus(),  e2.getSalary() + e2.getBonus())).map(e->new Topperformer(e.getName(),e.getSalary()+e.getBonus())).get();     System.out.println(t);     //开发二部员工最高     Topperformer t1=two.stream().max((e1, e2) -> Double.compare(e1.getSalary() + e1.getBonus(),  e2.getSalary() + e2.getBonus()))      .map(e->new Topperformer(e.getName(),e.getSalary()+e.getBonus())).get();     System.out.println(t1);     // 2、统计平均工资,去掉最高工资和最低工资     one.stream().sorted((e1, e2) -> Double.compare(e1.getSalary() + e1.getBonus(),  e2.getSalary() + e2.getBonus()))      .skip(1).limit(one.size() - 2).forEach(e -> {   // 求出总和:剩余员工的工资总和   allMoney += (e.getSalary() + e.getBonus());      });     System.out.println("开发一部的平均工资是:" + allMoney / (one.size() - 2));     //开发二部的平均工资     two.stream().sorted((e1, e2) -> Double.compare(e1.getSalary() + e1.getBonus(),  e2.getSalary() + e2.getBonus()))      .skip(1).limit(two.size() - 2).forEach(e -> {   // 求出总和:剩余员工的工资总和   allMoney += (e.getSalary() + e.getBonus());      });     System.out.println("开发二部的平均工资是:" + allMoney / (two.size() - 2));     // 3、合并2个集合流,再统计     Stream<Employee> s1 = one.stream();     Stream<Employee> s2 = two.stream();     Stream<Employee> s3 = Stream.concat(s1 , s2);     s3.sorted((e1, e2) -> Double.compare(e1.getSalary() + e1.getBonus(),  e2.getSalary() + e2.getBonus()))      .skip(1).limit(one.size() + two.size() - 2).forEach(e -> {   // 求出总和:剩余员工的工资总和   allMoney2 += (e.getSalary() + e.getBonus());      });     // BigDecimal     BigDecimal a = BigDecimal.valueOf(allMoney2);     BigDecimal b = BigDecimal.valueOf(one.size()  + two.size() - 2);     System.out.println("开发部的平均工资是:" + a.divide(b,2, RoundingMode.HALF_UP));    }}

运行截图
在这里插入图片描述

收集Stream流

public class StreamDemo05 {    public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("张无忌"); list.add("周芷若"); list.add("赵敏"); list.add("张强"); list.add("张三丰"); list.add("张三丰"); Stream<String> s1 = list.stream().filter(s -> s.startsWith("张")); List<String> zhangList = s1.collect(Collectors.toList()); // 可变集合 zhangList.add("java1"); System.out.println(zhangList); Stream<String> s2 = list.stream().filter(s -> s.startsWith("张")); Set<String> zhangSet = s2.collect(Collectors.toSet()); System.out.println(zhangSet); Stream<String> s3 = list.stream().filter(s -> s.startsWith("张")); String[] arrs = s3.toArray(String[]::new); System.out.println("Arrays数组内容:" + Arrays.toString(arrs));    }}

截图在这里插入图片描述