File tree Expand file tree Collapse file tree 2 files changed +14
-0
lines changed
Expand file tree Collapse file tree 2 files changed +14
-0
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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 ))
You can’t perform that action at this time.
0 commit comments