We all know that hashcode is used when you store objects in map or set. And we all know that the hashcode() method of the object is used to uniquely identify the object stored in the set and that HashSet will not store duplicate values. What do we derive from these? Shall we say, "Objects having same hashcode are considered as same"?! Guess what is the output of the following program. public class HashCodeTest { static class Person { int hashCode = 1; @Override public int hashCode() { return hashCode; } } public static void main(String[] args) { Set persons = new HashSet (); persons.add(new Person()); persons.add(new Person()); persons.add(new Person()); persons.add(new Person()); System.out.println("Set size: " + persons.size()); Map map = new HashMap (); map.put(new Person(), 1); map.put(new Person(), 2); map.put(new Person(), 3); map.put(new Person(), 4); System.out.pr...
All about Java Programming & Rich Client Application development using Netbeans