2013-11-22 58 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 –