Hash table. How it works?

Now I'm trying to figure out how to build Hashtable.

The most interesting is how are objects added to Hashtable?

I read in a book that:

in the first step: Calculated object hashCode().

Next, we determine the position of the object Hashtable: obj.hashCode() % Hashtable.length.

For example, add more elements to Hashtable:

Hashtable<String, String> hm=new Hashtable<String, String>(100);

        hm.put("Lee","Lee");
        hm.put("lee","lee");
        hm.put("eel","eel");

Define the bucket in which the object is placed:

    System.out.println("Lee".hashCode() % 100);
    System.out.println("lee".hashCode() % 100);
    System.out.println("eel".hashCode() % 100);

If I understand the algorithm, the objects should be placed in the table as follows:

eel /*because,"eel".hashCode() % 100=0*/, 
lee /*because, "lee".hashCode() % 100=20*/, 
Lee /*because, "Lee".hashCode() % 100=68*/

but what do we see as a result?

System.out.println(hm);

{Lee=Lee, lee=lee, eel=eel} 

Please tell me where I made a mistake?

+3
source share
4 answers

Hashtable ( HashMap) ( ), IMHO , . Java ( Java5 Java6).

Btw Hashtable , ( ) HashMap.

-. HashMap , , , Java4. . - ( -, ), , , , . . Java Specialist:

+7

A Hashtable - . Hashtable.

.hashCode .equals , / .

:

  • capacity, 100, . , Hashtable, 0,75.

  • . , , , "".

:

- , - , . . , rehash, .

+2

- , - ( ).

- (...), , Java , .

+1

As mentioned earlier, Hashtable is implementation dependent, and I would recommend reading about Hashtable in general to get an idea of ​​how they work, and then after understanding how it works, to read about a specific implementation in Java or another language .

Wikipedia has a good article on this topic, so I suggest reading this first.

0
source

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


All Articles