List 类的Stream处理:
1List<String> list = new ArrayList<String>(); 2 list.add("djk"); 3 list.add("djk1"); 4 list.add("djk12"); 5// map 6list = list.stream().map(item->item.toUpperCase()).collect(Collectors.toList()); 7 8// filter 9list = list.stream().filter(item->!item.equals("djk")).collect(Collectors.toList()); 10 11//flatMap 12List<String> list = new ArrayList<>(); 13list.add("aaa bbb ccc"); 14list.add("ddd eee fff"); 15list.add("ggg hhh iii"); 16 17list = list.stream().map(s -> s.split(" ")).flatMap(Arrays::stream).collect(toList()); 18 19 20class Apple { 21 private Integer id; 22 private String name; 23 private BigDecimal money; 24 private Integer num; 25 public Apple(Integer id, String name, BigDecimal money, Integer num) { 26 this.id = id; 27 this.name = name; 28 this.money = money; 29 this.num = num; 30 } 31} 32 33 34List<Apple> appleList = new ArrayList<>();//存放apple对象集合 35 36Apple apple1 = new Apple(1,"苹果1",new BigDecimal("3.25"),10); 37Apple apple12 = new Apple(1,"苹果2",new BigDecimal("1.35"),20); 38Apple apple2 = new Apple(2,"香蕉",new BigDecimal("2.89"),30); 39Apple apple3 = new Apple(3,"荔枝",new BigDecimal("9.99"),40); 40 41 42// group by用法 43Map<Integer, List<Apple>> groupBy = appleList.stream().collect(Collectors.groupingBy(Apple::getId)); 44 45// 根据id去重 46List<Person> unique = appleList.stream().collect( 47 collectingAndThen( 48 toCollection(() -> new TreeSet<>(comparingLong(Apple::getId))), ArrayList::new) 49 ); 50 51 52// List转Map 53// (k1,k2)->k1 表示当k1,k2 2个key冲突的时候,选择k1 54Map<Integer, Apple> appleMap = appleList.stream().collect(Collectors.toMap(Apple::getId, a -> a,(k1,k2)->k1)); 55 56 57//计算 总金额 58BigDecimal totalMoney = appleList.stream().map(Apple::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add); 59System.err.println("totalMoney:"+totalMoney); //totalMoney:17.48 60 61 62List<Student> list = Lists.newArrayList(); 63list.add(new Student("测试", "男", 18)); 64 65//按性别进行分组统计人数 66Map<String, Integer> map = list.stream().collect(Collectors.groupingBy(Student::getSex, Collectors.summingInt(p -> 1))); 67 68//判断是否有年龄大于25岁的学生 69boolean check = list.stream().anyMatch(student -> student.getAge() > 25); 70 71 //按照年龄从小到大排序 72List<Student> l3 = list.stream().sorted((s1, s2) -> s1.getAge().compareTo(s2.getAge())).collect(toList()); 73 74//求年龄最小的两个学生 75List<Student> l4 = list.stream().limit(2).collect(toList()); 76 77list.removeif(e -> e.getAge()==25); 78 79list.sort((o1, o2) -> o1.getAge() - o2.getAge()); 80 81 82 83 84 85 86 87
java8 Map的新方法
1// map merge() 2class Students{ 3 String name; 4 String subject 5 int score; 6} 7 8Map<String, Integer> studentScoreMap2 = new HashMap<>(); 9studentScoreList.forEach(studentScore -> studentScoreMap2.merge( 10 studentScore.getStuName(), 11 studentScore.getScore(), 12 Integer::sum)); 13 14System.out.println(objectMapper.writeValueAsString(studentScoreMap2)); 15 16// 结果如下: 17// {"李四":228,"张三":215,"王五":235} 18 19// key = 10 對應的value不存在, 則用101010 覆蓋 20Map<Integer, String> map = new HashMap(); 21String s1 = map.putIfAbsent(10, "101010"); 22