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<List<Integer>> a1=sortMod(a);    for(int i=0; i <a1.size();i++)            System.out.print(a1.get(i)+ );}public static LinkedList sortMod(int a[]){    HashMap<Integer, TreeSet<Integer>> map = new HashMap<Integer, TreeSet<Integer>>();    for(Integer i =0; i<a.length;i++){        Integer modKey = a[i]%3;        if(!map.containsKey(modKey)){            map.put(modKey, new TreeSet<Integer>());        }        TreeSet<Integer> temp = map.get(modKey);        temp.add(a[i]);        map.put(modKey,temp);    }    LinkedList<List<Integer>> result= new LinkedList<List<Integer>>();    TreeSet<Integer> temp;    for(int i=0;i<=2;i++) {        if(map.containsKey(i)){           temp = map.get(i);        }        else return null;        List<Integer> list = new ArrayList<Integer>(temp);        result.add(list);    }    return result;}}
Sorting a list by the modulus of 3
java;sorting;set