簡體   English   中英

如何將自定義Java對象添加到HashTable

[英]How to add custom java object to HashTable

我有一個名為Items的類,其中包含一些數據,如下所示:

class Items {
    public String item1 ;
    public String item2;
    public int item3;
}

我還有另一個Java類,用於為此Items類設置值,如下所示:

Items tempItems1 = new Items();
tempItems1 .item1  = "a";
tempItems1 .item2  = "b";
tempItems1 .item3  = 1;

Items tempItems2 = new Items();
tempItems2 .item1  = "aa";
tempItems2 .item2  = "bb";
tempItems2 .item3  = 11;

現在的問題是,我需要將這兩個對象(tempItems1,tempItems2)添加到HashTable中,這樣我才能遍歷HashTable來獲取此值。

我按如下方式創建了HashTable,但找不到找到添加上述每個Java對象並遍歷它的方法。

HashTable<String,Items> custom = new HashTable<String,Items>();

有人可以幫我解決這個問題嗎?

custom.put("string_key1", tempItems1);
custom.put("string_key2", tempItems2);

橫移

for (Map.Entry<String, Items> entry : custom.entrySet()) {
    // entry.getKey()
    // entry.getValue()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM