【问题标题】:how do I add objects of different types to a hashmap in Android?如何将不同类型的对象添加到 Android 中的 hashmap?
【发布时间】:2012-05-05 04:09:33
【问题描述】:

我正在尝试添加要通过单个键值添加/检索的各种记录。

每条数据记录应有以下参数;

public class WebRecord {
        Integer UniqueCustomerID;
        Integer Count; // Number of websites for a given customer

//Repeat the following 'Count' number of times
       { // Each Record for a given Website for the same customer
    String BusinessName, Websites, siteTag;
    Integer uniqueWebID, BusinessTypeID;
       }

}

我希望这些记录基于我从 Web 服务获得的计数值而存在。 我打算如下使用Hashmap:

谁能告诉我如何在 HASHMAP 中实现该数据结构? 我想使用单个键值 UniqueCustomerID 放置和获取这些记录;

【问题讨论】:

  • 这与android没有任何关系。您应该删除 android 标签。

标签: java data-structures hashmap


【解决方案1】:

不擅长java概念,但我认为你可以做到,

     class WebRecord{ 
          int UniqueCustomerID; 
          int Count;
     } 

     class ContactRecord{ 
        String BusinessName, Websites, siteTag; 
        int uniqueWebID, BusinessTypeID; 
    } 

在你的 java 文件中

   MyActivity{ 
            public Map<WebRecord,ArrayList<ContactRecord>> map=new HashMap<WebRecord,ArrayList<ContactRecord>>(); 

        //now create one object of web record

        //create arraylist of ContactRecord depending upon "count" variable's value

        // fill the above both and put into map

            map.put(webRec, arrContact); 
     } 

【讨论】:

    【解决方案2】:

    如果您有多个等于Count 值的ContactRecord 对象,那么您不需要显式存储该值。

    鉴于您上面的数据结构,您可以做到。

    public HashMap<Integer, ArrayList<ContactRecord>> map;
    

    地图的关键是UniqueCustomerID。要发现 Count 值,您可以像这样查询地图中的列表。

    map.get(customerId).size();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多