关于Java8:StreamAPI的一点记录
关于 Stream ,Functional Interface 的一点记录
stream对于集合操作的便捷度提升:
1 import java.util.ArrayList; 2 import java.util.List; 3 import java.util.Map; 4 import java.util.stream.Collectors; 5 6 public class ActiveMac { 7 8 public static void main(String[] args) { 9 List<Human> humans = new ArrayList<>(); 10 humans.add(new Human("Daniel", 23, "Google")); 11 humans.add(new Human("Max", 33, "Microsoft")); 12 humans.add(new Human("Jenny", 18, "Google")); 13 humans.add(new Human("Alex", 28, "Facebook")); 14 humans.add(new Human("Charles", 34, "Twitter")); 15 humans.add(new Human("Roy", 31, "Microsoft")); 16 17 // 对集合内符合条件的计数 18 long nums = humans.stream().filter(human -> (human.getAge() > 20)).count(); 19 System.out.println("nums:" + nums); 20 21 // 对集合内符合条件的筛选输出 22 List<Human> nameContE = humans.stream().filter(human -> human.getName().contains("e")).collect(Collectors.toList()); 23 System.out.println("nameContainsE:" + nameContE.toString()); 24 25 // 对集合内元素中元素进行操作 26 List<Integer> doubleAge = humans.stream().map(human -> human.getAge() * 2).collect(Collectors.toList()); 27 System.out.println("doubleAge:" + doubleAge.toString()); 28 29 // 对集合内元素分组 30 Map<String, List<Human>> group = humans.stream().collect(Collectors.groupingBy(Human::getCompany)); 31 System.out.println(group.toString()); 32 } 33 } 34 35 class Human { 36 private String name; 37 private Integer age; 38 private String company; 39 40 public Human(String name, Integer age, String company) { 41 super(); 42 this.name = name; 43 this.age = age; 44 this.company = company; 45 } 46 47 public String getName() { 48 return name; 49 } 50 51 public void setName(String name) { 52 this.name = name; 53 } 54 55 public Integer getAge() { 56 return age; 57 } 58 59 public void setAge(Integer age) { 60 this.age = age; 61 } 62 63 public String getCompany() { 64 return company; 65 } 66 67 public void setCompany(String company) { 68 this.company = company; 69 } 70 71 @Override 72 public String toString() { 73 return name + "-" + age + "-" + company; 74 } 75 }
新旧方法的对比:
// 1.对集合内符合条件的计数 long nums = humans.stream().filter(human -> (human.getAge() > 20)).count(); System.out.println(nums); // 2.对集合内符合条件的筛选输出 List<Human> nameContE = humans.stream().filter(human -> human.getName().contains("e")).collect(Collectors.toList()); System.out.println(nameContE.toString()); // 3.对集合内元素中元素进行操作 List<Integer> doubleAge = humans.stream().map(human -> human.getAge() * 2).collect(Collectors.toList()); System.out.println(doubleAge.toString()); // 4.对集合内元素分组 Map<String, List<Human>> group = humans.stream().collect(Collectors.groupingBy(Human::getCompany)); System.out.println(group.toString()); // 1 int num = 0; for (Human h : humans) { if (h.getAge() > 20) { num++; } } System.out.println(num); //旧方法-循环 // 2 List<Human> eResult = new ArrayList<>(); for (Human h : humans) { if (h.getName().contains("e")) { eResult.add(h); } } System.out.println(eResult.toString()); // 3 List<Integer> dounleA = new ArrayList<>(); for (Human h : humans) { Integer newAge = h.getAge() * 2; dounleA.add(newAge); } System.out.println(dounleA.toString()); // 4 Map<String, List<Human>> maps = new HashMap<>(); for (Human h : humans) { List<Human> hs = new ArrayList<>(); String key = h.getCompany(); if (maps.containsKey(key)) { hs = maps.get(key); } hs.add(h); maps.put(key, hs); } System.out.println(maps.toString()); }
输出结果方面并不存在差异
新方法-stream
5 [Daniel-23-Google, Jenny-18-Google, Alex-28-Facebook, Charles-34-Twitter] [46, 66, 36, 56, 68, 62] {Google=[Daniel-23-Google, Jenny-18-Google], Twitter=[Charles-34-Twitter], Microsoft=[Max-33-Microsoft, Roy-31-Microsoft], Facebook=[Alex-28-Facebook]}
旧方法-循环
5 [Daniel-23-Google, Jenny-18-Google, Alex-28-Facebook, Charles-34-Twitter] [46, 66, 36, 56, 68, 62] {Google=[Daniel-23-Google, Jenny-18-Google], Twitter=[Charles-34-Twitter], Microsoft=[Max-33-Microsoft, Roy-31-Microsoft], Facebook=[Alex-28-Facebook]}
其他的下次记录
一介书生,敲敲键盘而已。