概述
- 在Java8中,得益于lambda所带来的函数式编程,引入了一个全新的Stream流的概念
- 目的:用于简化集合和数组操作的api
案例
需求:创建一个集合存储多个字符串元素,将集合中所有以“z”开头的元素存储到新的集合中,再把这个新集合中长度为3的元素存储到另一个新集合中,遍历输出最新集合的元素
传统方式:
Stream流:
stream流就像一个流水线,先将所有元素放上去,再借助简化的api操作元素并得到最后想要的元素
Stream流的获取
Stream操作集合或者数组的第一步是先得到Stream流,然后才能使用流的功能
代码参考
java">//Collection集合获取流
Collection<String> list = new ArrayList<>();
Stream<String> s = list.stream();
//Map集合获取流
Map<String,Integer> maps = new HashMap<>();
//先获取键流
Stream<String> keyStream = maps.keySet().stream();
//在获取值流
Stream<Integer> valueStream = maps.values().stream();
//键值对流
Stream<Map.Entry<String,Integer>> keyAndValueStream = maps.entrySet().stream();
java">//数组获取流
String[] arr = {"gst","zcy","xhk"};
Stream<String> arrStream = Arrays.stream(arr);
Stream<String> arrStream2 = Stream.of(arr);
Stream流的常用API
代码参考
java">List<String> list = new ArrayList<>();list.add("gst");list.add("zcy");list.add("txw");list.add("xhk");list.add("zja");list.add("hb");list.add("zz");list.stream().filter(s -> s.startsWith("z")).forEach(s -> System.out.println(s));System.out.println("------------");long size = list.stream().filter(s -> s.length() == 3).count();System.out.println(size);System.out.println("------------");list.stream().filter(s -> s.startsWith("z")).limit(2).forEach(s -> System.out.println(s));//个数限制为2个
// list.stream().filter(s -> s.startsWith("z")).limit(2).forEach(System.out::println);//简化System.out.println("------------");list.stream().filter(s -> s.startsWith("z")).skip(2).forEach(System.out::println);//跳过前两个System.out.println("------------");//Map加工方法//给集合元素的前面都加上gstlist.stream().map(s -> "gst的"+s).forEach(System.out::println);System.out.println("------------");//把所有的名称都加工成学生对象list.stream().map(s -> new Student(s)).forEach(System.out::println);
// list.stream().map(Student::new).forEach(System.out::println);//简化System.out.println("------------");//合并流Stream<String> s1 = list.stream().filter(s -> s.startsWith("z"));Stream<String> s2 = Stream.of("Java1","Java2");Stream<String> s3 = Stream.concat(s1,s2);s3.forEach(System.out::println);//注意:这里两个流如果是不同类型的合并时要用Object类型接
Stream流的综合应用
代码参考
java">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;}@Overridepublic String toString() {return "Topperformer{" +"name='" + name + '\'' +", money=" + money +'}';}
}
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;}@Overridepublic String toString() {return "Employee{" +"name='" + name + '\'' +", sex=" + sex +", salary=" + salary +", bonus=" + bonus +", punish='" + punish + '\'' +'}';}
}
public class StreamDemo04 {public static double allMoney = 0;public static double allMoney2 = 0;public static double allMoney3 = 0;public static void main(String[] args) {List<Employee> one = new ArrayList<>();one.add(new Employee("gst",'女',30000,20000,null));one.add(new Employee("zcy",'男',35000,20000,"被打"));one.add(new Employee("txw",'女',30000,20000,"被打"));one.add(new Employee("xhk",'男',35000,20000,"被打"));List<Employee> two = new ArrayList<>();two.add(new Employee("ggg",'女',35000,15000,null));two.add(new Employee("zzz",'男',30000,20000,"被打"));two.add(new Employee("ttt",'女',20000,10000,null));two.add(new Employee("xxx",'男',30000,20000,"被打"));two.add(new Employee("qqq",'男',40000,20000,"被打"));//1.一部门的最高工资//制定大小规则
// Employee e = one.stream().max((e1,e2) ->Double.compare(e1.getSalary()+e1.getBonus(),e2.getSalary()+e2.getBonus()))
// .get();
// System.out.println(e);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 t2 = 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(t2);//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 -> {//求出总和allMoney2 += (e.getSalary()+e.getBonus());});System.out.println("开发一部的平均工资:"+allMoney2/(one.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 -> {//求出总和allMoney3 += (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流
代码参考
java">List<String> list = new ArrayList<>();list.add("gst");list.add("zcy");list.add("txw");list.add("xhk");list.add("zja");list.add("hb");list.add("zz");Stream<String> s1 = list.stream().filter(s -> s.startsWith("z"));List<String> zlist = s1.collect(Collectors.toList());System.out.println(zlist);
// //注意:流只能使用一次,上述以及将流里的数据收集走了就等于没有了
// Set<String> zset = s1.collect(Collectors.toSet());
// System.out.println(zset);//只能再重新获取
// Stream<String> s2 = list.stream().filter(s -> s.startsWith("z"));
// Set<String> zset = s2.collect(Collectors.toSet());
// System.out.println(zset);Stream<String> s3 = list.stream().filter(s -> s.startsWith("z"));Object[] arrs = s3.toArray();System.out.println(Arrays.toString(arrs));