Unlike, say, a HashMap, an ArrayList does not need to use the hashCode() method since the order of the elements in an ArrayList is determined by the order in which they were inserted, and not by hashing.
Does ArrayList use the hashCode method of added objects when adding them
Pergunta
Does the following code use the hashCode
method of my Scooter
class:
void using_ArrayList(){
List coll=new ArrayList();
Scooter s1=new Scooter();
s1.setNumber("HR26KC345352344");
s1.setHorse_power(123.321);
s1.setYear_of_made(1997);
Scooter s2=new Scooter();
s2.setNumber("HR26KC34535");
s2.setHorse_power(123.321);
s2.setYear_of_made(1997);
Scooter s3=new Scooter();
s3.setNumber("HR26KC345352344");
s3.setHorse_power(123.321);
s3.setYear_of_made(1997);
coll.add(s1);
coll.add(s2);
coll.add(s3);
Scooter s=new Scooter();
s.setNumber("HR26KC345352344");
System.out.println(coll.contains(s));
}
Scooter Class:
class Scooter{
private String number;
private double horse_power;
private int year_of_made;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public double getHorse_power() {
return horse_power;
}
public void setHorse_power(double horse_power) {
this.horse_power = horse_power;
}
public int getYear_of_made() {
return year_of_made;
}
public void setYear_of_made(int year_of_made) {
this.year_of_made = year_of_made;
}
public boolean equals(Object o){
if((o instanceof Scooter)&&((Scooter)o).getNumber()==this.getNumber()){
System.out.println("EQUALS:TRUE"); //OK
return true;
}
else{
System.out.println("EQUALS:FALSE"); //OK
return false;
}
}
public int hashCode(){
System.out.println("HASHCODE");// NOT able To reach here...
return number.length();
}
}
I am able to reach equals()
method. But not able to reach hashCode()
method. Does hashCode()
method is NOT used by ArrayList
collection? Please tell me, as I am new to Java-Collections.
Solução
Outras dicas
Does hashCode() method is NOT used by ArrayList collection?
I assume that you mean the hashCode()
methods of the elements of the ArrayList
. The only case where the element hashCode()
methods are called by the ArrayList
object, is when computing the hash code of the ArrayList
itself.
You can confirm this by looking at the source code, or reading the javadocs. (The behaviour is specified in the List
API and implemented in AbstractList
...)
So, it is expected that you do not see calls to hashCode()
in your example, either from List.add
or List.contains
. In particular, contains
iterates the list elements calling equals
on each one until a call returns true
. The ArrayList
implementation does nothing clever to make contains
go fast.
I would also like to point out that the explanation given in this answer is incorrect:
"Unlike, say, a
HashMap
, anArrayList
does not need to use thehashCode()
method since the order of the elements in anArrayList
is determined by the order in which they were inserted, and not by hashing."
This is incorrect in a couple of respects:
The order of an ArrayList is not determined by the order in which elements are added. It is determined by the order and position of the additions (and other operations).
The reason for using
hashCode
inHashMap
is NOT to determine an order. (Indeed, the order of aHashMap
is nonsensical ... to a first approximation.) Hashing (viahashCode
) is actually used to provideO(1)
lookup by key.LinkedHashMap
is a counter-example to the stated reasoning. It preserves insertion order AND usehashCode
.
In fact, the two issues (element order and use of hashing) are orthogonal. The actual reasons that hashCode()
isn't used for lookup in ArrayList
are more pragmatic:
The addition of a hash table-like data structure would increase the
ArrayList
's memory overheads by a factor of at least 5 per element, and probably more.Implementing BOTH
O(1)
hash-based lookup based on elements values (forcontains
) ANDO(1)
lookup by element position (forget
) in the same data structure is very complicated.
ArrayList
is designed to be memory and time efficient for a subset of use-cases. Hashing would be a hindrance in those use-cases. But either way, it doesn't use it.
It is obvious from your code, it doesn't use the hashCode. This methods is used in hashes, not linear collections like ArrayList. And search there is performed in O(N)
Hashcode is used in only those collections which needs to identify unique values.
so its used in Set kind of collections.
in arraylist duplicates are allowed and hence no concept of checking hashcode.