user1365697
user1365697

Reputation: 5989

Is it possible to get two result and join them with stream in JAVA8?

I want to get the name and the and the URL from Remote

public interface Remote {
    String getName();

    Remote withName(String name);

    String getUrl();

    Remote withUrl(String url);
}

   List<String> remoteNames = remoteList.stream().map(Remote::getName).filter(name -> name.equalsIgnoreCase(origin)).collect(Collectors.toList());

   List<String> remoteUrls = remoteList.stream().map(Remote::GetUrl).filter(url-> url.equalsIgnoreCase(origin)).collect(Collectors.toList());

Is it possible to do it in one call and to get all the result in one variable ?

Upvotes: 0

Views: 82

Answers (1)

OldCurmudgeon
OldCurmudgeon

Reputation: 65811

Create a new class that hold both fields and create it at map time:

List<NameAndURL> remotes = remoteList.stream().map(t -> new NameAndURL(t.getName(), t.getURL()))...

Alternately you can zip the two streams together.

Upvotes: 1

Related Questions