前提・実現したいこと
Java8にて自クラスをvalueに持つMapのvalueのプロパティでソートを行いたいです。
<String, String>などでは参考記事がありましたが、
<String, MyClass>のように自クラスが値として定義されている場合、
sortedにはどのように引数を定義すれば良いのでしょうか?
単純にcomparingByValue()を指定した場合のエラーメッセージ
型 Stream<Map.Entry<String,MyClass>> のメソッド sorted(Comparator<? super Map.Entry<String,MyClass>>) は引数 (Comparator<Map.Entry<Object,Comparable<? super Comparable<? super V>>>>) に適用できません
該当のソースコード
エラーなく定義できる例
Map<String, String> map = new HashMap<>(); map.entrySet().stream() .sorted(java.util.Map.Entry.comparingByValue()) .forEach(System.out::println);
自クラスをMapに定義した場合
Map<String, MyClass> map = new HashMap<>(); map.entrySet().stream() .sorted(java.util.Map.Entry.comparingByValue()) .forEach(System.out::println);

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/03 03:26
2018/12/03 03:49