2011-07-01 54 views
2

因此,目前我從java中的一個XML文件中提取兩個不同的屬性(對於我的項目)相互關聯,並將它們打印到控制檯。但是,我希望能夠以引用一個值的方式存儲這些值,以檢索它的相應副本。例如:在Java中存儲一些數據的最佳方式是什麼? (Array vs ArrayList)

Id: rId11 & Target: image3 
Id: rId10 & Target: image2 
Id: rId9 & Target: image1 

有了這些3個值,我會想辦法來存儲每行,但是當我引用「RID」我能得到它的相應的「目標」值。我正在考慮使用數組還是數組,但我不確定哪個更適合我的目的,或者我會如何僅引用一個值並獲取其他值。任何人都可以給我一些建議嗎?先謝謝你。

回答

4

如果您的密鑰是唯一的,請使用Map

Map<String, String> mak = new HashMap<String, String>(); 
map.put("rId11","image3"); 
map.put("rId10","image2"); 
map.put("rId9","image1"); 

參考:

否則,創建包含鍵和值的自定義對象,並創建這些的List(或Set ???)。

public class Entry { 
    private final String id; 
    private final String value; 
    public Entry(String id, String value) { 
     this.id = id; this.value = value; 
    } 
    public String getId() { return id; } 
    public String getValue() { return value; } 
    // also implement equals() and hashCode(), please 
} 

List<Entry> entries = new ArrayList<Entry>(); 
entries.add(new Entry("rId11","image3")); 

參考:

+1

非常感謝!我決定使用你的HashMap的答案,但是對於我正在做的事情做了一些細微的修改。 Java相當新穎,所以在你提到它之前我都沒有考慮過映射。還要感謝所有爲您的輸入做出貢獻的人。 :) –

+0

@ This 0ne Pr0grammer:不客氣。如果您不熟悉Java,則應該通過Java教程http://download.oracle.com/javase/tutorial進行工作,並閱讀Josh Bloch的Effective Java(第2版)。 –

2

使用Map,其中Id爲關鍵字,Target爲目標值。請注意,Map是一個接口,因此僅定義行爲。你需要選擇一個特定的實現,比如HashMap。

2

你是一個有點曖昧你想要什麼。如果您想查找基於給定鍵的值,請將對存儲在HashMap(更快)或Hashtable(速度較慢但線程安全)。

原始陣列(以及更高級的基於List的集合,例如ArrayListVector)不支持開箱即用的名稱/值對。他們簡直就是......名單。原始數組可以提供更多的性能,因爲您可以避免創建對象,但更高級的List類型集合可以更安全,更靈活。

儘管如此,它聽起來像(?)像你想要一個Map類型集合,而不是List類型一。

UPDATE:順便說一句,如果你使用地圖,那麼你仍然可以使用所有「rId」值的列表。這將是一個Set數據類型實際上,但是這名單只是一種特殊的表弟,不允許重複:

Map<String, String> myMap = new HashMap<String, String>(); 
myMap.put("rId11","image3"); 
// ... additional put's for the other values 

Set<String> myRids = myMap.keySet(); 
for(String rId : myRids) { 
    // do whatever you want with each rId one-by-one, etc 
    // You could also use "myRids.iterator()" to work with an Iterator instead 
} 
+2

請不要推薦Vector和Hashtable。他們應該在幾年前被棄用。閱讀[this](http://stackoverflow.com/questions/453684/use-hashtable-vector-or-hashmap-or-arraylist-in-java)或[許多其他相關問題]之一(http:// stackoverflow.com/search?q=%2Bhashtable+%2Bvector+%2Bdeprecated)瞭解更多信息。 –

+0

哦,讓我休息一下。是的,我99.999%肯定這傢伙不是在編寫多線程應用程序。是的,即使在我自己的多線程開發中,自2002年以來,我還沒有使用Vector或Hashtable。但是,它是新手常見的混淆之處,他們永遠不會真正將它從標準庫中拿出來。所以我認爲至少向新手解釋那些多餘的類型是有用的。 –

+0

要澄清原始問題提交者...最好使用HashMap而不是Hashtable,而ArrayList而不是Vector。後者類型提供了一些神祕的保護級別,但對大多數開發沒有用處,並且帶來嚴重的性能損失。 –

2

我想,特別是如果排序是不是會的java.util.HashMap更適合這個要求需要。

// not sure what types these are but this would work better 
Map<String, String> m = new HashMap<String, String>(); 
m.put("rId11", "image3"); 
String other = m.get("rId11"); 
1

如果「鑰匙」你的目標值將是唯一和永遠只能有一個目標映射到他們,那麼我會建議使用的java.util.HashMap代替。您可以通過傳入密鑰來檢索任何目標值。另外你可以像遍歷ArrayList一樣迭代HashMap。

1
public class Item { 
    private String id; 
    private String target; 

    public Item(String id, String target) { 
     this.id = id; 
     this.target = target; 
    } 

    public String getId() { 
     return this.id; 
    } 

    public String getTarget() { 
     return this.target; 
    } 
} 

List<Item> items = new ArrayList<Item>(); 
// or 
Map<String, Item> itemsIndexedById = new HashMap<String, Item>(); 
// depending on your use-case 

閱讀the Java tutorial about collections

2

如果我理解正確,您希望能夠查找諸如「rId10」之類的內容並獲取值「image2」(並且僅限於此)。如果是這樣的話,我認爲最好的(在速度方面)和最簡單的解決方案將是一個哈希表(java.util.Hashtable) - 小心使用Java泛型(在Java 1.5之後)。也檢出http://en.wikipedia.org/wiki/Hash_table

+1

請不要推薦Hashtable。它應該在幾年前被棄用。閱讀[this](http://stackoverflow.com/questions/453684/use-hashtable-vector-or-hashmap-or-arraylist-in-java)或[許多其他相關問題]之一(http:// stackoverflow.com/search?q=%2Bhashtable+%2Bvector+%2Bdeprecated)瞭解更多信息。 –

1

如果您需要動態添加元素,ArrayList非常有用

相關問題