Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

count HashMap values

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Tony Docherty Thank you for reply, your method working. I didn't consider delimiter, but you give a idea.
 
You've gotta fight it! Don't give in! Read this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic