【问题标题】:How to put an array object in hashmap?如何将数组对象放入哈希图中?
【发布时间】:2016-05-06 07:09:17
【问题描述】:

我正在尝试解决这个问题,但我现在在这里停留了一段时间。我的问题是我找不到如何将数组对象放入我的哈希图的方法。

这是我从 google 获得的下拉菜单的 hashmap 示例代码。

private Map<String,Map<String,String>> data2 = new HashMap<String,Map<String,String>>();
private String country; 
private String city;  
private Map<String, String> countries;
private Map<String,String> cities;

 countries  = new HashMap<String, String>();
        countries.put("USA", "USA");
        countries.put("Germany", "Germany");
        countries.put("Brazil", "Brazil");

        Map<String,String> map = new HashMap<String, String>();
        map.put("New York", "New York");
        map.put("San Francisco", "San Francisco");
        map.put("Denver", "Denver");
        data2.put("USA", map);

        map = new HashMap<String, String>();
        map.put("Berlin", "Berlin");
        map.put("Munich", "Munich");
        map.put("Frankfurt", "Frankfurt");
        data2.put("Germany", map);

现在.. 我的问题是如何将我的对象放入 hashmap 中。

这是我获取数组对象的代码。

     while(rs.next()){
                     PropertyData reader = new PropertyData();
                     id = rs.getInt("id");
                     prop_name = rs.getString("prop_name");
                     prop_value = rs.getString("prop_value");
                     engine_name = rs.getString("engine_name");
                     //other setter..


                     reader.setId(id);
                     reader.setPropName(prop_name);
                     reader.setPropValue(prop_value);
                     reader.setEngineName(engine_name);
                     //other setter..

                     prop.add(reader);
                 }

从上面的代码。我想将 id 放在 hashmap 键上,将 propname 放在 hashmap 字符串名称上。选择后,我想显示引用 id 的其他值。

这是我的对象被添加的地方。

    static List<PropertyData> prop = new ArrayList<PropertyData>();

我试图在我的 hashmap 上初始化它,但它返回 null。

【问题讨论】:

  • 不可能知道你想做什么,问更好的问题,发布一些完整的代码(准备运行)并展示你的问题。
  • 只要说出你想放的什么和你想放的哪里就足够了。 2行代码。
  • 好吧,等我编辑一下

标签: java arrays hashmap


【解决方案1】:
 new HashMap<String, String>() 

表示键的类型为字符串,值的类型为字符串。如果您想在值中包含 PropertyData,则将其定义为

  new HashMap<String, PropertyData>()

【讨论】:

    【解决方案2】:

    如果您可以使用以下符号创建 HashMap,则可以将列表添加到您的地图..

    HashMap<String, List<PropertyData>> map = new HashMap<>();
    

    【讨论】:

      【解决方案3】:

      好的,您在此列表中有数据:

      List<PropertyData> prop = new ArrayList<PropertyData>();
      

      现在,像这样创建地图(如果 PropertyData 有一个唯一的 id - 还将 size 设置为数组的大小,调整大小只会进行一次):

      HashMap<Integer, PropertyData> map = new HashMap<>(prop.size());
      

      这就是你在地图中放置它的方式:

      for (PropertyData pd : prop) map.put(pd.getId(), pd);
      

      如果PropertyData 中的id 不是唯一的,请创建int hashCode() 函数,该函数将创建唯一的int(基于所有变量中的值 - IDE 可以为您创建)。

      【讨论】:

      • 我试过这个,但 IDE 建议添加 cast 到 map 然后它变成了这样。 ((List) map).add(pd.getId(), pd); 然后我得到一个错误 java.util.HashMap cannot be cast to java.util.List
      • 对不起,我的错误函数应该是 map.put 而不是 map.add.. 我会更新我的答案...
      • 嗨,兄弟,如果可以,我有一个后续问题。我可以在循环内获取 sysout 中的所有值,但是当我实现它时,我只能检索数组的最后一个元素。也许我在循环之外叫错了?调用它或将其传递到循环之外的正确方法是什么?非常感谢
      • PropertyData 循环内我有这个代码countries = new HashMap&lt;Integer, PropertyData&gt;(); countries.put(pd.getId(),pd); 我把它放在这里private Map&lt;Integer, PropertyData&gt; countries; 现在当我在我的xhtml 中调用它时,我的下拉菜单显示“36”的值,它是我的id的最后一个元素
      • countries = new HashMap();这应该在循环之前。如果这是在循环中,您将在每次迭代中创建新地图,因此您将只有最后一个元素。
      猜你喜欢
      • 2019-04-26
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      • 1970-01-01
      • 2011-09-01
      • 1970-01-01
      • 2023-03-27
      • 2014-03-26
      相关资源
      最近更新 更多