2013-11-22 36 views
0

如果我想從圖中的元素我有一個地圖就像如下如何使用屬性名稱

Map<String,Integer> map = new HashMap<String, Integer>(); 

map.put("one",1); 
map.put("two",2); 

搜索Java對象的名單,我可以使用

map.get("one"); 

我有列表

List<TestVO> list = new ArrayList<TestVO>(); 
TestVO vo1 = new TestVO(); 
    vo1.setId(1); 
    vo1.setName("one"); 


TestVO vo2 = new TestVO(); 
    vo2.setId(2); 
    vo2.setName("two"); 

list.add(vo1); 
list.add(vo2); 

如果我想從該名單中有名爲「一個」我需要遍歷這個搜索list.Is有沒有簡單的方法來找出THI S'

我發現這個Searching in a ArrayList with custom objects for certain strings

但有沒有其他簡單的方法來做到這一點?在此先感謝...

+0

你試過用'Collections.binarySearch(list,「one」);'? – Linus

+0

@Linus「one」是TestVO類的名稱屬性。 TestVO的實例存儲在列表中。 –

+0

在鏈接的問題中,您是否閱讀過[此答案](http://stackoverflow.com/a/12496479/2024761)? – SudoRahul

回答

3

哈希地圖搜索數據複雜度爲O(1)式中的情況下的列表是O(N)。

所以不幸的是,答案是你必須迭代列表。這就是爲什麼選擇合適的數據結構非常重要。

+0

@Debriter我的意思是HashMap,如果這是你的觀點。請參閱 - http://stackoverflow.com/questions/4553624/hashmap-get-put-complexity –