Sorting a list by the modulus of 3 The input is an unsorted array of integers. The output should be an array of integers sorted based on the modulus of 3 (or any other number).

It begins with all integers conforming to Integer % 3 == 0 then 1 and then 2.

public class sortByMod {
public static void main(String args[]) throws Exception {

    int a[] = {1,90,2100, 12, 24, 80, 90, 5};
    LinkedList&lt;List&lt;Integer&gt;&gt; a1=sortMod(a);
    for(int i=0; i &lt;a1.size();i++)
            System.out.print(a1.get(i)+" ");
}

public static LinkedList sortMod(int a[]){
    HashMap&lt;Integer, TreeSet&lt;Integer&gt;&gt; map = new HashMap&lt;Integer, TreeSet&lt;Integer&gt;&gt;();
    for(Integer i =0; i&lt;a.length;i++){
        Integer modKey = a[i]%3;
        if(!map.containsKey(modKey)){
            map.put(modKey, new TreeSet&lt;Integer&gt;());
        }
        TreeSet&lt;Integer&gt; temp = map.get(modKey);
        temp.add(a[i]);
        map.put(modKey,temp);
    }
    LinkedList&lt;List&lt;Integer&gt;&gt; result= new LinkedList&lt;List&lt;Integer&gt;&gt;();
    TreeSet&lt;Integer&gt; temp;
    for(int i=0;i&lt;=2;i++) {

        if(map.containsKey(i)){
           temp = map.get(i);
        }
        else return null;

        List&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;(temp);
        result.add(list);

    }
    return result;
}
}
Your code definitely doesn't work with any number except 3, and it's quite likely you'll make an error if you'll be changing the code to another number. So I'd use 

public LinkedList sortMod(int a[], int number)


instead. Also, the code returns null when there's no numbers of any modulus. I.e. sortMod(new int[]{1}) returns null as well as sortMod(new int[]{1, 4, 7, 10, 13}).

You can also try to use Streams, feature of Java 8. For example, the single line

return map.values().stream().map(ArrayList::new).collect(Collectors.toList());


does a lot of job, effectively replacing 10 lines of your code. You can try to make the task in two lines using Java 8 features. Also it works fine with sortMod(new int[]{1, 4, 7, 10, 13}).