Reputation: 11
List<Map<String, Object>> apptlist = jdbcTemplate.queryForList(sb.toString());
Here I got a list. In that list duplicate maps are showing. I need to remove those duplicates using Java 8 Stream. Can anybody help me?
I tried Stream this
Set<Map<String, Object>> set = new HashSet<>();
apptlist.stream().map(s -> s.toLowerCase()).filter(s -> !set.contains(s)).forEach(set::add);
and this
apptlist.stream().distinct().collect(Collectors.toList());
but I didn't get any results. Still, I am getting duplicates.
Upvotes: -2
Views: 2958
Reputation: 556
Maps are equal when their length equals and for every key map1[key].equals(map2[key])
String
has a proper equals method in java but Object
does not.
Default equals
implementation for Object compares the address of the object, not its fields values.
We can see your code works properly when using values with proper equals
method such as Integer
import java.util.List;
import java.util.Map;
import java.util.Arrays;
import java.util.HashMap;
import java.util.stream.Collectors;
class Main {
public static void main(String[] args) {
Map<String, Integer> map1 = new HashMap<>();
Map<String, Integer> map2 = new HashMap<>();
map1.put("a", 1);
map1.put("b", 2);
map2.put("a", 1);
map2.put("b", 2);
List<Map<String, Integer>> mapList = Arrays.asList(map1, map2);
System.out.println(mapList.stream().distinct().collect(Collectors.toList()));
}
}
[{a=1, b=2}]
Upvotes: 1
Reputation: 2948
You don't need the stream API, creating a Set
will elimnate duplicates:
Set<Map<String, Object>> set = new HashSet<>(list);
Provided that, equals map has the same keys, and the values associated to that keys in the Maps implements equals
and hashCode
.
Upvotes: 2