[英]how do I add objects of different types to a hashmap in Android?
我正在嘗試添加要通過單個鍵值添加/檢索的各種記錄。
每個數據記錄應具有以下參數;
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放置並獲取這些記錄;
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);
}
如果您有數量等於Count
值的ContactRecord
對象,則不需要顯式存儲該值。
考慮到上面的數據結構,您可以執行。
public HashMap<Integer, ArrayList<ContactRecord>> map;
映射的鍵是UniqueCustomerID
。 要發現Count
值,您可以像這樣查詢地圖中的列表。
map.get(customerId).size();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.