如何比较输入文本中的单词长度并根据长度值对它们进行排序

时间:2014-03-19 22:16:31

标签: java sorting hashmap

我有一串我希望根据长度排序的单词。有些单词不止一次重复,所以我使用HashMap返回单词及其计数:

Map<String, Integer> map = new HashMap<String, Integer>();

for (String s : fragments) {
    if (map.containsKey(s)) {
        map.put(s, map.get(s) + 1);
    } else {
        map.put(s, 1);
    }

}

如何使用它来返回每个单词的长度并将它们从最长到最短排序?

编辑: 例如,如果输入是字符串

"From the torrent, or the fountain,From the red cliff of the mountain"

我做了一个简单的循环:

for (int i = 0; i < fragments.length; i++) {
    System.out.println(fragments[i]+" " +fragments[i].length());
}

输出为:(完全输出re:@Pshemo)

from 4
the 3
or 2
the 3
the 3
red 3
cliff 5
of 2
the 3
mountain 8

等等。它没有被排序,每次看到这个单词时都会重复“the”这样的单词。我想不重复重复,并将单词从最长到最短排序。

我想要的输出应该是:

mountain 8
cliff 5
from 4
the 3
red 3
of 2
or 2

1 个答案:

答案 0 :(得分:2)

使用自定义比较器:

public static void main(String[] args) {
    String phrase = "From the torrent, or the fountain, From the red cliff of the mountain";
    //or whatever method of getting the individual words you want.
    //I just chose StringTokenizer (obviously)
    StringTokenizer st = new StringTokenizer(phrase, " ,");
    TreeSet<String> ts = new TreeSet<>(new Comparator<String>() {

        @Override
        public int compare(String s1, String s2) {
            int retVal = Integer.compare(s1.length(), s2.length());
            if(retVal == 0){
                retVal = s1.compareTo(s2);
            }
            return retVal;
        }
    });
    while(st.hasMoreTokens()){
        ts.add(st.nextToken());
    }
    for(String s : ts.descendingSet()){
        System.out.println(s + " " + s.length());
    }
}

它甚至可以按字母顺序排列相同长度的单词: - )