So I know that sets cannot take duplicates... more formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most has one null element. I realize Treeset sorts the set for me. Here is my set code:
import java.util.Set;
import java.util.HashSet;
import java.util.TreeSet;
public class SetExample {
public static void main(String args[]) {
int count[] = {11, 22, 33, 44, 55};
Set<Integer> hset = new HashSet<Integer>();
try{
for(int i = 0; i<4; i++){
hset.add(count[i]);
}
System.out.println(hset);
TreeSet<Integer> treeset = new TreeSet<Integer>(hset);
System.out.println("The sorted list is:");
System.out.println(treeset);
}
catch(Exception e){
e.printStackTrace();
}
}
}
This is my output:
ArrayList Elements:
[Chaitanya, Rahul, Ajeet]
LinkedList Elements: [Kevin, Peter, Kate]
[33, 22, 11, 44]
The sorted list is:
[11, 22, 33, 44]
Why is the set ALWAYS in the order of [33,22,11,44]?