您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關java8怎么通過Lambda處理List集合的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
這篇文章主要介紹了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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。