Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

easiest way?

 
Charles Saik
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an input file that contains a bunch of records. Each record has a state abbreviation and several counts of various things.

My program is setup to read in the file and process the counts as a whole, and it works great. But what I need to do now is process the counts by state as well.

I figured that I would make a two-dimensional array, with the first dimension indicating the 50 states, and the second dimension to hold the accumulated counts for each state.

The trouble is, the state abbreviations are strings, and the counts are integers. I could probably use int constants to indicate each state, but I'm not sure if this is the easiest / most effective way to do what I want.

Any suggestions?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you probably want to use a Map of some kind. this will let you store pairs - one (the state as a string) is they key value, and the other is the data.

in your case, you'd simply check to see if the key exists in the map. if so, increment the current value. if not, create the key with whatever initial value you have.
 
Tony Docherty
Bartender
Posts: 2969
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are going to have to trawl the array to find the matching state for every record you read in you'd be better off using a Map such as HashMap. Maps are much more efficient at finding data than having to search an array each time. You could use the state string as the key and create a simple local mutable integer class (to hold the count) as the value.
 
Charles Saik
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all.

I've never used a HashMap before... can someone show me how to create a simple one?
 
Adam Nace
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I would recommend making an enum, and using an enum map. Although 50 is awefully big for an enum, it'll make your code cleaner, because if you find a name isn't a state, it'll get caught by the valueOf method. This should be relatively safe, because the fifty states aren't likely to change anytime soon.

- Adam
 
Eric Duval
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.TreeMap;
public class TreeMapTest {

public static void main (String args[]) {
TreeMap<String, Integer> theMap = new TreeMap<String, Integer>();

theMap.put("NY", 1);
theMap.put("CA", 1);


String state = "CA";
if(theMap.containsKey(state)) {
int count = theMap.get(state) + 1;
theMap.put(state, count);
}

System.out.println("California was found " + theMap.get("CA") + " times.");
System.out.println("Here is the full map:\n " + theMap.toString());
}
}

===============
OUTPUT:
===============

California was found 2 times.
Here is the full map:
{CA=2, NY=1}
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mentioned "several counts of various things". In situations like that it's often worth making a custom class to represent one row.

Then you can do something like

I used lots of bad shortcuts there ... public variables, didn't show a constructor, etc. Does the idea look good for your problem?
 
Charles Saik
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a bunch. That looks very interesting. I'm going to have to check it out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic