How does one Convert a List to Map with Java-8 Streams where Key is the ListValue and the Value is the amount of values of that specific list
This is my list: List<Card> cards; my Java-8 stream where i want to create the Map Map<String, Integer> cardsMap = cards.stream().collect(Collectors.groupingBy(Card::getCardValue, amount of cards that are grouped)); This obviously doesn't work but i am clueless of how i would do it otherwise.
Is this what you meant? Map<String, Long> cardsMap = cards .stream() .collect(Collectors.groupingBy(Card::getCardValue, Collectors.counting())); It will give you a map from card values to counts of cards with that value in your original list. For example, if you have: List<Card> cards = Arrays.asList(new Card("4"), new Card("8"), new Card("4")); (and I know I’ve probably reduced your Card() constructor), the above will map "4" to 2 and "8" to 1.
found the problem, this is how one does it: Map cardsMap = cards.stream().collect(Collectors.groupingBy(e -> e.getCardValue(), Collectors.counting())); found answer here: How to count the number of occurrences of an element in a List
How to ignore lines starting with tilde symbol“~” while parsing?
JAVA Get and Post Requests
Java - Declaring constants in Model class or in the constants class?
Java IOException when deploying war file in wildfly 8
Why I must cast to Generic Type T even if I know it returns correctly?
register log4j 1.x as MXBean in Spring
What is the difference between direct initialization of class member vs doing it within a method?
JAX-RS web service is working on GlassFish Server 4.0 but not working on Tomcat
How To insert Gujarati in TextBox via Spring Hibernate in database?
gcm_defaultSenderId can't be resolved
Does jspInit() run only once?
Reading standard input via command line java
Rendering multiple lines through click and unclick libgdx java
Separating variables/arrays from an expression, and deleting duplicates?
execution of task in java within specified time
Why Does (a) print (0)?