I am trying to collect a list into a map but I have duplicate entry in the list object.
So suppose I have aList
where in that I have
[{1, "abc.jpg"}, {2, "bcd.png"}, {1, "mno.jpg"}, {3, "abc.jpg"}, {1, null}, {1, ""}]
Now in above list you can see 1 has value at repetition with different values.
I want to collect the above list into map provided only if it has some value in it.
like in above set you may have question if I find distinct then collect by grouping them which filled value will be picked my answer is any filled value is required but not empty, whitespace or null.
so anyone could be okay except in three case empty, whitespace or null.
I just want to set in my list if that id has any image or not therefore checking for only empty, whitespace or null.
Output(Ambiguous):
Output 1
[{1, "abc.jpg"}, {2, "bcd.png"}, {3, "abc.jpg"}]
Output 2
[{2, "bcd.png"}, {1, "mno.jpg"}, {3, "abc.jpg"}]
Both above list result could be the output in map.
My Approach:
itemImageMap = imageService.findByItemUuidIn(itemUuids)
.stream().filter(obj->(Objects.nonNull(obj)&& Objects.nonNull(obj[1])))
.collect(Collectors.toMap(obj->String.valueOf(obj[0]), obj->String.valueOf(obj[1])));
I am getting duplicate key error.
For more clarity on what my purpose is to collect this in map. Once I collect into map my goal is this =>
itemDetailsList.stream().filter(obj->itemImageMap.containsKey(obj.getItemUuid())).map(obj-> {
String imageUrl = itemImageMap.get(obj.getItemUuid());
if(StringUtils.isNotBlank(imageUrl))
obj.setShowImage(true);
return obj;
});
if I have a path then set true else default is false.