Solved

usage of contains in Arraylist

Posted on 2013-11-21
4
182 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 166 total points
Comment Utility
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
Comment Utility
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 26

Accepted Solution

by:
dpearson earned 167 total points
Comment Utility
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
Comment Utility

set.put(selection);

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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 …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now