Reputation: 1329
I have a method as below
private Map<String,List<String>> createTableColumnListMap(List<Map<String,String>> nqColumnMapList){
Map<String, List<String>> targetTableColumnListMap =
nqColumnMapList.stream()
.flatMap(m -> m.entrySet().stream())
.collect(groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toList())));
return targetTableColumnListMap;
}
I want to uppercase the map keys but couldn't find a way to do it. is there a java 8 way to achieve this?
Upvotes: 7
Views: 30217
Reputation: 9839
This doesn't require any fancy manipulation of Collectors. Lets say you have this map
Map<String, Integer> imap = new HashMap<>();
imap.put("One", 1);
imap.put("Two", 2);
Just get a stream for the keySet()
and collect into a new map where the keys you insert are uppercased:
Map<String, Integer> newMap = imap.keySet().stream()
.collect(Collectors.toMap(key -> key.toUpperCase(), key -> imap.get(key)));
// ONE - 1
// TWO - 2
Edit:
@Holger's comment is correct, it would be better (and cleaner) to just use an entry set, so here is the updated solution
Map<String, Integer> newMap = imap.entrySet().stream()
.collect(Collectors.toMap(entry -> entry.getKey().toUpperCase(), entry -> entry.getValue()));
Upvotes: 16
Reputation: 2080
Answer for your question [which you can copy and paste] :
Map<String, List<String>> targetTableColumnListMap = nqColumnMapList.stream().flatMap(m -> m.entrySet().stream())
.collect(Collectors.groupingBy(e -> e.getKey().toUpperCase(), Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
Upvotes: 4