We help IT Professionals succeed at work.

ArrayList versus HashMap

booksplus
booksplus asked
on
904 Views
Last Modified: 2006-11-18
If I'm using an ArrayList as a cache to tell me if I looked something up and did not find anything - SO don't look it up again - THEN
I don't actually need the value - I ONLY need the key.

THEREFORE my question is - in this case what is faster a HashMap or an ArrayList.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
HashSet

Commented:
HashMap
u put the key it gives the value
CERTIFIED EXPERT
Top Expert 2016

Commented:
Values are not needed, right?

Commented:
if u want just the key exist or not =>
  public classname getobjectuwant(String key) {
    classname objectname =null;
    boolean found=false;
    for(Iterator i=hashmap.keySet().iterator();i.hasNext();){
      objectname =(classname)hashmap.get((String)i.next());
      if(objectname .getLabel().equals(label)) {
        found=true;
        break;
      }
    }
    return found?objectname :null;
  }
CERTIFIED EXPERT
Top Expert 2016

Commented:
boolean present = hashSet.contains(x);

boolean present = hashMap.containsKey(x);

Commented:
if(objectname .getLabel().equals(label)) {
replace with
if(objectname .getLabel().equals(key)) {

Author

Commented:
CEHJ - to answer your question - YES,  values are not needed.
CERTIFIED EXPERT
Top Expert 2016

Commented:
HashSet is the way to go then
ArrayList
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
if performance is your concern then use a TreeMap, or a TreeSet.
They'll provide the fastest lookup.
CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> In descending order of lookup:

I'd check your sources because that is incorrect.
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>O(C) for hashset is only applicable is it is assumed that ...

That's all correct, but with care, you should be able to get pretty close to O(C)

Author

Commented:
seet82 -
what do you mean "the keys are distributed evenly"  - I want to better understand what you are saying.  And please  remind me about meaning
O(C), etc.

Thanks.
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.