Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

usage of contains in Arraylist

Posted on 2013-11-21
4
Medium Priority
?
202 Views
Last Modified: 2013-12-03
I have an Arraylist, which is based on a class containing below properties.

private String selection;
private String choice;

so Arraylist will contain objects, with each containing above properties.

I have a value for selection. I want to check this value is available in property selection
in any of the object in the list, without using a loop.

Is there any way to achieve this?
0
Comment
Question by:sakthikumar
  • 2
4 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 664 total points
ID: 39667291
You could do that by making equals dependent only on the 'selection' attribute. But that wouldn't be a hugely good implementation
0
 
LVL 14

Assisted Solution

by:jb1dev
jb1dev earned 668 total points
ID: 39667465
As CEHJ says, you could change the equals() method on your class to only compare the "selection" member. You would then call the ArrayList's contains() method, which will in turn call the equals() method on all items in the collection until it finds one where the equals() method return true.
I would agree that that is probably not a good implementation though.
Also it still uses a loop, just not in your code. The loop occurs in the ArrayList contains() implementation.

If you do not want to use a loop because you are concerned about performance, i.e. you want constant time O(1) access, you could consider using a HashMap, where you would insert into the map a key which would be the "selection" String, and a value which would be the object instance. That's only if you need the object instance. If only need to know if "selection" is present, you could just use a HashSet<String> and add the "selection"  String to the HashSet.

Then you get constant time contains() checking without using a loop.

E.g.
HashSet<String> set = new HashSet<String>();
set.put(selection);
...
...
if(set.contains(somevalue)) {
   ...
}
0
 
LVL 28

Accepted Solution

by:
dpearson earned 668 total points
ID: 39667471
I have a value for selection. I want to check this value is available in property selection
in any of the object in the list, without using a loop.

If you only store the values in the list then somebody (either the Java library or your code) will have to loop through them to find the ones with that value.  There's no getting around that.

If you want to make it faster and avoid the looping, then you should store the values in a another data structure, like a Map instead:

Map<String, Object> map = new HashMap<String, Object>() ;
Object myObject = map.get(selection) ;

A lookup from a hashtable takes constant time - no looping required.

Doug
0
 
LVL 14

Expert Comment

by:jb1dev
ID: 39667494

set.put(selection);

Sorry, that should be
set.add(selection);
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question