开发者社区> 问答> 正文

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

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

展开
收起
问问小秘 2020-02-13 17:30:02 999 0
1 条回答
写回答
取消 提交回答
  • 以下实例演示了如何使用 Collections 类的 max() 和 min() 方法来获取List中最大最小值:

    Main.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("最大值: " + Collections.max(list));
          System.out.println("最小值: " + Collections.min(list));
       }
    }
    
    

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

    [one, Two, three, Four, five, six, one, three, Four]
    最大值: three
    最小值: Four
    
    2020-02-13 17:30:22
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载