What exactly is bucket in hashmap?

42,595

Solution 1

No, a bucket is each element in the array you are referring to. In earlier Java versions, each bucket contained a linked list of Map entries. In new Java versions, each bucket contains either a tree structure of entries or a linked list of entries.

From the implementation notes in Java 8:

/*
 * Implementation notes.
 *
 * This map usually acts as a binned (bucketed) hash table, but
 * when bins get too large, they are transformed into bins of
 * TreeNodes, each structured similarly to those in
 * java.util.TreeMap. Most methods try to use normal bins, but
 * relay to TreeNode methods when applicable (simply by checking
 * instanceof a node).  Bins of TreeNodes may be traversed and
 * used like any others, but additionally support faster lookup
 * when overpopulated. However, since the vast majority of bins in
 * normal use are not overpopulated, checking for existence of
 * tree bins may be delayed in the course of table methods.
 ...

Solution 2

bucket

I hope this may help you to understand the implementation of hash map well.

Solution 3

Buckets exactly is an array of Nodes. So single bucket is an instance of class java.util.HashMap.Node. Each Node is a data structure similar to LinkedList, or may be like a TreeMap (since Java 8), HashMap decides itself what is better for performance--keep buckets as LinkedList or TreeMap. TreeMap will be only chosen in case of poorly designed hashCode() function, when lots of entries will be placed in single bucket. See how buckets look like in HashMap:

/**
     * The table, initialized on first use, and resized as
     * necessary. When allocated, length is always a power of two.
     * (We also tolerate length zero in some operations to allow
     * bootstrapping mechanics that are currently not needed.)
     */
    transient Node<K,V>[] table;
Share:
42,595
dgupta3091
Author by

dgupta3091

I started my career with Libsys, a company that builds Automation Systems required in Education domain. Next, I joined WizeCommerce, which was into digital marketing and later moved to working with Times Group real estate vertical Magicbricks.com, Noida. Currently I am working for Treasury team of ION Trading Pvt. ltd which is into Finance domain. I have primarily worked as a backend developer having experience in technologies like Java, J2EE, Spring Boot, Microservices, Kafka, Hibernate etc. I am also experienced in UI technologies (javascript,angular), tools like Docker, Git, Maven, testing frameworks like Robot, Selenium and Junit and RDBMS databases (MySQL, PostgreSQL). I am a research enthusiast and have addressed more than 3 international conferences in the field of Swarm Intelligence. I also have 12 research papers under my name , all published in renowned journals like IEEE, Springer, iJES, Inderscience, etc. and a technical blog https://darora3091.wordpress.com/

Updated on July 09, 2022

Comments

  • dgupta3091
    dgupta3091 almost 2 years

    Recently, in an interview I was asked, what exactly is a bucket in hashmap? Whether it is an array or a arraylist or what?

    I got confused. I know hashmaps are backed by arrays. So can I say that bucket is an array with a capacity of 16 in the start storing hashcodes and to which linked lists have their start pointer ?

    I know how a hashmap internally works, just wanted to know what exactly is a bucket in terms of data structures.

  • dgupta3091
    dgupta3091 about 8 years
    So when we say hashmap has a capacity of 16 in the start, so at that time it creates an array of 16 space and has its each element called as a bucket.?
  • Eran
    Eran about 8 years
    @dgupta3091 yes, though in Java 8 implementation the array is created lazily (i.e. only when the first entry is put in the HashMap).
  • Eran
    Eran about 8 years
    @JonnyHenly I just checked the implementation in Java 8, and none of the constructors initialize the array. It is only initialized by resize() (which will be called by put if the array is null) and readObject(java.io.ObjectInputStream s) (deserialization).
  • Jonny Henly
    Jonny Henly about 8 years
    @Eran That makes sense, I forgot the main reason to set an initial capacity is to minimize the number of rehash operations. I just think it's odd to create the array lazily, for instance - you create a hashmap with a rather large initial capacity in a loading method, thinking it will take some time. Then later down the road you find out the first call to put takes longer than the loading function. Perhaps my logic is off, I am pretty tired. Thanks for responding to my comment though.
  • Eran
    Eran almost 4 years
    @Konstantin yes, each bucket is an instance of java.util.HashMap.Node