Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

usage of contains in Arraylist

Posted on 2013-11-21
4
Medium Priority
?
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 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:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

722 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