Java 8: Map of field to list of items [duplicate] - java

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::get‌​Surname));

Related

How to remove duplicate from list<Map<String,Object>> [duplicate]

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());

Java 8: Merge 2 String Lists into Map [duplicate]

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)));

JAVA8: Map list of objects to String[] [duplicate]

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)

How to map from one container into another w/Java 8 streams? [duplicate]

This question already has answers here:
Collection to stream to a new collection
(4 answers)
Closed 7 years ago.
So, in Clojure, I can just say something like this...
(into [] (map some-function some-collection))
And I get a new vector of my transformed data.
Is there some simple equivalent of into for Java 8 streams? For example, I don't see a constructor on ArrayList that takes a stream, nor do I see some sort of helper function in java.util.Collections, nor the stream interface.
You can do it using Collectors:
someCollection.stream()
.map(someFunction)
.collect(Collectors.toList());
You can do other cool stuff with Collectors, as explained in its javadoc:
Map<Department, Integer> totalByDept =
employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.summingInt(Employee::getSalary)));

Retrieve values from hashmap (.values()) [duplicate]

This question already has answers here:
How do I convert a Map to List in Java?
(13 answers)
Closed 9 years ago.
I would be pleasure if you can suggest.
Could you suggest how properlly I can retriev all values from hashmap.
In such simplee example:
Map<String, Values> someMap = ....;
List<Values> valuesFromMap = (List<Values>) someMap.values();
After that I've got that, Java couldn't cast to java.util.List
Thank you in advanced.
The values method returns a Collection, not a List. Use
Collection<Values> valuesFromMap = someMap.values();
someMap.values() returns Collection. If you need to convert into List than do that as follows:
List<Values> valuesFromMap = new ArrayList<>(someMap.values());

Categories