-1

I would like to know, what is best way to create variable in Java.

I have country states, some int and some another int :

 USA -> {{"NY", 100, 100},{"CO", 200, 200},...}
 CAN -> {{"Quebec", 100, 100} ...}

There are some countries. I know I need create class, where I will put this data (state, integer, integer), but later I will want provide country and take only this countries data. Data will not change.

Should I put everything into one static variable or split into different vars?

Darka
  • 2,762
  • 1
  • 14
  • 31
  • 1
    How would you "put everything into one static variable"? And you need a collection of some sort. – Zong Oct 22 '13 at 19:57
  • Why not have a `Country` class with `String state` `int whatever` `int whatever2` fields? If there's ever a place for OOP, it's here. If you want this data to not change, you can make country immutable(http://stackoverflow.com/questions/3162665/immutable-class) – Cruncher Oct 22 '13 at 19:57
  • @Cruncher Ok I will create Country class, but how put data in it. What I mean, where should I initiate them (in contructor, ...)? Like create different variable for every Country, put into List or Map? – Darka Oct 22 '13 at 20:02
  • Make a Country class that accepts an array of States in the constructor. Then make a State class that accepts `String, int, int` in its consturctor. – Cruncher Oct 22 '13 at 20:04
  • Have enums really not been mentioned yet? – clwhisk Oct 22 '13 at 20:15
  • I know stupid question and maybe not clear, but thanks for help. – Darka Oct 22 '13 at 20:23

6 Answers6

1

As java is Object Oriented Programming, my suggestion would be model it as an object

class State{
  int code;
  String Name;
  int population;
  //other info
}

class Country{
int code;
String name
List<State> state;
// Other details

}

Now in a class create a Collection of Country

List<Country> countries;
Shamse Alam
  • 1,285
  • 10
  • 21
1

I understand your question as follows:

You want to store associations of country to collection-of-{sate, data1, data2}, and later access the collection that is associated with a specific country.

In this case you can use a HashMap, which provides just such association. Assuming you have a StateData class that captures the {state, data1, data2} triple, you can write:

HashMap<String, Collection<StateData>> countries;

Populate the map with:

List<StateData> stateColl = new LinkedList<StateData>();
Map<String, List<StateData>> countries = new HashMap<String, List<StateData>>();
countries.put("USA", stateColl);
stateColl.add(stateData1);
stateColl.add(stateData2);
//...

where stateData1,... is of type StateData. Note that you can use any collection that fits your needs instead of LinkedList. You could also use a TreeMap instead of the HashMap if you need to store the countries in (alphabetical) order, but that comes with some performance hits.

Then access the state collections as:

List<StateData> stateColl = countries.get("USA");
for (StateData stateData : stateColl)
{
  // process the state info
}
Attila
  • 28,265
  • 3
  • 46
  • 55
1

I suggest you have a class to host the data you need to keep (String,int,int), something like this

class State{
private String name;
private int id,xyz;
//setter and getter methods
}

then you may need two lists/array, one for US, and another for CAN.

public class Foo{
private List<State> usGuys=new ArrayList<>();//you would use State[] instead
private List<State> canGuys=new ArrayList<>();
}

for keeping the instances you would either keep them withing List or arrays, it's up to you and the requirements.

but about your issue, you would keep all the data into one array/list, so in this case you need to add another attribute to State class to indicates the country
private String country
but this may complex/make-it-hard the process in future because for example if you want to get all states in US, you need a for loop in order to check which states are belong to US, having multiple list may give you a better management
but what if the size of the countries is unknown!
simple enough, like you have list for storing states, you will have a list for keeping countries., something like this

class Country{
private List<State> states;
private String name;
}

class Foo{
private List<Countries> countries;
}

I hope I could give some hand.

1

Enum will be ideal for storing this information. They are preferred way of storing constants since Java5.

drop.in.ocean
  • 298
  • 2
  • 9
1

It can be done in one operation by using an initializer block. A minimal implementation to reprsent your data would be:

Create classes for your entities:

class Country {
    String shortName;
    List<State> states;
    Country(String s, State... stateArray) {
        shortName = s;
        states = Arrays.asList(stateArray);
    }
    // getters omitted
}

class State {
    String code;
    int num1, num2:
    State(String c, int n1, int n2) {
        cude = c;
        num1 = n1;
        num2 = n2;
    }
    // getters omitted
}

Then to create the variable:

List<Country> countries = new ArrayList<Country>() {{
    add(new Country("USA", 
        new State("NU", 100, 100),
        new State("CO", 200, 200)
    );
    add(new Country("CAN",
        new State("Quebec", 100, 100)
    );
 }};
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

How about something like this:

static class State {
    String name;
    int i1, i2;
    State(String name, int i1, int i2) {
        this.name = name;
        this.i1 = i1;
        this.i2 = i2;
    }
}

final static Map<String, Map<String, State>> countryStateMap;

static {
    countryStateMap = new HashMap<>();
    Map<String, State> usa = new HashMap<>();
    usa.put("NY", new State("NY", 100, 100));
    usa.put("CO", new State("CO", 200, 200));

    Map<String, State> ca = new HashMap<>();
    ca.put("Quebec", new State("Quebec", 100, 100));

    // and so on

    countryStateMap.put("USA", usa);
    countryStateMap.put("CA", ca);
}

This uses a final static Map an initializes it in a static block. The initialization is done a soon as the class is loaded.

Then you can use it like this in your code

Map<String, State> usaStates = countryStateMap.get("USA");
State ny = usaStates.get("NY");
A4L
  • 17,353
  • 6
  • 49
  • 70
  • Thanks @A4L This almost what I want. So just to conferm, you suggest to put coutryStateMap as static in some class and then call it in different class something like someClass.coutryStateMap.get("USA");. Will be more then 7 countries. – Darka Oct 22 '13 at 20:17