当前位置: 代码迷 >> Java Web开发 >> String数组重复项统计,该怎么解决
  详细解决方案

String数组重复项统计,该怎么解决

热度:2066   发布时间:2013-02-25 21:21:42.0
String数组重复项统计
我有一个英文的文本,已经存为了String[],现在想统计这个文本的词频,并且从高到底排序,应该如何写?

------解决方案--------------------------------------------------------
Java code
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();    }}
  相关解决方案