一、创建 Stream
1.1、创建 Stream 流
1.1.1、List 集合获取 Stream 流
Collection<String> list = new ArrayList<>();
Stream<String> s1 = list.stream();
1.1.2、Map 集合获取 stream 流
Map<String, Integer> map = new HashMap<>();
// 键流
Stream<String> s2 = map.keySet().stream();
// 值流
Stream<Integer> s3 = map.values().stream();
// 键值对
Stream<Map.Entry<String, Integer>> s4 = map.entrySet().stream();
1.1.3、数组获取 Stream 流
String[] strs = {"A", "B", "C"};
Stream<String> s5 = Arrays.stream(strs);
Stream<String> s6 = Stream.of(strs);
1.2、方法分类
1.2.1、中间方法
1.2.2、终结方法
1.2.3、收集方法
二、代码操作
public class MyUser {private Integer id;private String name;private Integer score;private List<String> menus;public MyUser() {}public MyUser(Integer id, String name, Integer score, List<String> menus) {this.id = id;this.name = name;this.score = score;this.menus = menus;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getScore() {return score;}public void setScore(Integer score) {this.score = score;}public List<String> getMenus() {return menus;}public void setMenus(List<String> menus) {this.menus = menus;}@Overridepublic String toString() {return "Merchant{" +"id=" + id +", name='" + name + '\'' +", score=" + score +", menus=" + menus +'}';}
}
public static void main(String[]args){MyUser myUser1 = new MyUser(1,"AA",11, Arrays.asList("A","B","C"));MyUser myUser2 = new MyUser(2,"BB",22, Arrays.asList("D","E","F"));MyUser myUser3 = new MyUser(3,"CC",19, Arrays.asList("O","P","H"));MyUser myUser4 = new MyUser(4,"DD",33, Arrays.asList("K","J","I"));MyUser myUser5 = new MyUser(5,"EE",39, Arrays.asList("X","M","N"));List<MyUser> list = Arrays.asList(myUser1,myUser2,myUser3,myUser4,myUser5);// 遍历plist.stream().forEach(s -> System.out.println(s));// 取最大值、最小值Comparator<? super MyUser> comparator = Comparator.comparing(MyUser::getScore);Optional<MyUser> max = list.stream().max(comparator);Optional<MyUser> min = list.stream().min(comparator);System.out.println("max: "+max);System.out.println("min: "+min);// 单个属性加减乘除,生成新集合。List<MyUser> collect1 = list.stream().map(x -> {x.setScore(x.getScore()+100);return x;}).collect(Collectors.toList());System.out.println("collect1: "+collect1);// 过滤元素 对原集合的成员的属性修改list.stream().filter(x->x.getId()>2).forEach(x -> {x.setName(x.getName()+100);});System.out.println("list22: "+list);// 统计//统计员工人数Long count = list.stream().collect(Collectors.counting());//求平均工资Double average = list.stream().collect(Collectors.averagingDouble(MyUser::getScore));//求最高工资Optional<Integer> max2 = list.stream().map(MyUser::getScore).collect(Collectors.maxBy(Integer::compare));//求工资之和Integer sum = list.stream().collect(Collectors.summingInt(MyUser::getScore));//一次性统计所有信息DoubleSummaryStatistics collect = list.stream().collect(Collectors.summarizingDouble(MyUser::getScore));System.out.println("统计员工人数:"+count);System.out.println("求平均工资:"+average);System.out.println("求最高工资:"+max2);System.out.println("求工资之和:"+sum);System.out.println("一次性统计所有信息:"+collect);// 过滤后 转为 MapMap map =list.stream().filter(s->s.getId()>3).collect(Collectors.toMap(s->s.getId(),y->y));System.out.println("map:"+map);}