booksplus
asked on
ArrayList versus HashMap
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.
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.
HashSet
HashMap
u put the key it gives the value
u put the key it gives the value
Values are not needed, right?
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().iterato r();i.hasN ext();){
objectname =(classname)hashmap.get((S tring)i.ne xt());
if(objectname .getLabel().equals(label)) {
found=true;
break;
}
}
return found?objectname :null;
}
public classname getobjectuwant(String key) {
classname objectname =null;
boolean found=false;
for(Iterator i=hashmap.keySet().iterato
objectname =(classname)hashmap.get((S
if(objectname .getLabel().equals(label))
found=true;
break;
}
}
return found?objectname :null;
}
boolean present = hashSet.contains(x);
boolean present = hashMap.containsKey(x);
boolean present = hashMap.containsKey(x);
if(objectname .getLabel().equals(label)) {
replace with
if(objectname .getLabel().equals(key)) {
replace with
if(objectname .getLabel().equals(key)) {
ASKER
CEHJ - to answer your question - YES, values are not needed.
HashSet is the way to go then
ArrayList
if performance is your concern then use a TreeMap, or a TreeSet.
They'll provide the fastest lookup.
They'll provide the fastest lookup.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> In descending order of lookup:
I'd check your sources because that is incorrect.
I'd check your sources because that is incorrect.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>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)
That's all correct, but with care, you should be able to get pretty close to O(C)
ASKER
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.
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:-)