-1

I have a generic Object, let's say it's user:

@Data
class User {
    public String userName;
    public int age;
}

I create a bunch of them:

   User user = new User("Freddy", 22);
   User user = new User("Freddy", 18);
   User user = new User("Hans", 21);
   User user = new User("Michael", 5);
   User user = new User("Freddy", 29);
   User user = new User("Hans", 33);
   User user = new User("Michael", 20);
   User user = new User("Freddy", 15);

These are stored in let's say an ArrayList(assume they are all there)

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

Now I wish to make three lists:

 List<User> freddyList;
 List<User> hansList;
 List<User> michaelList;

I want to loop through userList and as you can predict, add the user to freddyList if userName equals "Freddy", and so forth.

How do I solve this with forEach?

usersList.foreach(
    if (usersList.getUsername().equals("Freddy")){
        freddyList.add(usersList.Object) // This doesn't work
    }
)
cbll
  • 6,499
  • 26
  • 74
  • 117
  • Maybe look at [What is the best way to filter a Java Collection?](https://stackoverflow.com/questions/122105/what-is-the-best-way-to-filter-a-java-collection) (which is a possible duplicate) and [Java 8 Streams Filter examples](https://www.mkyong.com/java8/java-8-streams-filter-examples/) – MadProgrammer Feb 07 '18 at 21:00
  • `forEach` takes a lambda expression as input – NiVeR Feb 07 '18 at 21:01

5 Answers5

2

Lambda expressions should be short and clean. With forEach you will end up using multi expression lambda that would look ugly.

My suggestion is to use use Java stream groupingBy:

Map< String, List<User> > map = userList.stream().collect( Collectors.groupingBy( u -> u.getUserName() ) );

List<User> freddyList = map.get("Freddy");
List<User> hansList = map.get("Hans");
List<User> michaelList = map.get("Michael");
tsolakp
  • 5,858
  • 1
  • 22
  • 28
1

Adding to the comment, your code should look something like this:

usersList.forEach( user ->
    if (user.getUsername().equals("Freddy")){
        freddyList.add(user);
    }
)
NiVeR
  • 9,644
  • 4
  • 30
  • 35
1

You need to consume the user object from forEach (look here) and add it to the respective list as shown below:

List<User> userList = new ArrayList<>();
//Add your users here
//Declare your freddyList, hansList, etc..
userList.forEach(user -> {
    if (user.getUsername().equals("Freddy")){
        freddyList.add(user);
    } else if(user.getUsername().equals("Hans")) {
        hansList.add(user);
    } else if(user.getUsername().equals("Michael")) {
        michaelList.add(user);
    }
});

But, I suggest you can directly get the list of users by grouping by userName using Java streams (Collectors.groupingBy) as shown below in a single line:

Map<String, List<User>> groupByUser =
    userList.stream().collect(Collectors.groupingBy(User::getUserName));
Vasu
  • 21,832
  • 11
  • 51
  • 67
0

You can do a for each user if the value of the current user is equal to either one of these do that bit of code.

Example:

for(User user : userList){
if (user.getUsername().equals("Freddy"))
{
    freddyList.add(user);
}
else if(user.getUsername().equals("Hans"))
{
    hansList.add(user);
}
else
{
   michaelList.add(user);
}

You can change it to a switch if you like, use another method, but that is the best logic you need to get it working.

Also make sure your object class User has the getUsername() method defined so you can use it in your code.

arahman
  • 585
  • 2
  • 9
0

You seems to misunderstand some things :

  • usersList.getUsername() does not exists, you cannot get a username from a list
  • usersList.Object does not belong to anything too

You'd better use filter() :

List<User> freddyList = usersList.stream().filter(u -> u.getUserName().equals("Freddy"))
                                          .collect(Collectors.toList());

List<User> hansList = usersList.stream().filter(u -> u.getUserName().equals("Hans"))
                                          .collect(Collectors.toList());

List<User> michaelList = usersList.stream().filter(u -> u.getUserName().equals("Michael"))
                                          .collect(Collectors.toList());

These will iterate over the list which contains all the guys, keep the ones that have the good username, and collect them into the good list

azro
  • 53,056
  • 7
  • 34
  • 70