Skip to main content
added 24 characters in body
Source Link
Jigar Joshi
  • 241.4k
  • 42
  • 409
  • 446
class A{
    
      @Override
      public int hashCode() {
        return 10;
    }
}

public class SampleClass {

    public static void main(String[] args){
        Map map = new HashMap();

        map.put(new A(), "A");
        map.put(new A(), "B");

        System.out.println(map.size());
        
        System.out.println(new A().hashCode());
        System.out.println(new A().hashCode());
    }
}

Output :-

2

10Output :-

10

2

10

10

Why 2???. If we are implementing hashCode method which is returning same integer. Should not the size be 1???

class A{
    
      @Override
      public int hashCode() {
        return 10;
    }
}

public class SampleClass {

    public static void main(String[] args){
        Map map = new HashMap();

        map.put(new A(), "A");
        map.put(new A(), "B");

        System.out.println(map.size());
        
        System.out.println(new A().hashCode());
        System.out.println(new A().hashCode());
    }
}

Output :-

2

10

10

Why 2???. If we are implementing hashCode method which is returning same integer. Should not the size be 1???

class A{
    
      @Override
      public int hashCode() {
        return 10;
    }
}

public class SampleClass {

    public static void main(String[] args){
        Map map = new HashMap();

        map.put(new A(), "A");
        map.put(new A(), "B");

        System.out.println(map.size());
        
        System.out.println(new A().hashCode());
        System.out.println(new A().hashCode());
    }
}

Output :-

2

10

10

Why 2???. If we are implementing hashCode method which is returning same integer. Should not the size be 1???

added 90 characters in body
Source Link
Oliver Charlesworth
  • 273.6k
  • 34
  • 590
  • 687

class A{

class A{
    
      @Override
      public int hashCode() {
        return 10;
    }

}

public class SampleClass {

}

public class SampleClass {

    public static void main(String[] args){
        Map map = new HashMap();

        map.put(new A(), "A");
        map.put(new A(), "B");

        System.out.println(map.size());
        
        System.out.println(new A().hashCode());
        System.out.println(new A().hashCode());
    }
}

}

Output :-

2

10

10

Why 2???. If we are implementing hashCode method which is returning same integer. Should not the size be 1???

class A{

  @Override
  public int hashCode() {
    return 10;
}

}

public class SampleClass {

public static void main(String[] args){
    Map map = new HashMap();

    map.put(new A(), "A");
    map.put(new A(), "B");

    System.out.println(map.size());
    
    System.out.println(new A().hashCode());
    System.out.println(new A().hashCode());
}

}

Output :-

2

10

10

Why 2???. If we are implementing hashCode method which is returning same integer. Should not the size be 1???

class A{
    
      @Override
      public int hashCode() {
        return 10;
    }
}

public class SampleClass {

    public static void main(String[] args){
        Map map = new HashMap();

        map.put(new A(), "A");
        map.put(new A(), "B");

        System.out.println(map.size());
        
        System.out.println(new A().hashCode());
        System.out.println(new A().hashCode());
    }
}

Output :-

2

10

10

Why 2???. If we are implementing hashCode method which is returning same integer. Should not the size be 1???

Source Link
Jaikrat
  • 1.2k
  • 4
  • 25
  • 49

hashCode implementation

class A{

  @Override
  public int hashCode() {
    return 10;
}

}

public class SampleClass {

public static void main(String[] args){
    Map map = new HashMap();

    map.put(new A(), "A");
    map.put(new A(), "B");

    System.out.println(map.size());
    
    System.out.println(new A().hashCode());
    System.out.println(new A().hashCode());
}

}

Output :-

2

10

10

Why 2???. If we are implementing hashCode method which is returning same integer. Should not the size be 1???