Javaexamples-collection-minmax

提供:Dev Guides
移動先:案内検索

Javaの例-リストから最小値と最大値を見つける

問題の説明

リストの最小値と最大値を見つける方法は?

溶液

次の例では、最小および最大メソッドを使用して、リストの最小および最大を見つけます。

import java.util.*;

public class Main {
   public static void main(String[] args) {
      List list = Arrays.asList("one Two three Four five six one three Four".split(" "));
      System.out.println(list);
      System.out.println("max: " + Collections.max(list));
      System.out.println("min: " + Collections.min(list));
   }
}

結果

上記のコードサンプルは、次の結果を生成します。

[one, Two, three, Four, five, six, one, three, Four]
max: three
min: Four