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

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 {com.pra.cg.Employee@2=A, com.pra.cg.Employee@12=G, com.pra.cg.Employee@22=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









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 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 is Kafka max message size
What are Basic Java Language Elements?
what is the default buffer size for bufferedreader
What type of headers have to be added in the mail function to attach a file?
ModuleNotFoundError: No module named 'odoo11-addon-stock-putaway-same-location'
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?
hashmap
Interview Questions - What are important Java Language Elements?
HASHMAP
Hashmap
Java Hashmap Iterator
HashMap and HashCode
Hashmap
HashMap
program for Hashmaps - Java Beginners
Collection : HashMap Example
HashMap - Struts
java program to compare two hashmaps and check whether they are equal or not
What is the maximum size of a file that can be uploaded using PHP and how can we change this?
ModuleNotFoundError: No module named 'bucket'
How to draw a circle bucket, draw a circle bucket, circle bucket
Java HashMap example.
Java HashMap - Java Tutorials
How to design a color in the bucket, design a color in the bucket, color in the bucket
Java HashMap iterator and example
JSON to HashMap
Size of commarea
ModuleNotFoundError: No module named 'farbox-bucket'
ModuleNotFoundError: No module named 'maskerade.rides2-secure-bucket'
ModuleNotFoundError: No module named 'maskerade.rides3-secure-bucket'
ModuleNotFoundError: No module named 'mbonig.secure-bucket'
ModuleNotFoundError: No module named 'mbonig.secure-bucket'
ModuleNotFoundError: No module named 's3-unique-bucket'
ModuleNotFoundError: No module named 'token-bucket'
ModuleNotFoundError: No module named 'alt-bucket'
ModuleNotFoundError: No module named 'bucket-filter'
ModuleNotFoundError: No module named 'Bucket-Limiter'

Ads