Java 实例 - 查找 List 中的最大最小值


以下实例演示了如何使用 Collections 类的 max() 和 min() 方法来获取List中最大最小值:

  1. /*
  2. author by shouce.ren
  3. Main.java
  4. */
  5.  
  6. import java.util.*;
  7.  
  8. public class Main {
  9. public static void main(String[] args) {
  10. List list = Arrays.asList("one Two three Four five six one three Four".split(" "));
  11. System.out.println(list);
  12. System.out.println("最大值: " + Collections.max(list));
  13. System.out.println("最小值: " + Collections.min(list));
  14. }
  15. }

以上代码运行输出结果为:

  1. [one, Two, three, Four, five, six, one, three, Four]
  2. 最大值: three
  3. 最小值: Four