Sets don't allow duplicate values. want to add duplicates ,then go with lists
When we use keySet method on a map it basically returns a Set of keys from a Map
Operations on Sets are very fast
Set is basically a Map with key as the key it stores and value is basically null
And if we iterate through the items of a set we have to use an iterator . we don't have a method called get for a Set as we have in case of Lists.
What we can do is use a for loop to loop through all items of a Set.Here planets is a set of class named HeavenlyBody
for(HeavenlyBody planet :planets){
System.out.println("\t"+planet.getName())
Please note that if we try to add two different objects to the above Set called planets where both objects have a same class member variable name
called "Pluto" both will be added to Set as both are considered different objects as the default equality method on objects(.equals) only checks for
referential equality i.e object reference is different i.e two objects are considered equal even if two references points to same object
But if we want two objects to be considered same based on some variable like in our case if we want to check if name is same we override the equals and hashcode method for the concerned class ,in our case HeavenlyBody