「Java」ComparatorクラスのnaturalOrder()とreverseOrder()のサンプル
説明
naturalOrder()
自然な順序
reverseOrder()
自然順序付けの逆
Javaコード
package com.arkgame.study.mvc; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; public class ComparseableDemo { private static Integer dig[] = { 21, 32, 45, 67, 9,39 }; public static void main(String[] args) { List<Integer> cftLst = Arrays.asList(dig); //昇順 List<Integer> resLstA = cftLst.stream() .sorted(Comparator.naturalOrder()) .collect(Collectors.toList()); //降順 List<Integer>resLstB =cftLst.stream() .sorted(Comparator.reverseOrder()) .collect(Collectors.toList()); System.out.println("list element: " +cftLst); System.out.println("list element sorted 昇順 : " +resLstA); System.out.println("list element sorted 降順 : " +resLstB); } }
結果
list element: [21, 32, 45, 67, 9, 39]
list element sorted 昇順 : [9, 21, 32, 39, 45, 67]
list element sorted 降順 : [67, 45, 39, 32, 21, 9]