• Post Reply Bookmark Topic Watch Topic
  • New Topic

looking for a way to count certain values in an array list and how many times they occur  RSS feed

 
jim Jimerson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, First time here. i have a csv file with 5000 plus rows fed in to an array list, one of my Columns is country's, i want to go through that column get all the country's listed and how many times they occur then i want to feed the county's and the occurrences into a graph of some sort. im just not too sure how to go about it, i have the array list out putting to a J table and i have individual items outputting to separate text areas in a separate tabbed panel.

i was thinking i would would take my current array list run it through a sorter/counter and put the country's and their number of occurrences into a new array list but im not to sure how to go about it.



any advice is greatly appreciated.


Cheers


 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Jim.

Instead of a List, why not try a Map? You can have the country as the key and the number of occurrences as a value. You can build it as you read in the file line by line.

For reading CSV files, I recommend using a third party product, like Apache Commons CSV.

For now, I'd just write a program that reads to file and prints the counties and their occurrences. Than create a GUI. At that point you may want to post to the Swing or JavaFX forum.
 
jim Jimerson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i already have a reader setup and working putting a whole lot of data into an array list so i can display it in a Jtable, also will the map auto sort and count for me or will i have to make a method to do it for me?
 
Carey Brown
Bartender
Posts: 3011
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you're already using Swing. I suggest looking into JFreeChart for doing your plotting. Nice package, fairly easy to use.
 
Carey Brown
Bartender
Posts: 3011
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Maps

You'd need to put the map into a list at the end for sorting.
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
will the map auto sort and count for me or will i have to make a method to do it for me?

TreeMap will sort the key values (country), or you can use map.keySet() to return a Set of all the keys. A Set is like a List except that it doesn't accept duplicates.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!