String数组重复项统计
我有一个英文的文本,已经存为了String[],现在想统计这个文本的词频,并且从高到底排序,应该如何写?
[解决办法]
public class Test { public static void main(String[] args) { String[] strs = new String[]{"a","b","a"}; Map<String, Integer> map = new HashMap<String, Integer>(); for(String s : strs){ Integer key = map.get(s); if(key == null){ map.put(s, 1); }else{ map.put(s, key.intValue() + 1); } } Set<Entry<String, Integer>> set = map.entrySet(); List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(); Iterator<Entry<String, Integer>> iter = set.iterator(); while(iter.hasNext()){ list.add(iter.next()); } Collections.sort(list, new ComparatorEntry()); for(Entry<String, Integer> entry : list){ System.out.println(entry.getKey() + " : " + entry.getValue()); } }}class ComparatorEntry implements Comparator<Entry<String, Integer>>{ @Override public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { return o2.getValue().intValue() - o1.getValue().intValue(); }}