• Post Reply Bookmark Topic Watch Topic
  • New Topic

Efficient Arraylist search  RSS feed

 
sethu subbiah
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,

I am looking for an efficient way to search whether all elements in arraylist<string> 1 are present in arraylist<string> 2. If not get them all in a separate list. Thank you !
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it could be as simple as

However you need to consider: can the lists contain duplicate values? If so, does the number of times a duplicate occurs have an effect on the answer? In that case, a HashSet probably won't help, but a HashMap might.
 
sethu subbiah
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi , there are more than 40,000 entries minimum but they are all unique. So redundant data is not an issue. IT should be effecient and less time consuming. Those are bunch of paths. Thanks !
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
40,000 is not all that much for a pre-sized Set implementation. Have you tried it?
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The costly part is to check wether list2 contains an element from list1 (each call to contains() potentially has to check list2.size() elements). You can improve this by copying list2 into a HashSet, because the contains() method of the HashSet has near constant time cost. So you'd look at something like:


 
sethu subbiah
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, Its working. Thanks a lot ppl.
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surprisingly my latest post talk about the exact same problem!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!