• 以下是java.util.Collections.min()方法和java.util.Collections.min()的声明
public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> cl) 
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)
  • min和max方法的实现原理都是使用迭代器模式,如下源码:
    /*** Returns the maximum element of the given collection, according to the* <i>natural ordering</i> of its elements.  All elements in the* collection must implement the <tt>Comparable</tt> interface.* Furthermore, all elements in the collection must be <i>mutually* comparable</i> (that is, <tt>e1.compareTo(e2)</tt> must not throw a* <tt>ClassCastException</tt> for any elements <tt>e1</tt> and* <tt>e2</tt> in the collection).<p>** This method iterates over the entire collection, hence it requires* time proportional to the size of the collection.** @param  coll the collection whose maximum element is to be determined.* @return the maximum element of the given collection, according*         to the <i>natural ordering</i> of its elements.* @throws ClassCastException if the collection contains elements that are*         not <i>mutually comparable</i> (for example, strings and*         integers).* @throws NoSuchElementException if the collection is empty.* @see Comparable*/public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) {Iterator<? extends T> i = coll.iterator();T candidate = i.next();while (i.hasNext()) {T next = i.next();if (next.compareTo(candidate) > 0)candidate = next;}
        return candidate;}
    /*** Returns the minimum element of the given collection, according to the* order induced by the specified comparator.  All elements in the* collection must be <i>mutually comparable</i> by the specified* comparator (that is, <tt>comp.compare(e1, e2)</tt> must not throw a* <tt>ClassCastException</tt> for any elements <tt>e1</tt> and* <tt>e2</tt> in the collection).<p>** This method iterates over the entire collection, hence it requires* time proportional to the size of the collection.** @param  coll the collection whose minimum element is to be determined.* @param  comp the comparator with which to determine the minimum element.*         A <tt>null</tt> value indicates that the elements' <i>natural*         ordering</i> should be used.* @return the minimum element of the given collection, according*         to the specified comparator.* @throws ClassCastException if the collection contains elements that are*         not <i>mutually comparable</i> using the specified comparator.* @throws NoSuchElementException if the collection is empty.* @see Comparable*/public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp) {if (comp==null)
            return (T)min((Collection<SelfComparable>) (Collection) coll);Iterator<? extends T> i = coll.iterator();T candidate = i.next();while (i.hasNext()) {T next = i.next();if (comp.compare(next, candidate) < 0)candidate = next;}
        return candidate;}
  • coll-方法参数是实现了Collection接口的集合,Collection接口实现了Iterator接口
  • 示例如下:
        List<String> list = new ArrayList<String>();list.add("a");list.add("b");list.add("c");list.add("d");list.add("1687");list.add("e");String result = Collections.min(list);String result1 = Collections.max(list);System.out.println(result);System.out.println(result1);

输出结果是:

1687
e

上面的实例会按照自然排序输出集合的最小值1687和最大值e

  • Collections中的max和min方法除了按照默认自然排序方式取得最大值和最小值外还可以传入一个比较器Comparator定制比较。
        List<String> list = new ArrayList<String>();list.add("a");list.add("b");list.add("c");list.add("d");list.add("1687");list.add("e");String result = Collections.min(list, new Comparator<String>() {@Overridepublic int compare(String o1, String o2) {return o1.compareTo(o2);}});String result1 = Collections.max(list, new Comparator<String>() {@Overridepublic int compare(String o1, String o2) {return o1.compareTo(o2);}});System.out.println(result);System.out.println(result1);

min和max方法中新增一个比较器可以灵活的定制我们的比较规则。

