我正在尝试根据哈希图中每个键的出现次数对程序的输出进行排序。我希望它以出现的递增顺序打印出来。它已经正确打印出来了,只是没有按顺序打印。我查看了此页面Sort a Map<Key, Value> by values但对于哪些仍然有效,哪些无效,这确实令人困惑。
哈希图 -
Map<String, NumberHolder> uaCount = new HashMap<String, NumberHolder>();
印刷部分
for(String str : uaCount.keySet())
{
String [] arr = str.split(":");
long average = uaCount.get(str).sumtime_in_milliseconds / uaCount.get(str).occurrences;
System.out.println(arr[0] + " ---> " + arr[1] + "---> " + arr[2] + "--->" + arr[3] + "\nAverage = " + average + " milliseconds \nOccurrences = " + uaCount.get(str).occurrences);
}
我的NumberHolder
班级定义
public static class NumberHolder
{
public int occurrences = 0;
public int sumtime_in_milliseconds = 0;
}