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

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

Related

How do I go from Stream<ArrayList<Object>>> to ArrayList<Object> in Java? [duplicate]

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

Add Elements to an ArrayList within a HashMap [duplicate]

This question already has answers here:
Adding a value to a list to an already existing key in Map
(3 answers)
Closed 4 years ago.
How to add an Element to an ArrayList within a HashMap?
This is a question, that I have asked myself many times and forgot it after solving it. I guess many have the same one so here is the simple answer to it.
// Example
HashMap<String, ArrayList<String>> someElements = new HashMap<String, ArrayList<String>>();
// fill the HashMap with the key that you need
// initiate it with an empty ArrayList
someElements.put("keyString" , new ArrayList<String>());
// Later when wanting to add an element to the ArrayList of a key use
someElements.get("keyString").add("TheStringValue");

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)

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

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

Categories