• Post Reply Bookmark Topic Watch Topic
  • New Topic

count HashMap values

 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am loading text file contents to GUI using this code:

Contents of input.txt file:

I w'd like to ask how can I count the items, i.e. values v in sections? How can I get number 4, according to authors name?
 
Dave Tolls
Ranch Hand
Posts: 2284
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your values were List<String> instead of a concatenated String it would be a lot easier.
Indeed, it would actually be possible.
 
Tony Docherty
Saloon Keeper
Posts: 3142
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are lots of ways of doing this, such as having a second map with the same keys but with a mutable Integer as the value which you increment every time you concatenate a new value; or you could change the current map's value type to an ArrayList and add each new value to the ArrayList - the size of the ArrayList then gives you the number of elements of that key type; or you could use your own class instead of an ArrayList which has two fields, a string and an int. You can then concatenate the values as you are currently doing and keep a count of how many values have been concatenated.

This list is certainly not exhaustive and the best approach depends on what you are trying to achieve.

BTW Are you sure you want to concatenate the authors names etc without any delimiter between them?
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Tony Docherty Thank you for reply, your method working. I didn't consider delimiter, but you give a idea.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!