Collections中min和max工具方法详解相关推荐

  1. python支持向量机回归_Python中支持向量机SVM的使用方法详解

    除了在Matlab中使用PRTools工具箱中的svm算法,Python中一样可以使用支持向量机做分类.因为Python中的sklearn库也集成了SVM算法,本文的运行环境是Pycharm. 一.导 ...

  2. python中验证码连通域分割的方法详解

    python中验证码连通域分割的方法详解 这篇文章主要给大家介绍了关于python中验证码连通域分割的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用python具有一定的参考学习价值,需 ...

  3. php中读取大文件实现方法详解

    php中读取大文件实现方法详解 来源:   时间:2013-09-05 19:27:01   阅读数:6186 分享到:0 [导读] 本文章来给各位同学介绍php中读取大文件实现方法详解吧,有需要了解 ...

  4. linux ipset 流量,linux中ipset命令的使用方法详解

    linux中ipset命令的使用方法详解 发布时间:2020-10-25 17:07:19 来源:脚本之家 阅读:97 作者:lijiaocn 栏目:服务器 ipset介绍 iptables是在lin ...

  5. python怎么横着输出_对python3中, print横向输出的方法详解

    对python3中, print横向输出的方法详解 Python 2 : print打印的时候,如果结尾有逗号,打出来时候不会换行.但是在python3里面就不行了. Python3: 3.0的pri ...

  6. vue ajax highcharts,在vue项目中引入highcharts图表的方法(详解)

    npm进行highchars的导入,导入完成后就可以进行highchars的可视化组件开发了 npm install highcharts --save 1.components目录下新建一个char ...

  7. Linux中history历史命令使用方法详解

    在/etc/profile里添加如下:#History export HISTTIMEFORMAT="[%F %T]" HISTDIR=/home/common/.hist if ...

  8. python更新数据库表的时间字段_python更新数据库中某个字段的数据(方法详解)

    连接数据库基本操作,我把每一步的操作是为什么给大家注释一下,老手自行快进. 请注意这是连接数据库操作,还不是更新. import pymysql #导包 #连接数据库 db = pymysql.con ...

  9. python中update是啥意思_python中update的基本使用方法详解

    前言 Python 字典 update()方法用于更新字典中的键/值对,可以修改存在的键对应的值,也可以添加新的键/值对到字典中. 语法格式 d.update(e) 参数说明 将e中键-值对添加到字典 ...

最新文章

  1. 第十六届全国大学生智能车竞赛赛题规划
  2. Eclipse单元测试Android编程,在Eclipse中进行Android单元测试-Fun言
  3. 机器学习该如何应用到量化投资系列(二)
  4. 联想e480一键恢复小孔_联想IdeaPad 340C评测:3000必入的15寸轻薄本
  5. 推荐 15 款常用开发工具
  6. 【机器学习】贝叶斯学派与频率学派有何不同?
  7. AliCloudDenoise 语音增强算法:助力实时会议系统进入超清音质时代
  8. MarkdownPad安装以及绘制 UML 图
  9. ORACLE使用中的常见 实用的问题
  10. java中的values函数_详解java 中valueOf方法实例
  11. 神通数据库常用命令行
  12. 全国各省市区城市编码SQL
  13. 验房师专用验房项目验收内容
  14. 移动端html网页真机调试,Mac端调试iphone移动端网页
  15. 北京高校毕业生就业突击讲座
  16. 市面上的计算机培训机构 哪家实力最强
  17. ubuntu编辑只读文件
  18. 制作网站标准的目的以及网络营销的影响
  19. LayaAir使用总结
  20. rac节点时间同步方法

热门文章

  1. 斯巴拓压力测力传感器的特色称重模块基本选型要求
  2. NFT Insider #51:YGG举办首届经理峰会,Gotchiverse将于月底推出
  3. 关于百度地图SDK的andriod开发的定位功能实现
  4. 高德地图画带箭头的线_小非带你玩转分镜头脚本
  5. 从外行的视角尝试讲解为什么这回丰田栽了
  6. 移动硬盘格式化数据恢复图文介绍
  7. php浅蓝色英文,浅蓝色HTML5宽屏大气企业模板
  8. QT多线程(三)线程互斥与同步
  9. 七夕情人节礼物:爱情花园 v3.2 bug
  10. 支付宝联合我的天科技 AR技术助力杭州海外参展