您好,登錄后才能下訂單哦!
小編給大家分享一下Java8怎樣通過(guò)Lambda處理List集合,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
Java 8新增的Lambda表達(dá)式,我們可以用簡(jiǎn)潔高效的代碼來(lái)處理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()); }); }
運(yùn)行結(jié)果:
2、list轉(zhuǎn)為Map
public static void main(String[] args) { List<User> userList = Lists.newArrayList();//存放user對(duì)象集合 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,轉(zhuǎn)為Map Map<Long,User> userMap = userList.stream().collect(Collectors.toMap(User::getId, a -> a,(k1, k2)->k1)); System.out.println(userMap); }
運(yùn)行結(jié)果:
3、將List分組:List里面的對(duì)象元素,以某個(gè)屬性來(lái)分組
public static void main(String[] args) { List<User> userList = Lists.newArrayList();//存放user對(duì)象集合 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); //根據(jù)name來(lái)將userList分組 Map<String, List<User>> groupBy = userList.stream().collect(Collectors.groupingBy(User::getName)); System.out.println(groupBy); }
運(yùn)行結(jié)果:
4、過(guò)濾:從集合中過(guò)濾出來(lái)符合條件的元素
public static void main(String[] args) { List<User> userList = Lists.newArrayList();//存放user對(duì)象集合 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()); }); }
運(yùn)行結(jié)果:
5、求和:將集合中的數(shù)據(jù)按照某個(gè)屬性求和
public static void main(String[] args) { List<User> userList = Lists.newArrayList();//存放user對(duì)象集合 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); }
運(yùn)行結(jié)果:
6、從List轉(zhuǎn)為Map,key與value 一 一對(duì)應(yīng)
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)); }
運(yùn)行結(jié)果:
看完了這篇文章,相信你對(duì)“Java8怎樣通過(guò)Lambda處理List集合”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。