如何在Android中的hashmap中添加不同类型的对象?

时间:2012-05-05 04:09:34

标签: java data-structures hashmap

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

每个数据记录应具有以下参数;

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;

来放置和获取这些记录

2 个答案:

答案 0 :(得分: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); 
     } 

答案 1 :(得分:0)

如果您有多个ContactRecord个对象等于Count值,则无需显式存储该值。

鉴于您的数据结构,您可以这样做。

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

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

map.get(customerId).size();