This question already has answers here:
What is the best way to combine two lists into a map (Java)?
(18 answers)
Closed 5 years ago.
I have the following:
List<String> keys
List<String> values
I would like map these two lists to a Map<String, String> using Java 8 Streams. The lists have both the same size and are sorted the same way.
I tried to map these two with the following
Map<String, String> result= keys.stream().
collect(Collectors.toMap(keys::get, values::get));
But this doesnt work at all - how can I do this correclty?
Thanks in advance :)
You can iterate over the indices of the Lists with an IntStream:
Map<String, String> result =
IntStream.range(0,keys.size())
.boxed()
.collect(Collectors.toMap(i -> keys.get(i), i -> values.get(i)));
Related
This question already has answers here:
Java 8 Streams FlatMap method example
(7 answers)
Closed 3 years ago.
I have a Stream<ArrayList<Object>> and I want to "extract" the ArrayList from it and assign it to a variable. How do I do that?
My resulting variable needs to be of type ArrayList<Object> so I can iterate over it and do stuff.
If you want to get one ArrayList then use
ArrayList<Object> result = strm.flatMap(ArrayList::stream)
.collect(Collectors.toCollection(ArrayList::new));
Stream.flatMap method lets you replace each value of a stream with
another stream and then concatenates all the generated streams into a single stream.
List<Object> objectList = new ArrayList<>();
List<Object> collect = Stream.of(objectList)
.flatMap(m -> m.stream())
.collect(Collectors.toList());
This question already has answers here:
Remove duplicates in ArrayList - Java
(8 answers)
Closed 4 years ago.
How to remove duplicate from List of map below is the code snippet.
In Map multiple duplicate entry will be there for value i want to remove those duplicate values.
List<Map<String, Object>> obtainAuditLog = pddBamControlDao.populateAuditLogMap();
You can use java stream API to remove duplicates from the list:
Sample code:
List<Map<String, Object>> obtainAuditLogNoDuplicate = obtainAuditLog.stream()
.distinct()
.collect(Collectors.toList());
This question already has answers here:
Zipping streams using JDK8 with lambda (java.util.stream.Streams.zip)
(14 answers)
Closed 5 years ago.
for simplicity I have 2 lists of String and I need to join the strings into one and create another list.
For eg --
List 1 = [a,b,c,d]
List 2 = [e,f,g,h]
I want the output as
List3 = [ae,bf,cg,dh]
I can do this using regular for loops. but dont know how to proceed for java8
I am trying to get myself thinking in n Java 8 :-)
I'm not sure there's a better (easy) way to do this than to access the elements from the two lists by index:
List<String> zipped = IntStream.range(0, list.size())
.mapToObj(i -> list1.get(i) + list2.get(i))
.collect(Collectors.toList());
This question already has answers here:
How to convert a Java 8 Stream to an Array?
(9 answers)
Closed 5 years ago.
I have a List of 'Client' objects each one with a field "email".
I need something like:
List<String> listEmails = clients.stream().map(client->client.getEmail())
.collect(Collectors.toList());
...but returning directly a String[].
Is there a proper way to map a List<Client> to a String[] listEmails using Java 8 streams?
Sure :
String[] result = clients
.stream()
.map(client->client.getEmail())
.toArray(String[]::new)
This question already has answers here:
Java 8 lambdas group list into map
(2 answers)
Closed 5 years ago.
Lets say I have a List<Person> Gathering and I want a Map<String, List<Person>>, mapping Person.surname to a List of Person:s that have the same surname. Is there a convenient way to do this using streams?
Yes, by using Collectors.groupingBy(...):
Map<String, List<Person>> personsBySurname = gathering.stream()
.collect(Collectors.groupingBy(Person::getSurname));