What is a good java data structure for storing nested elements (e.g. cities in states)?

I'm just starting out in Java and looking for advice on a good way to store nested datasets. For example, I am interested in storing data on the population of the city, which can be accessed by viewing the city in a certain state. (Note: in the end, other data will be stored with each city, this is only the first attempt to start work.)

The current approach I'm using is to have a StateList object that contains a HashMap that stores state objects via a string key (i.e. HashMap <String, State>). Each state object contains its own HashMap of city objects, disconnected from the city name (i.e. HashMap <String, City>).

A shortened version of what I came up with looks like this:

//TestPopulation.java

public class TestPopulation {

  public static void main(String [] args) {

    // build the stateList Object
    StateList sl = new StateList();

    // get a test state
    State stateAl = sl.getState("AL");

    // make sure it there.
    if(stateAl != null) {

      // add a city
      stateAl.addCity("Abbeville");

      // now grab the city
      City cityAbbevilleAl = stateAl.getCity("Abbeville");

      cityAbbevilleAl.setPopulation(2987);

      System.out.print("The city has a pop of: ");
      System.out.println(Integer.toString(cityAbbevilleAl.getPopulation()));

    }

    // otherwise, print an error
    else {
      System.out.println("That was an invalid state");
    } 
  }
}

//StateList.java

import java.util.*;

public class StateList {

  // define hash map to hold the states
  private HashMap<String, State> theStates = new HashMap<String, State>();

  // setup constructor that loads the states
  public StateList() {

    String[] stateCodes = {"AL","AK","AZ","AR","CA","CO"}; // etc...

    for (String s : stateCodes) {
      State newState = new State(s);
      theStates.put(s, newState);
    }
  }

  // define method for getting a state
  public State getState(String stateCode) {
    if(theStates.containsKey(stateCode)) {
      return theStates.get(stateCode);
    }
    else {
      return null;
    } 
  }
}

//State.java

import java.util.*;

public class State {

  // Setup the state code
  String stateCode;

  // HashMap for cities
  HashMap<String, City> cities = new HashMap<String, City>();

  // define the constructor
  public State(String newStateCode) {
    System.out.println("Creating State: " + newStateCode);
    stateCode = newStateCode;
  }

  // define the method for adding a city
  public void addCity(String newCityName) {
    City newCityObj = new City(newCityName);
    cities.put(newCityName, newCityObj); 
  }

  // define the method for getting a city
  public City getCity(String cityName) {
    if(cities.containsKey(cityName)) {
      return cities.get(cityName);
    }
    else {
      return null;
    } 
  }
}

//City.java

public class City {

  // Define the instance vars
  String cityName;
  int cityPop;

  // setup the constructor
  public City(String newCityName) {
    cityName = newCityName;
    System.out.println("Created City: " + newCityName);
  }

  public void setPopulation(int newPop) {
    cityPop = newPop;
  }

  public int getPopulation() {
    return cityPop;
  }
}

This works for me, but I wonder if there are any errors that I have not come across, or if there are alternative / better ways to do the same.

(PS I know that I need to add some more error checking, but right now I'm focusing on trying to find a good data structure.)

(NOTE: Edited to change setPop () and getPop () for setPopulation () and getPopulation (), respectively, to avoid confusion)

+3
source share
4 answers

Multimap guava. , . , MapMaker " " .

Multimap<String, City> stateToCities = ArrayListMultimap.create();

stateToCities.put("GA",new City("Atlanta",100000));
stateToCities.put("GA",new City("Cumming",50000));
+1

States, - .

: cityAbbevilleAl null. getPop -, .

0

"". . ; , .

Can the population be negative? If not, I would check this in your contract for setPopulation ().

0
source

Source: https://habr.com/ru/post/1782955/


All Articles