Q:

Changing the order of TreeSet elements to customized output

I know that TreeSet stores objects in a sorted manner. But is there a way that i can customize the order?  
For example if i have a treeSet:  
 TreeSet t1 = new TreeSet();
    t1.add("c");
    t1.add("d");
    t1.add("a");

And now if i iterate over it>
 Iterator it1 =t1.iterator();

    while(it1.hasNext()){
      Object o1 = it1.next();
      System.out.println(o1);
    }

i will always get the order as: a>c>d, however i want it to return the order the same as i added the elements in it i.e c>d>a? 

A:

Use LinkedHashSet for it, 
TreeSet sorts the element, and for string it sorts based on natural order( this is how its comparator is implemented), If you want to manage the insertion order then you need to user LinkedHashSet
and if you don't need the uniquness (feature of set) then go for List

A:

Since you mention about being bound to use TreeSet, something like this comes to my mind:  
 Set<String> result = new TreeSet<String>(new Comparator<String>(){
    @Override
    public int compare(String arg0, String arg1) {
        return returnCode(arg0).compareTo(returnCode(arg1));
    }
 });

where:  
private Integer returnCode(String p){
        int code = 0;
        String id = p.toLowerCase();
        if ("a".equalsIgnoreCase(id)) code = 3;
        else if ("b".equalsIgnoreCase(id)) code = 2;
        else if ("c".equalsIgnoreCase(id)) code = 1;
        //etc
        return new Integer(code);
 }  

So basically you are implementing your own comparator which is nothing but assigning certain integer values to the inserted String (which i assume you know already).  
NOTE: this solution will not work, incase you do not catch the option in your returnCode() method. I assume you already know the data that is being fed to the TreeSet.

