比较器和未知值,使用IncomparableValueException异常。

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

Comparator and unknow value with IncomparableValueException

问题

我使用这个自定义比较器

List<String> currencies = ImmutableList.of("PLN","EUR","USD","GBP", "CHF");
final Ordering<String> currencyOrdering = Ordering.explicit(currencies);

@Override
public int compare(Account o1, Account o2) {
    return currencyOrdering.compare(o1.getCurrency(),o2.getCurrency());
}

但是我遇到了错误

IncomparableValueException: 无法比较值: JPY

我希望这些未知值随机排在最后...
如果可能的话,如何在标准Java库中解决这个问题。

英文:

I use this custom comparator

List&lt;String&gt; currencies = ImmutableList.of(&quot;PLN&quot;,&quot;EUR&quot;,&quot;USD&quot;,&quot;GBP&quot;, &quot;CHF&quot;);
final Ordering&lt;String&gt; currencyOrdering = Ordering.explicit(currencies);

@Override
public int compare(Account o1, Account o2) {
    return currencyOrdering.compare(o1.getCurrency(),o2.getCurrency());
}

But I have error

IncomparableValueException: Cannot compare value: JPY

I would like these unknown values ​​to be randomly at the end...
If possible, how to solve it in standard Java library.

答案1

得分: 3

你可以这样做:

使用Guava库:

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Ordering;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> currencies = ImmutableList.of("OTHERS", "PLN", "EUR", "USD", "GBP", "CHF");
        final Ordering<String> currencyOrdering = Ordering.explicit(currencies).onResultOf(lang -> currencies.contains(lang) ? lang : "OTHERS");
        System.out.println(currencyOrdering.compare("JPY", "PLN")); // -1
        System.out.println(currencyOrdering.compare("JPY", "TTT")); // 0
        System.out.println(currencyOrdering.compare("CHF", "GBP")); // 1
    }
}

不使用Guava库:

import java.util.Comparator;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> currencies = List.of("PLN", "EUR", "USD", "GBP", "CHF");
        Comparator<Object> objectComparator = Comparator.comparingInt(currencies::indexOf);
        System.out.println(objectComparator.compare("JPY", "PLN"));
        System.out.println(objectComparator.compare("JPY", "TTT"));
        System.out.println(objectComparator.compare("CHF", "GBP"));
    }
}
英文:

What you can do :

    List&lt;String&gt; currencies = ImmutableList.of(&quot;OTHERS&quot;, &quot;PLN&quot;,&quot;EUR&quot;,&quot;USD&quot;,&quot;GBP&quot;, &quot;CHF&quot;);
    final Ordering&lt;String&gt; currencyOrdering = Ordering.explicit(currencies).onResultOf(lang -&gt; currencies.contains(lang) ? lang : &quot;OTHERS&quot;);
    System.out.println(currencyOrdering.compare(&quot;JPY&quot;, &quot;PLN&quot;)); # -1
    System.out.println(currencyOrdering.compare(&quot;JPY&quot;, &quot;TTT&quot;)); # 0
    System.out.println(currencyOrdering.compare(&quot;CHF&quot;, &quot;GBP&quot;)); # 1

Basically, I ask Guava to map the languages to their own value if they are in currencies list, otherwise I just give them the value OTHERS which is the first one in the list (so everything not in the list is put at the start of the list)

Note : you can do it without guava with :

    List&lt;String&gt; currencies = List.of(&quot;PLN&quot;,&quot;EUR&quot;,&quot;USD&quot;,&quot;GBP&quot;, &quot;CHF&quot;);
    Comparator&lt;Object&gt; objectComparator = Comparator.comparingInt(currencies::indexOf);
    System.out.println(objectComparator.compare(&quot;JPY&quot;, &quot;PLN&quot;));
    System.out.println(objectComparator.compare(&quot;JPY&quot;, &quot;TTT&quot;));
    System.out.println(objectComparator.compare(&quot;CHF&quot;, &quot;GBP&quot;));

Basically, I specify that the "rank" on item is its position in the list. OTHERS is not needed since missing element will have the value -1

huangapple
  • 本文由 发表于 2020年10月1日 21:58:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64156948.html
匿名

发表评论

匿名网友

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

确定