如何使比较器正常工作,目前它给出了错误长度的字符串。

huangapple go评论59阅读模式
英文:

How to make comparator work correctly, now it gives wrong length of strings

问题

以下是翻译好的部分:

我有这些单词存储在一个 txt 文件中

jaguar chimpanzee bison whale
marmot bison lemur panther camel lizard wolf bear
gecko
mongoose leopard sable sable dingo whale jaguar
rat lemur lemur gorilla zebra tortoise 
asp lion tapir tortoise gorilla cheetah bison marten
marmot cheetah camel
snake marmot
zebra asp cheetah lizard gecko gorilla asp lion tortoise
kangaroo whale penguin yak cheetah mouse panther

我需要找出其中最长的 3 个单词并将其放入一个映射中
Comparator 类如下所示

private static class SortByValue implements Comparator<String> {

    public SortByValue(Map<String, Integer> map) {
        this.map = map;
    }

    private Map<String, Integer> map;

    @Override
    public int compare(String o1, String o2) {

        return (map.get(o1) >= map.get(o2)) ? -1 : ((map.get(o1) == map.get(o2)) ? 0 : 1);
    }
}

在我这里没有显示的所有方法之后的输出应为数组中最长的前三个字符串):

chimpanzee ==> 10

mongoose ==> 8

tortoise ==> 8

但是比较器会产生错误的结果无法通过考试):

chimpanzee ==> 10

tortoise ==> 8

kangaroo ==> 8

错误出在哪里请帮忙解决
英文:

I have these words in a txt file:

jaguar chimpanzee bison whale
marmot bison lemur panther camel lizard wolf bear
gecko
mongoose leopard sable sable dingo whale jaguar
rat lemur lemur gorilla zebra tortoise
asp lion tapir tortoise gorilla cheetah bison marten
marmot cheetah camel
snake marmot
zebra asp cheetah lizard gecko gorilla asp lion tortoise
kangaroo whale penguin yak cheetah mouse panther

I need to find the 3 longest words and put it in a map.
Comparator class is:

private static class SortByValue implements Comparator&lt;String&gt;{

    public SortByValue(Map&lt;String, Integer&gt; map) {
        this.map = map;
    }

    private Map&lt;String, Integer&gt; map;

    @Override
    public int compare(String o1, String o2) {

        return (map.get(o1) &gt;= map.get(o2)) ? -1 : ((map.get(o1) == map.get(o2)) ? 0 : 1);
    }

}

The output after all methods I don't show here should be (the first longest strings in array):

chimpanzee ==&gt; 10

mongoose ==&gt; 8

tortoise ==&gt; 8

But comparator makes it (wrong, cant pass exam):

chimpanzee ==&gt; 10

tortoise ==&gt; 8

kangaroo ==&gt; 8

Where is the mistake? Help please

答案1

得分: 1

这是如何创建一个比较器,可以按长度对字符串进行排序,优先排列最长的字符串:

private static class SortByValue implements Comparator<String> {
    @Override
    public int compare(String o1, String o2) {
        return Integer.compare(o2.length(), o1.length());
    }
}

应用于您的字符串列表,前三个输出将是 chimpanzee、mongoose 和 tortoise。

英文:

Here is how you can make a Comparator that sorts strings by length, longest first:

private static class SortByValue implements Comparator&lt;String&gt;{
    @Override
    public int compare(String o1, String o2) {
        return Integer.compare(o2.length(), o1.length());
    }
}

Applied to your list of strings, the first 3 words that come out will be chimpanzee, mongoose, tortoise.

huangapple
  • 本文由 发表于 2020年8月21日 05:29:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63513365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定