Skip to content

Commit a346df8

Browse files
update stream
1 parent 9446da7 commit a346df8

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/FDynamic/Stream/StreamIntermediateAPI.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,40 @@ public static void main(String[] args) {
1515
);
1616
filter(personList);
1717
distinct(personList);
18+
map(personList);
1819
}
1920

21+
//过滤出符合条件,person的age>18的人
2022
public static void filter(List<Person> personList){
2123
List<Person> list=personList.stream()
2224
.filter(person->person.getAge()>18)
2325
.collect(Collectors.toList());
2426
printList("filter:", list);
2527
}
2628

29+
//过滤不同的人
2730
public static void distinct(List<Person> personList){
2831
List<Person> list=personList.stream()
2932
.distinct()
3033
.collect(Collectors.toList());
3134
printList("distinct:", list);
3235
}
3336

37+
// 使用map,将List<Person>转换为person的名字List<String>
38+
public static void map(List<Person> personList){
39+
List<String> list=personList.stream()
40+
.map(Person::getName)
41+
.collect(Collectors.toList());
42+
System.out.println("map: "+list);
43+
}
44+
45+
// 打印personList中的内容
3446
public static void printList(String flag, List<Person> personList){
3547
System.out.println(flag);
3648
for(Person person:personList){
3749
System.out.println(person);
3850
}
51+
System.out.println();
3952
}
4053

4154
}

src/FDynamic/Stream/StreamTerminalAPI.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public static void main(String[] args) {
1616
max(personList);
1717
}
1818

19+
//找到年龄最大的人
1920
public static void max(List<Person> personList){
2021
Person person=personList.stream()
2122
.max(Comparator.comparing(Person::getAge))

0 commit comments

Comments
 (0)