You can use Collectors.toMap(), to make a new Map with zone id as the key and Zone as the value, if there is a duplicate then take the List<Part> from the second Zone and append it to the first one:
Map<Integer, Zone> map = z.getZones().stream()
.collect(Collectors.toMap(Zone::getId, Function.identity(),
(zone1, zone2) -> {
zone1.getParts().addAll(zone2.getParts());
return zone1;
}));
Answer from Fullstack Guy on Stack OverflowThis is possible using the mergeFunction parameter of Collectors.toMap(keyMapper, valueMapper, mergeFunction):
Map<String, String> phoneBook =
people.stream()
.collect(Collectors.toMap(
Person::getName,
Person::getAddress,
(address1, address2) -> {
System.out.println("duplicate key found!");
return address1;
}
));
mergeFunction is a function that operates on two values associated with the same key. adress1 corresponds to the first address that was encountered when collecting elements and adress2 corresponds to the second address encountered: this lambda just tells to keep the first address and ignores the second.
As said in JavaDocs:
If the mapped keys contains duplicates (according to
Object.equals(Object)), anIllegalStateExceptionis thrown when the collection operation is performed. If the mapped keys may have duplicates, usetoMap(Function keyMapper, Function valueMapper, BinaryOperator mergeFunction)instead.
So you should use toMap(Function keyMapper, Function valueMapper, BinaryOperator mergeFunction) instead. Just provide a merge function, that will determine which one of duplicates is put in the map.
For example, if you don't care which one, just call
Map<String, String> phoneBook = people.stream().collect(
Collectors.toMap(Person::getName, Person::getAddress, (a1, a2) -> a1));
Sure. Just use the toMap overload accepting a merge function:
Collectors.toMap(fac -> fac.getScriptinterfaceid(),
fac -> getSiteIDAndNPI(fac.getPlid(), session, log),
(value1, value2) -> value2 /* accept the second version */))
Try using
list.stream.collect(Collectors.groupingBy(srv::Scriptinterfaceid))
This will return a map with Scriptinterfaceid as the Key & List<srv> as Value.
That's what groupingBy is for:
Map<String,List<String>> vehicle =
stream.collect(Collectors.groupingBy(e -> e[0],
Collectors.mapping(e -> e[1],
Collectors.toList())));
Output map:
{CAR=[Audi], BIKE=[Harley Davidson, Pulsar]}
You can use groupingBy
getMapStream()
.map(item -> Arrays.asList(item))
.collect(Collectors.groupingBy(l->l.get(0),
Collectors.mapping(l1->l1.get(1),Collectors.toList())));
or use toMap() with merge function.
Map<String,List<String>> vehicle = getMapStream()
.collect(Collectors.toMap(item->item[0],
item->new ArrayList<>(Arrays.asList(item[1])),
(l1,l2)->{l1.addAll(l2);return l1;}));
Map<String, Adegae> adegaeMap = adegaes.stream()
.collect(Collectors.toMap(adegae -> adegae.getId().getAecode().trim(),
Function.identity(), (e1, e2) -> e1)); may help in your case
You can use the distinct() method on Stream to eliminate the duplicates before further processing.
You are searching for a multimap, and indeed both commons-collections and Guava have several implementations for that. Multimaps allow for multiple keys by maintaining a collection of values per key, i.e. you can put a single object into the map, but you retrieve a collection.
If you can use Java 5, I would prefer Guava's Multimap as it is generics-aware.
We don't need to depend on the Google Collections external library. You can simply implement the following Map:
Map<String, ArrayList<String>> hashMap = new HashMap<String, ArrayList>();
public static void main(String... arg) {
// Add data with duplicate keys
addValues("A", "a1");
addValues("A", "a2");
addValues("B", "b");
// View data.
Iterator it = hashMap.keySet().iterator();
ArrayList tempList = null;
while (it.hasNext()) {
String key = it.next().toString();
tempList = hashMap.get(key);
if (tempList != null) {
for (String value: tempList) {
System.out.println("Key : "+key+ " , Value : "+value);
}
}
}
}
private void addValues(String key, String value) {
ArrayList tempList = null;
if (hashMap.containsKey(key)) {
tempList = hashMap.get(key);
if(tempList == null)
tempList = new ArrayList();
tempList.add(value);
} else {
tempList = new ArrayList();
tempList.add(value);
}
hashMap.put(key,tempList);
}
Please make sure to fine tune the code.