TreeMap按value值进行排序时间:2025-04-04 07:56:12import ; import ; import ; import ; import ; import ; import ; import ; public class CountAccurrenceOfWord { public static void main(String[] args) { String text = "Have a good day. Have a good class. Have a good visit. Have fun"; Map<String, Integer> hashMap = new HashMap<String, Integer>(); String[] words = ("[ .!?]"); for (int i = 0; i < ; i++) { if (words[i].length() > 1) { if ((words[i]) != null) { int value = (words[i]).intValue(); value++; (words[i], value); } else (words[i], 1); } } Map<String, Integer> treeMap = new TreeMap<String, Integer>(hashMap); .println("Dispaly words and their count in ascending order of the word"); (treeMap); List arrayList = new ArrayList(()); (arrayList, new Comparator(){ public int compare(Object o1, Object o2) { obj1 = () o1; obj2 = () o2; return ((Integer) ()).compareTo((Integer)()); } }); (arrayList); } }