您好,登錄后才能下訂單哦!
小編給大家分享一下Java8怎樣通過Lambda處理List集合,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
Java 8新增的Lambda表達式,我們可以用簡潔高效的代碼來處理List。
1、遍歷
public static void main(String[] args) { List<User> userList = Lists.newArrayList(); User user1 = new User(1L, "張三", 24); User user2 = new User(2L, "李四", 27); User user3 = new User(3L, "王五", 21); userList.add(user1); userList.add(user2); userList.add(user3); userList.stream().forEach(user ->{ System.out.println(user.getName()); }); }
運行結果:
2、list轉為Map
public static void main(String[] args) { List<User> userList = Lists.newArrayList();//存放user對象集合 User user1 = new User(1L, "張三", 24); User user2 = new User(2L, "李四", 27); User user3 = new User(3L, "王五", 21); userList.add(user1); userList.add(user2); userList.add(user3); //ID為key,轉為Map Map<Long,User> userMap = userList.stream().collect(Collectors.toMap(User::getId, a -> a,(k1, k2)->k1)); System.out.println(userMap); }
運行結果:
3、將List分組:List里面的對象元素,以某個屬性來分組
public static void main(String[] args) { List<User> userList = Lists.newArrayList();//存放user對象集合 User user1 = new User(1L, "張三", 24); User user2 = new User(2L, "李四", 27); User user3 = new User(3L, "王五", 21); User user4 = new User(4L, "張三", 22); User user5 = new User(5L, "李四", 20); User user6 = new User(6L, "王五", 28); userList.add(user1); userList.add(user2); userList.add(user3); userList.add(user4); userList.add(user5); userList.add(user6); //根據name來將userList分組 Map<String, List<User>> groupBy = userList.stream().collect(Collectors.groupingBy(User::getName)); System.out.println(groupBy); }
運行結果:
4、過濾:從集合中過濾出來符合條件的元素
public static void main(String[] args) { List<User> userList = Lists.newArrayList();//存放user對象集合 User user1 = new User(1L, "張三", 24); User user2 = new User(2L, "李四", 27); User user3 = new User(3L, "王五", 21); User user4 = new User(4L, "張三", 22); User user5 = new User(5L, "李四", 20); User user6 = new User(6L, "王五", 28); userList.add(user1); userList.add(user2); userList.add(user3); userList.add(user4); userList.add(user5); userList.add(user6); //取出名字為張三的用戶 List<User> filterList = userList.stream().filter(user -> user.getName().equals("張三")).collect(Collectors.toList()); filterList.stream().forEach(user ->{ System.out.println(user.getName()); }); }
運行結果:
5、求和:將集合中的數據按照某個屬性求和
public static void main(String[] args) { List<User> userList = Lists.newArrayList();//存放user對象集合 User user1 = new User(1L, "張三", 24); User user2 = new User(2L, "李四", 27); User user3 = new User(3L, "王五", 21); User user4 = new User(4L, "張三", 22); User user5 = new User(5L, "李四", 20); User user6 = new User(6L, "王五", 28); userList.add(user1); userList.add(user2); userList.add(user3); userList.add(user4); userList.add(user5); userList.add(user6); //取出名字為張三的用戶 int totalAge = userList.stream().mapToInt(User::getAge).sum(); System.out.println("和:" + totalAge); }
運行結果:
6、從List轉為Map,key與value 一 一對應
public static void main(String[] args) { List<User> userList = Lists.newArrayList(); User user1 = new User(1L, "張三", 24); User user2 = new User(2L, "李四", 27); User user3 = new User(3L, "王五", 21); User user4 = new User(4L, "張三", 22); User user5 = new User(5L, "李四", 20); User user6 = new User(6L, "王五", 28); userList.add(user1); userList.add(user2); userList.add(user3); userList.add(user4); userList.add(user5); userList.add(user6); Map<Long/*Id*/,User> userMap = userList.stream().collect(Collectors.toMap(User::getId, user -> user)); System.out.println("toMap:" + JSONArray.toJSONString(userMap)); }
運行結果:
看完了這篇文章,相信你對“Java8怎樣通過Lambda處理List集合”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。