4
アルファベット順の値に基づいてマップをソートし、大文字と小文字の区別を無視してキーのリストを返したいとします。Java-8:大文字と小文字の区別を無視してMap.Entry#comparisonByValueを使用して(値に基づいて)マップをソートする方法?
/**
* This method will sort allCostPlanDetailsRows based on Value in <Key,Value> pair
*
* @param sortingOrder_ whether to sort the LinkedHashMap in Ascending order or Descending order
* @return List<String> returns List of costPlanDetailsRowId in sorted order
*/
private List<String> sortCostPlanDetailRows(SortingOrder sortingOrder_)
{
return _allCostPlanDetailRows
.entrySet()
.stream()
.sorted(sortingOrder_ == SortingOrder.DESC ? Map.Entry.<String, String>comparingByValue(Comparator.nullsFirst(Comparator.naturalOrder())).reversed() : Map.Entry.comparingByValue(Comparator.nullsFirst(
Comparator.naturalOrder())))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}
どうすればこの問題を解決できますか?
注:上記のコードを改善することができれば歓迎されます。
おかげで、それは同様にのみNULL値または他のすべての値に適用できるのでしょうか? –
'Comparator.nullsFirst'を使用しているので、' null'が最初にあります。 'null'以外の文字列はすべて、大文字と小文字を区別せずにソートされます。 – Mureinik
ありがとうございました:) –