从Multimap

时间:2017-01-15 23:20:51

标签: java multimap

我想从multiMap打印对象的所有键和所有属性。 相同的键可以有不同的对象。

我使用以下代码创建了multiMap:

Multimap<Integer,Country> country=ArrayListMultimap.create();

我的班级国家是:

class Country {
    String country;
    int population;
}

如何从中检索所有对象属性: 使用HashMap我使用以下代码:

 for (Map.Entry p : country.entrySet()) {
    Country country=(Country)p.getValue();
    nameCountry=country.country;
    population=country.population;
 }

3 个答案:

答案 0 :(得分:1)

如果要重复,请使用keySet()以避免重复,使用keys()。然后通过get(..)

获取国家/地区实例

答案 1 :(得分:1)

使用几乎相同但不是entrySet()使用entries()

 for (Map.Entry<Integer, Country> p : country.entries()) {
     Country country=(Country)p.getValue();
     nameCountry=country.country;
     population=country.population;
  }

答案 2 :(得分:0)

您可以使用keySet获取唯一键,然后获取该键的集合。

 for (Integer key : countries.keySet()) {
      Collection<Country> collection = countries.get(key);
      for (Country country : collection)
      {
        String name = country.country;
        int poplulation = country.population;
      }
  } 
相关问题