import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class MapSort {
public static void main(String[] args) {
String str = "adf adf ad adf adf ad ad f ad ad";
String[] items = str.split(" ");
Map
for (String s : items) {
if (map.containsKey(s))
map.put(s, map.get(s) + 1);
else {
map.put(s, 1);
}
}
List
for (Entry
list.add(entry);
}
Collections.sort(list, new EntryComparator());
for (Entry
System.out.println(obj.getKey() + "\t" + obj.getValue());
}
}
}
class EntryComparator implements Comparator
public int compare(Entry
return o1.getValue() > o2.getValue() ? 0 : 1;
}
}
参数你可以自己修改!
单词从哪来?用户输入?