//Make an array list of custom objects

List<User> userList = new ArrayList<User>();

userList.add(new User("ani","bha","[email protected]","Monday"));

userList.add(new User("ani2","bha2","[email protected]","Tuesday"));

userList.add(new User("ani5","bha5","[email protected]","Monday"));

userList.add(new User("ani2","bha2","[email protected]","Tuesday"));

userList.add(new User("ani5","bha5","[email protected]","Monday"));


我想将userList分为两个子列表。包含“ Monday”的用户进入一个子列表,将包含“ Tuesday”的用户进入另一个子列表。

最佳答案

您可以使用Collectors.groupingBy()根据作为参数传递的函数对流的元素进行分组,并将其存储在Map中,其中键是函数的结果,而值是关联的List

 Map<String, List<User>> collect = userList.stream()
        .collect(Collectors.groupingBy(User::getDay));

关于java - 将列表分为多个子列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48400511/

10-10 02:40