java


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

Related Links

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)?

Categories

HOME
grep
leon
rdf
ravendb
acquia
gorm
kibana-4
windows-10-universal
tomcat6
quickbooks
circular-dependency
alignment
windows-phone-7
visual-studio-2005
visual-studio-cordova
clojurescript
transformation
pugjs
text-rendering
google-pagespeed
nodatime
custom-wordpress-pages
object-detection
cx-freeze
autoconf
web-sql
chromium-embedded
realex-payments-api
cultureinfo
lcd
rundeck
cas
chromebook
minitab
opentype
gsmcomm
gtrendsr
usbserial
ghost4j
nssegmentedcontrol
wpf-controls
espeak
elasticsearch-plugin
azure-ml
awt
mesos-chronos
quadratic-programming
spring-security-kerberos
midl
wso2carbon
qwt
osx-mavericks
chain-builder
upstart
fancybox-2
statsd
nodebb
smart-table
cubes
medium.com
grails-tomcat-plugin
skobbler-maps
mongocsharpdriver
qpid
gridview-sorting
markojs
ipconfig
independentsoft
kendonumerictextbox
google-places
ruby-2.2
atk4
sysinternals
mono-embedding
remobjects
truevault
tween
gwidgets
codeigniter-url
fpml
id3v2
xojo
has-many-through
jubula
yorick
system.net.webexception
hyprlinkr
algebraic-data-types
soundtouch
bulkloader
dmoz
path-separator
mt
datawindow
armcc
invite
xtype
telerik-scheduler
site.master
h.323
web-application-design
routedevent
forums
microsoft-virtualization
premature-optimization

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App