1

I am attemping to populate a JComboBox with the names of cities.

My program has a class called 'Country'. The Country object contains a HashMap of objects called 'City' with a method getName, returning a String value.

public class Country {

    private final Map<String, City> cities = new HashMap<>();

    public Collection<City> getCities() {
        return cities.values();
    }

}

public class City {

    String cityName;

    public String getName() {
        return cityName;
    }

}

Is it possible to return an String array of cityName without using a loop? I was trying the following but it did not work:

Country country 1 = new Country();

String[] cityNames = country1.getCities().toArray();
JComboBox cityChoice = new JComboBox(cityNames);

This returns an Array of City objects, however I am not sure how to use the City getName method in conjunction with this.

marcolopes
  • 9,232
  • 14
  • 54
  • 65
  • Duplicate of http://stackoverflow.com/questions/3293946/the-easiest-way-to-transform-collection-to-array – skozlov Apr 04 '15 at 11:20
  • Don't think that is a duplicate - they are related clearly, but this question is about turning a collection of a particular type into an array which contains a property of the original collection not the collection itself. – Mike Curry Apr 04 '15 at 11:26

4 Answers4

1

You can not avoid looping. Either, you will loop, or Java will loop in the background.

You can avoid writing your own loop if keys in your map are city names. Then, you could only ask .keySet() from the map. But, even in that case, Java would loop in the background and copy the keys.

Other way is that you loop, but hide the loop in some method (lets say getCitiesArray()) in the class. So, you could do country1.getCitiesArray(); in the calling method. Code would look better and be easier to read, but you would still need to have loop inside of the class.

mr.engineer
  • 187
  • 12
  • 1
    +1 for the second suggestion. With Java 8 the `getCitiesArray()` method could be `return getCities().stream().map(City::getName).toArray(String[]::new);` – Alexis C. Apr 04 '15 at 11:34
0

You can store Map key as CityName then do below to get Names.

cities.keySet();
Himanshu Ahire
  • 677
  • 5
  • 18
0

If you are using Java 8, you can use the Stream API to map the names of the cities to a String:

String []cityNames = country1.getCities().stream().map(City::getName).toArray(String[]::new);
Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
  • Unfortunately, the JComboBox constructor does not accept a list as parameter, but you could change it to `.toArray(String[]::new);` instead of `collect(toList());` since the OP wants an array – Alexis C. Apr 04 '15 at 11:38
  • .... or `.collect(toCollection(Vector::new)` but IMO an array is better since the `Vector` class is kind of obsolete. – Alexis C. Apr 04 '15 at 11:41
  • @AlexisC. Thanks for the comment. I thought converting a List to an Array would have been straightforward but your suggestion seems more elegant. – Chetan Kinger Apr 04 '15 at 11:41
0

The city object can be used directly in the combobox with some minor alterations.

public class City {

    String cityName;

    public String getName() {
        return cityName;
    }

    @Override
    public String toString() {
        return getName();
    }

 }

Then the population code

Country country1 = new Country();

City[] cities = country1.getCities().toArray();
JComboBox<City> cityChoice = new JComboBox<City>(cities);

You should probably override hashCode and equals also.

Mike Curry
  • 1,609
  • 1
  • 9
  • 12