# Java-如何对TreeMap进行排序

This is my code below, and currently, the output is unsorted. I've tried implementing the method from this link, https://beginnersbook.com/2014/07/how-to-sort-a-treemap-by-value-in-java/, but got the following error:

``````public static void main(String[] args){
Scanner input = new Scanner(System.in);

ArrayList<Integer> Digits = new ArrayList<>();
ArrayList<Integer> UniqueDigits = new ArrayList<>();
System.out.print("Enter digits: ");
int value;

do {
value = input.nextInt();
if (value > 0 && value < 101)
} while (value != 0);

Set<Integer> uniqueDigits = new HashSet<Integer>(Digits);
Map<Integer, Integer> frequencies = new HashMap<Integer, Integer>();
for (Integer uniqueDigit : uniqueDigits) {
frequencies.put(uniqueDigit, Collections.frequency(Digits, uniqueDigit));
}

// Printing
for (Integer key : frequencies.keySet()) {

if (frequencies.get(key) > 1) {
System.out.println(key + " - " + frequencies.get(key) + " times");
} else if (frequencies.get(key) == 1) {
System.out.println(key + " - " + frequencies.get(key) + " time");
}
}
}
``````

}