Coding - Interview Questions and Answers on Enum, Set and compareTo

Identify the Output for following code snippets.


1. 

enum Day {

MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY
}


public class BuggyBread1{

    public static void main (String args[]) {
   Set<Day> mySet = new HashSet<Day>();
   mySet.add(Day.SATURDAY);
   mySet.add(Day.WEDNESDAY);
   mySet.add(Day.FRIDAY);
   mySet.add(Day.WEDNESDAY);
   for(Day d: mySet){
    System.out.println(d);
   }
    }
}


OutPut

FRIDAY, SATURDAY and WEDNESDAY will be printed but the order cannot be determined.

Only one FRIDAY will be printed as Set doesn't allow duplicates.

Order cannot be determined as HashSet doesn't maintain elements in any particular order.


2.

enum Day {

MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY
}


public class BuggyBread1{

    public static void main (String args[]) {
    Set<Day> mySet = new TreeSet<Day>();
    mySet.add(Day.SATURDAY);
    mySet.add(Day.WEDNESDAY);
    mySet.add(Day.FRIDAY);
    mySet.add(Day.WEDNESDAY);
    for(Day d: mySet){
     System.out.println(d);
    }
    }
}

Output 

WEDNESDAY 
FRIDAY
SATURDAY


Only one FRIDAY will be printed as Set doesn't allow duplicates.

Elements will be printed in the order in which constants are declared in the Enum. TreeSet maintains the elements in the ascending order which is identified by the defined compareTo method. compareTo method in Enum has been defined such that the constant declared later are greater than the constants declared prior. 

3.

public class BuggyBread1{

    public static void main (String args[]) {
   Set<String> mySet = new TreeSet<String>();
   mySet.add("SATURDAY");
   mySet.add("WEDNESDAY");
   mySet.add("FRIDAY");
   for(String d: mySet){
    System.out.println(d);
   }
    }
}


Output

FRIDAY
SATURDAY
WEDNESDAY


TreeSet maintains the elements in the ascending order which is identified by the compareTo method. compareTo method in String has been defined such that it results in the natural alphabetic Order.


4.

public class BuggyBread1{

    public static void main (String args[]) {
   Set<String> mySet = new TreeSet<String>();
   mySet.add("1");
   mySet.add("2");
   mySet.add("111");
   for(String d: mySet){
    System.out.println(d);
   }
    }
}


Output

1
111
2

TreeSet maintains the elements in the ascending order which is identified by the compareTo method. compareTo method in String has been defined such that it results in the natural alphabetic Order. Here the elements in the TreeSet are of String and not of Integer. In String Natural Order, 111 comes before 2 as ascii of 1st character first determines the order.

5.

public class BuggyBread1{

    public static void main (String args[]) {
   Set<Integer> mySet = new TreeSet<Integer>();
   mySet.add(1);
   mySet.add(2);
   mySet.add(111);
   for(Integer d: mySet){
    System.out.println(d);
   }
    }
}

Output

1
2
111

TreeSet maintains the elements in the ascending order which is identified by the compareTo method. compareTo method in Integer has been defined such that it results in the natural numerical Order.