As mentioned in the previous comments you can have a sorted map with Integer and ArrayList parameters as below,
SortedMap<Integer, ArrayList<String>> map=new TreeMap<Integer, ArrayList<String>>().descendingMap();
Since you want the results to be in descending order I added descendingMap().
We use ArrayList because you may have duplicates in count array. More than one name can have the same count value. So one count will have a list of names.
And the complete code is below,
import java.util.ArrayList;
import java.util.SortedMap;
import java.util.TreeMap;
public class NameCount {
public static void main(String[] args) {
String[] names = {"James","Loiui","Mario","Peter"};
int[] count = {1, 4, 2, 1};
SortedMap<Integer, ArrayList<String>> map=new TreeMap<Integer, ArrayList<String>>().descendingMap();
ArrayList<String> nameList=null;
for(int i = 0; i < names.length; i++)
{
if(map.get(count[i])==null)
{
nameList = new ArrayList<String>();
nameList.add(names[i]);
map.put(count[i], nameList);
}
else
{
map.get(count[i]).add(names[i]);
}
}
for(int countVal : map.keySet())
{
ArrayList<String> namesListVal=map.get(countVal);
System.out.print("\nCount "+countVal+ ": NAMES : ");
for(String name : namesListVal)
{
System.out.print(name+" ");
}
}
}
}
SortedMap<String, Integer>. However if your goal is to sort by count, use aSortedMap<Integer, List<String>>.SortedMap<Integer, List<String>>, as I said.