If elements are added at same bucket location in HashMap then what will be the size of HashMap?

Ads
 

If elements are added at same bucket location in HashMap then what will be the size of HashMap?

I am trying to print the size of HashMap which contains key value pair at same bucket location. Following is the code. Please confirm the answer .

import java.util.HashMap; import java.util.Map;

public class HashcodeWork {

public static void main(String[] args) {

// int i = 34, j =15; // 2 and 18 34 -- bucket is 2 // int k = i & j ; // System.out.println(k); Map<Employee,String> map = new HashMap<Employee, String>(); Employee e1 = new Employee(2,"A"); Employee e3 = new Employee(18,"T"); Employee e5 = new Employee(34,"D"); map.put(e1, "A"); map.put(e3, "G"); map.put(e5, "O"); System.out.println(map + " Size:::: "+map.size());

}

}

class Employee { private Integer empId; private String name ;

public Employee(int i, String string) {
    // TODO Auto-generated constructor stub
    this.empId = i;
    this.name=string;
}
@Override
public boolean equals(Object arg0) {
    // TODO Auto-generated method stub
    Employee e = (Employee) arg0;
    return this.empId.equals(e.empId);
}
@Override
public int hashCode() {
    // TODO Auto-generated method stub
    int bucket = empId.hashCode();
    System.out.println(bucket+" bucket ");
    return bucket;
}

}

/* 2 bucket 18 bucket 34 bucket 2 bucket 18 bucket 34 bucket {[email protected]=A, [email protected]=G, [email protected]=O} Size:::: 3 */

Formula to calculate bucket location. // int i = 34, j =15; // 2 and 18 34 -- bucket is 2 // int k = i & j ; // System.out.println(k);

If this is not the way Java follows to calculate bucket location . I also tried with public int hashCode() { return 2; }

So whats the answer for , if bucket location is same for all the 3 elements then what will be the size of map?

View Answers

Ads









Related Tutorials/Questions & Answers:
If elements are added at same bucket location in HashMap then what will be the size of HashMap?
HashMap in Java
Advertisements
Example of size() method of HashMap.
DOJO+Creating button of same size.
how to sort the elements of hashmap and to print output the key and the value attached to it
JSP List Size
what is block size in hadoop
what is the size of java class
What are the elements in the deployment descriptor(web.xml)?
what is hibernate dtd file location
What type of headers have to be added in the mail function to attach a file?
What are Basic Java Language Elements?
What is Kafka max message size
what is the default buffer size for bufferedreader
javascript:getElementByClassname:Is it possible to collect all the elements with same classname
HashMap
What?s the maximum size of a row in SQL table?
Interview Questions - What are important Java Language Elements?
hashmap
HASHMAP
Hashmap
Java Hashmap Iterator
Hashmap
HashMap
HashMap and HashCode
program for Hashmaps - Java Beginners
How to draw a circle bucket, draw a circle bucket, circle bucket
How to design a color in the bucket, design a color in the bucket, color in the bucket
HashMap - Struts
Collection : HashMap Example
java program to compare two hashmaps and check whether they are equal or not

Ads