Question

I'm taking a java class and the instructor has had us build a HashMap like this:

Map<String, SortedSet<Car>> makeSetMap = new HashMap<String, SortedSet<Car>>();

The Car objects are read in from a file of cars (like Toyota Corolla 8 90000) and do some processing to get them into the Hashmap as described above. So the map's key is a string which ends up being a manufacturer name (Toyota) and then the values are SortedSet of Cars.

Later in the app i'm supposed to set it up so a user can select which model he wants to have displayed and then it will display just car data that are from that model. Right now I'm just stuck on figuring how to access all the data in the whole thing. I've written some code for the Hashmap and I can get the Manufacturer names to display with an iterator, but I can't figure out how to display the individual car data. :(

Here's what I have so far - it's a snippet, but I think you guys can figure it out:

    //I'm starting to have trouble here..
    Set<String> brands = makeSetMap.keySet();
    System.out.println("Brands are: " + brands.toString());
    Iterator<String> brandsIt = brands.iterator();

    while (brandsIt.hasNext())
    {
        SortedSet<Car> brandmodels;
        //brandmodels = brandsIt.next();
        //System.out.println("Working on: " +brandmodels.toString());

    }

Help! Thanks to anyone who can straighten me out.

Was it helpful?

Solution

To print all the car models for each manufacturer:

Map<String, SortedSet<Car>> makeSetMap = new HashMap<String, SortedSet<Car>>();

// ...

for(String key: makeSetMap.keySet()) { // loop through all manufacturers
   SortedSet<Car> cars = makeSetMap.get(key);
   for(Car c: cars) { // loop through all cars of that key
       System.out.println(key + " " + car);
   }
}

assuming your Car class has a toString override.

OTHER TIPS

To access all data you can use something like this:

for (Map.Entry<String, SortedSet<Car>> entry : makeSetMap.entrySet()) {
    String manufacturer = entry.getKey();
    SortedSet<Car> cars = entry.getValue();
    System.out.println("Working on: " + manufacturer);

    for (Car car : cars) {
        System.out.println(" Car: " + car);
    }
}

To access cars for specific manufacturer you can use something like this:

for (Car car : makeSetMap.get("Toyota")) {
    System.out.println("Car: " + car);
}

So you have map like this

Map<String, Set<? extends Car>> carMap = ...

And if you want all the Toyota's cars, then you would simply do:

Set<? extends Car> allToyotas = carMap.get("Toyota");

If you want to iterate, then you can do:

for(Car toyota : allToyotas) {
   System.out.println(toyota);
}

Something like this?

Set<String> brands = makeSetMap.keySet();
System.out.println("Brands are: " + brands.toString());

for(String brand : makeSetMap.keySet())    {
    SortedSet<Car> brandmodels = makeSetMap.get(brand);
    for(Car car : brandmodels){
        System.out.println(car);
    }
}

Yes, I figured it out based on this and the other comments! Thanks to everyone for your help here! Whew! Basically, while building the makeSetMap, I also was required to build a map ("makeMap") of Manufacturer names like (1-toyota, 2-honda), etc. I was to scan the user input at runtime and collect which I was to use, then somehow use that manufacturer name to extract all the cars in the hashmap and display their data. This code snippet was the key I was missing: SortedSet<Car> carSortedSetTmp = makeSetMap.get(makeMap.get(choice));

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top