For HashMap will implement hashCode () for reference values, if I'm only looking for a key?

I am using Java 6. For simplicity, everything will be publicly available.

Suppose I have this simple class.

public class A{
    public String name;
    public String data;
}

I want to put class A objects in a HashMap. I will use the field nameas a key, and the whole object as a value.

I will only search for an object on this map name.

My question is: for class A do I need to implement hashCodeand equalsto search for goals? Will this speed up the search? I know this will help for Sets, but I'm not sure about HashMaps whose key is just String.

+3
source share
3 answers

, hashCode equals . , ( ).

+5

, , JTK, String . , A

+2

hashCode equals HashMap. , A, - .

+1

Source: https://habr.com/ru/post/1772187/


All Articles