Reputation: 16192
I am trying to write a program that finds the duplicate value in map, and this map is created using the list and utiity methods.
I am able to get the expected output using for loop, but the code is too long using that.
I tried using the java stream API as shown below, but got empty results.
public class PrintListElements {
public static void main(String[] args) {
List<String> roles = new ArrayList<>();
roles.add("1");
roles.add("2");
roles.add("3");
roles.add("4");
roles.add("5");
roles.add("6");
roles.add("7");
roles.add("1");
HashMap<String, List<String>> idToMap = new HashMap<>();
roles.stream().map(role -> {
if (idToMap.containsKey(role)) {
return idToMap.get(role).add(getName(role));
} else {
return idToMap.put(role, new ArrayList<>(Arrays.asList(getName(role))));
}
})
idToMap.entrySet().forEach(e-> {
if(e.getValue().size()>1) {
System.out.println("found a key which has duplicate value : "+ e.getKey());
}
});
}
public static List<String> getNameLL(String id) {
ArrayList<String> ll = new ArrayList<String>();
ll.add(getName(id));
return ll;
}
public static String getName(String id) {
switch (id) {
case "1":
return "one";
case "2":
return "two";
case "3":
return "third";
case "4":
return "four";
case "5":
return "fifth";
case "6":
return "six";
case "7":
return "seven";
case "8":
return "one";
default:
return "one";
}
}
}
Expected Output:
[one, one]
[two]
[three]
[four]
[five]
[six]
[seven]
Can anyone please help me, to get the above-expected output result using Java Stream API
Upvotes: 1
Views: 1658
Reputation: 18480
You can use Collectors.groupingBy
to group by key and use Collectors.mapping
to map values and collect as a list for each key.
Map<String, List<String>> idToMap =
roles.stream()
.collect(Collectors.groupingBy(e -> e,
Collectors.mapping(e -> getName(e), Collectors.toList())));
Or map
operation is lazy, so code inside .map
is not executed. You can use terminal operation like forEach
by refactoring your current code,
roles.forEach(role -> {
if (idToMap.containsKey(role)) {
idToMap.get(role).add(getName(role));
} else {
idToMap.put(role, new ArrayList<>(Arrays.asList(getName(role))));
}
});
which can be simplified using Map
's merge
method
roles.forEach(
role -> idToMap.merge(role, new ArrayList<>(Arrays.asList(getName(role))), (a, b) -> {
a.addAll(b);
return a;
}));
Update: If you want just print for duplicate value, you can use Collectors.counting()
to get the frequency of key as a result collect as Map<String, Integer>
roles.stream()
.collect(Collectors.groupingBy(e -> e, Collectors.counting()))
.entrySet()
.forEach(e -> {
if (e.getValue() > 1) {
System.out.println("found a key which has duplicate value : " + e.getKey());
}
});
Upvotes: 4