Collectors.counting()
returns long
values for each key in this method:
private static Map<Integer, Long> countDuplicates(HashSet<Card> cards) {
return cards.stream().collect(Collectors.groupingBy(Card::getRankNumber, Collectors.counting()));
}
Is there a way to cast or convert the resulting Map
from Map<Integer, Long>
to Map<Integer, Integer>
?
Direct casting gives this exception:
Type mismatch: cannot convert from Map<Integer,Integer> to Map<Integer,Long>
Note: The implementation of my class guarantees that cards
has five objects in it, so there is no chance of overflow.
question from:
https://stackoverflow.com/questions/65910153/convert-a-hashmapinteger-long-ito-hashmapinteger-integer 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…