4

How to use multiple condtions in map function of stream ? I'm new to Java streams actually I want to use multiple condtions in a stream map something like:

List<String> cs = Arrays.asList("agent", "manager", "admin");

List<String> replace = cs.stream()
.map(p -> p.equals("agent") ? "manager" : p || p.equals("manager") ? "agent" : p )
.collect(Collectors.toList());

What I want is to replace agent with manager and manager with agent. That's if in a list agent exist replace it with manager and if manager exist replace it with agent.

3
  • 1
    And your question is? Also: Strings are not compared using == but using .equals. Commented Oct 31, 2018 at 10:40
  • why is used the map instead the filter? Commented Oct 31, 2018 at 10:41
  • 1
    @HadiJ because map is exactly what you want here, how is filter going to help? Commented Oct 31, 2018 at 10:43

6 Answers 6

5

You may do it like so,

List<String> interchanged = cs.stream()
    .map(s -> s.equals("manager") ? "agent" : s.equals("agent") ? "manager" : s)
    .collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

Comments

2

Another way to do that using List.replaceAll could be:

List<String> cs = Arrays.asList("agent", "manager", "admin");
cs.replaceAll(s -> {
    if (s.equals("manager")) {
        return "agent";
    }
    if (s.equals("agent")) {
        return "manager";
    }
    return s;
});

Comments

2

The other answers show how to deal with 2 options to replace elements. Here's a more general approach:

Map<String, String> replacements = Map.of("agent", "manager", "manager", "agent");

List<String> replace = cs.stream()
    .map(p -> replacements.getOrDefault(p, p))
    .collect(Collectors.toList());

If you have more words to be replaced, simply add them to the replacements map.

Comments

1
List<String> replace = cs.stream()
                .map(p -> p.equals("agent") ? "manager" : p.equals("manager") ? "agent" : p )
                .collect(Collectors.toList());

This will help you in this case but if you need more conditions use body style smth like this

map(p -> {...})

for creating readable code.

1 Comment

0

For readability you can check this

List<String> cs = Arrays.asList("agent", "manager", "admin");

List<String> replace = cs.stream()
.map(p -> { 
  if(p.equals("agent"))
   p = "manager"; 
  else if(p.equals("manager"))
   p = "agent; 

  return p;
})
.collect(Collectors.toList());

1 Comment

Maybe opinion based, but for some reasons I find the code in Ravindra's answer more readable.
0

Another way would be like this:

cs.replaceAll(s->s.replaceAll("agent|manager",replace(s)));



String replace(String s){
    return s.equals("manager")?"agent" :s.equals("agent")?"manager": s;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.