S
S
Senior Devosaur2019-11-13 19:13:34
Java
Senior Devosaur, 2019-11-13 19:13:34

How to translate the following code into stream in Java 8?

Hello, I have the following code:

Set<Team> teams = new HashSet<>();
    
    for (Team team : scenario.getTeamsActive()) {
      for (UserAsLearner userAsLearner : learners) {
        if(team.getLearners().contains(userAsLearner)) {
          teams.add(team);
          break;
        }
      }
    }

help implement it using Streams and Lambda expressions.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sergey, 2019-11-14
kuzmin @sergueik

Senior Devosaur **totally **no time to do it for you here is a simple example to start with

List<String> OSes = Arrays
      .asList(new String[] { "windows", "dos", "mac", "linux" });

   Map<String, String> defaultBrowsers = new HashMap<>();
    defaultBrowsers.put("windows", "Chrome");
    defaultBrowsers.put("linux", "Firefox");
    defaultBrowsers.put("mac", "Safari");

    List<String> result = OSes.stream().filter(o -> {
      return (defaultBrowsers.containsKey(o)) ? true : false;
    }).collect(Collectors.toList());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question