I have a List of Users, who have a name and an age. I would like to split this list into sublists, where each list contains all the users with a particular age.
Here's some code for an example:
public class User{ public String name; public int age; } User user1 = new User(); user1.setName("Tom"); user1.setAge("20"); User user2 = new User(); user2.setName("Jack"); user2.setAge("21"); User user3 = new User(); user3.setName("Peter"); user3.setAge("21"); List<User> userList = new ArrayList<User>(); userList.add(user1); userList.add(user2); userList.add(user3); How can I can split userList so List1 (holding users age 20) contains Tom and List2(holding users age 21) contains Jack and Peter?