ラムダ式で任意の順番で値を取り出したい
現在昇順で並び替えをして出力しているのですが、
任意の順番で値を取り出したい場合はどうした方がよろしいでしょうか。
該当のソースコード
Java
1 private <T> List<Method> getMethods(Class<T> cls) { 2 3Comparator<Method> sort = (o1, o2) -> { 4 5 Name a1 = o1.getDeclaredAnnotation(Name.class); 6 Name a2 = o2.getDeclaredAnnotation(Name.class); 7 8 boolean checkA1 = a1 != null && a1.value() != null && a1.value().length() != 0; 9 boolean checkA2 = a2 != null && a2.value() != null && a2.value().length() != 0; 10 11 int result = 0; 12 if (check && check) { 13 result = a1.value().compareTo(a2.value()); 14 } else if (!checkA1 && !checkA2) { 15 result = 0; 16 } else if (!checkA1) { 17 result = 1; 18 } else if (!checkA2) { 19 result = -1; 20 } 21 22 return result; 23 }; 24 25 List<Method> methods = Arrays.stream(cls.getDeclaredMethods()) 26 .filter(method -> method.getName().startsWith("get")) 27 .filter(method -> method.getDeclaredAnnotation(Item.class) == null).sorted(sort) 28 .collect(Collectors.toList()); 29 30
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。