0

I have a map like as follows

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

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

If i want to get an element from map i can use

 map.get("one");

I have a list

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);

If i want to search from this list which has name "one" i need to iterate this list.Is there any simple way to find out this?

I found this Searching in a ArrayList with custom objects for certain strings

But is there any other simple way to do that?Thanks in advance...

6
  • have you tried using Collections.binarySearch(list, "one");? Commented Nov 22, 2013 at 6:56
  • @Linus "one" is a name property of TestVO class. Instances of TestVO are stored in the List. Commented Nov 22, 2013 at 6:57
  • Did you read this answer in the question you linked? Commented Nov 22, 2013 at 6:58
  • @R.J I saw the marked answer Commented Nov 22, 2013 at 6:59
  • Marked answer doesn't mean that's the best answer. It means that the marked answer helped OP solve their problem, but doesn't mean other answers are wrong. If you'd read the other answers, you'd have seen it and if you're not bound by any clause to not use a 3rd-party lib, then the answer I proposed could very well be your solution. Why not give it a try? Commented Nov 22, 2013 at 7:01

1 Answer 1

3

Searching data in Hash Map is of complexity O(1) where as in case of List it is O(N).

So unfortunately the answer is you have to iterate over the list. That is why choosing proper data structure is so important.

Sign up to request clarification or add additional context in comments.

1 Comment

@Debriter I meant HashMap if that is what your point is. Refer - stackoverflow.com/questions/4553624/hashmap-get-put-complexity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.