英文:
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<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());
}
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<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
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<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"));
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论