一、抽取多個對象中的某個屬性,用數組接收數組
List<Student> students = new ArrayList<>();對象
List<String> names =students.stream().map(Student::getName).collect(Collectors.toList());get
二、抽取多個對象中的兩個屬性,key,value,用map接收stream
List<Student> students = new ArrayList<>();List
Map<Long, String> map = students .stream().collect(Collectors.toMap(Student::getId, Student::getName));model
三、過濾多個對象中須要的對象,把name不是張三的對象過濾掉map
List<Student> students = new ArrayList<>();sort
List<Student> models = students .stream().filter(a -> !a.getName().equals(「張三」)).collect(Collectors.toList());filter
四、假若有一個場景,{(1,香蕉),(1,蘋果),(1,橘子)},有三個對象,1表明一我的的id,我想把這我的想吃的水果合併起來,最終返回一個map,想要的結果是:{1=香蕉蘋果橘子}集合
Map<Long, String> map = new HashMap<>();
map.put(1,"香蕉蘋果");
//若是key存在,x是存在的value,y是當前的value,x+y會覆蓋存在的key的value
map.merge(1, "橘子", (x, y) -> x + y);
五、過濾掉多個對象中,有屬性重複的一項,好比說一個數組中有不少對象,我想把手機號重複屬性的對象只保留一個
List<Student> list= new ArrayList<>();
Set<Student> set = new TreeSet<>(Comparator.comparing(Student::getPhone));
set.addAll(list);
List<Student> list = new ArrayList<>(set);
六、根據集合裏面的數字升序
List<Student> list= new ArrayList<>();
List<Student> list = list.stream().sorted(Comparator.comparing(Student::getId)).collect(Collectors.toList());