Solved

usage of contains in Arraylist

Posted on 2013-11-21
4
197 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web application structure 18 135
Java: anonymous class 4 54
Java array sort 10 62
Selenium WebElement Login button findElement 9 20
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

738 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