Set<String> set2 = new HashSet<>();
String s2 = new String("rajkumar");
String s3 = new String("rajkumar");
set2.add(s3);
set2.add(s2);
System.out.println(set2.size()); //this will output 1
Person p1= new Person("rajkumar", 25);
Person p2= new Person("rajkumar", 25);
Set<Person>set= new HashSet<>();
set.add(p1);
set.add(p2);
System.out.println(set.size());//This will output 2
why is this happening. Isn't the has hashcode of a string objects should be different?
here person class is like
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}