1

Consider the below code

import java.util.HashSet;
import java.util.Random;
import java.util.Set;


public class Test {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    Person p1= new Person();
    //Person p2= new Person();
    p1.setName("same1");
    //p2.setName("same2");
    Person p2=p1;
    Set<Person> set= new HashSet<Person>();
    set.add(p1);
    set.add(p2);
    for(Person p: set){
        System.out.println(set.size()+">>"+p.getName()+" hashcode "+p.hashCode());
    }


}

}

class Person{
private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Override
public boolean equals(Object obj){
    return true;

}

@Override
  public int hashCode() {
    Random ran = new Random();
    int x = ran.nextInt(6) + 5;
    System.out.println("in hahcode method"+x);
    return x;
  }
}

As set does not contain duplicate, Here I have return true from equals method and return different hashcode for the same object. HashSet treat them as unique object. So far so good, as HashSet internally use HashMap to store objects.

It appears from above code snippet that HashSet use hashCode to check uniqueness not equals method. If hashCode is different for two objects,they will be stored in HashSet whether they are equal or not? Please let me know if I am missing something.

Vishal Singh
  • 624
  • 1
  • 5
  • 16
  • 1
    Your question about the max number of objects, doesn't really appear to match the question you're trying to ask of equals vs. hashcode. I suggest an edit. – Cruncher Feb 04 '14 at 14:50

2 Answers2

3

The maximum number of buckets that a HashSet can contain (remember: it's a HashMap under the hood) is given by its capacity. The initialCapacity can be passed as a parameter to the constructor, and as you can see in the documentation, it's an int. In theory the maximum number of buckets can be Integer.MAX_VALUE - the maximum size of an array in Java, but in practice this is implementation-dependent, for instance in the Oracle JDK it's 1 << 30.

However, a HashMap can contain more than one element per bucket, in case of collisions a linked list will be used at each bucket. So in principle the number of elements in a HashSet is unbound, as long as there's enough memory available for the JVM.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
0

Yes it is duplicate.Maximum size of HashSet, Vector, LinkedList

  • A HashSet uses a HashMap internally, so it has the same maximum size as that
    • A HashMap uses an array which always has a size that is a power of two, so it can be at most 230 = 1073741824 elements big (since the next power of two is bigger than Integer.MAX_VALUE).
    • Normally the number of elements is at most the number of buckets multiplied by the load factor (0.75 by default). However, when the HashMap stops resizing, then it will still allow you to add elements, exploiting the fact that each bucket is managed via a linked list. Therefore the only limit for elements in a HashMap/HashSet is memory.
Community
  • 1
  • 1
jarod
  • 9
  • 1
  • 4