Solved

usage of contains in Arraylist

Posted on 2013-11-21
4
194 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 166 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 167 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 27

Accepted Solution

by:
dpearson earned 167 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate jars 4 58
Is Applet the way to go for my drag and drop system? 8 35
Crystal Reports Licensing Questions 4 60
How to fix  socket closed error 11 27
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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:
This video teaches viewers about errors in exception handling.

756 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