1.8新特性(全)文章地址:https://blog.csdn.net/qq_29411737/article/details/80835658

 1 package reflect;
 2 
 3 /**
 4  * @author fkl
 5  * @date 2020/12/22 14:15
 6  **/
 7 
 8 public class User {
 9 
10     private int id;
11     private String name;
12     private int age;
13     private String address;
14 
15     public int getId() {
16         return id;
17     }
18 
19     public void setId(int id) {
20         this.id = id;
21     }
22 
23     public String getName() {
24         return name;
25     }
26 
27     public void setName(String name) {
28         this.name = name;
29     }
30 
31     public int getAge() {
32         return age;
33     }
34 
35     public void setAge(int age) {
36         this.age = age;
37     }
38 
39     public String getAddress() {
40         return address;
41     }
42 
43     public void setAddress(String address) {
44         this.address = address;
45     }
46 
47     public User(int id, String name, int age, String address){
48         this.id = id;
49         this.name = name;
50         this.age = age;
51         this.address = address;
52     }
53 }
 1 package reflect;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import java.util.stream.Collectors;
 6 
 7 /**
 8  * @author fkl
 9  * @date 2020/12/22 14:27
10  **/
11 public class UserTest {
12     public static void main(String[] args) {
13 
14         List<User> userList = new ArrayList<>();
15 
16         User u1 = new User(1, "u1", 23, "特使1");
17         User u2 = new User(2, "u2", 24, "特使2");
18         User u3 = new User(3, "u3", 25, "特使3");
19         User u4 = new User(4, "u4", 26, "特使4");
20         User u5 = new User(5, "u5", 22, "特使5");
21         User u6 = new User(6, "u6", 23, "特使6");
22 
23         userList.add(u1);
24         userList.add(u2);
25         userList.add(u3);
26         userList.add(u4);
27         userList.add(u5);
28         userList.add(u6);
29 
30         /**使用lambda表达式截取List中的某一个属性的值*/
31         System.out.println("使用lambda表达式截取List中的某一个属性的值");
32         List<Integer> ids = userList.stream().map(e -> e.getId()).collect(Collectors.toList());
33         System.out.println("取出某一属性的结果值为:" + ids);
34 
35 
36         System.out.println("《《《《《《《《《《》》》》》》》》");
37 
38         System.out.println("使用lambda表达式遍历循环List");
39         userList.stream().forEach(user -> {
40             System.out.println("user值:"+user.getId()+"," + user.getName()+","+user.getAge()+","+user.getAddress());
41         });
42 
43         System.out.println("《《《《《《《《《《》》》》》》》》");
44 /*
45         System.out.println("使用lambda表达式过滤某一个属性的值");
46         System.out.println("过滤前userList的值:" + userList);
47         userList = userList.stream().filter(s -> s.getAge() > 24).collect(Collectors.toList());
48         System.out.println("过滤后userList的值:"+userList);*/
49     }
50 }

 

posted on 2020-12-22 14:53  没入门就放弃  阅读(97)  评论(0编辑  收藏  举报