方法一

public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll)

此方法需要传入一个实现了Comparable接口的对象类的集合

创建实现了Comparable的对象类

public class Student1 implements Comparable<Student1> {

    private String name;
private int age; public Student1() {
} public Student1(String name, int age) {
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} @Override
public int compareTo(Student1 s) {
int num = this.age - s.age;
int num1 = (num == 0 ? this.name.compareTo(s.name) : num);
return num1;
}
}

调用

import java.util.ArrayList;
import java.util.Collections;
import java.util.List; public class Student1Test {
public static void main(String[] args) {
List<Student1> list1 = new ArrayList<Student1>();
list1.add(new Student1("林青霞", 27));
list1.add(new Student1("风清扬", 30));
list1.add(new Student1("刘晓曲", 28));
list1.add(new Student1("武鑫", 29));
list1.add(new Student1("林青霞", 27)); Student1 min=Collections.min(list1);
System.out.println(min.getName()+"---"+min.getAge());
Student1 max=Collections.max(list1);
System.out.println(max.getName()+"---"+max.getAge());
}
}

方法二

public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp)

此方法传入一个对象类的集合,以及一个比较器

创建对象类

public class Student2 {

    private String name;
private int age; public Student2() {
} public Student2(String name, int age) {
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}

调用

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; public class Student2Test {
public static void main(String[] args) {
List<Student2> list2 = new ArrayList<Student2>();
list2.add(new Student2("林青霞", 27));
list2.add(new Student2("风清扬", 30));
list2.add(new Student2("刘晓曲", 28));
list2.add(new Student2("武鑫", 29));
list2.add(new Student2("林青霞", 27)); Student2 min=Collections.min(list2,new MyComparator());
System.out.println(min.getName()+"---"+min.getAge());
Student2 max=Collections.max(list2,new MyComparator());
System.out.println(max.getName()+"---"+max.getAge());
}
} class MyComparator implements Comparator<Student2> {
@Override
public int compare(Student2 s1, Student2 s2) {
int num = s1.getAge() - s2.getAge();
int num1 = (num == 0 ? s1.getName().compareTo(s2.getName()) : num);
return num1;
}
}

Java中Collections的min和max方法的更多相关文章

  1. 5.7.2.2 min()和max()方法

    Math对象还包含许多方法,用于辅助完成简单和复杂的数学计算. 其中,min()和max()方法用于确定一组数值中的最小值和最大值.这两个方法都可以接受任意多个数值参数,如下例子: var max = ...

  2. 是什么让.NET7的Min和Max方法性能暴增了45倍?

    简介 在之前的一篇文章.NET性能系列文章一:.NET7的性能改进中我们聊到Linq中的Min()和Max()方法.NET7比.NET6有高达45倍的性能提升,当时Benchmark代码和结果如下所示 ...

  3. java中获取日期和时间的方法总结

    1.获取当前时间,和某个时间进行比较.此时主要拿long型的时间值. 方法如下:  要使用 java.util.Date .获取当前时间的代码如下 Date date = new Date(); da ...

  4. java中遍历map对象的多种方法

    在Java中如何遍历Map对象   How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有ma ...

  5. java中常用的字符串的截取方法

    java中常用的字符串的截取方法   1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int l ...

  6. Java中的五种单例模式实现方法

    [代码] Java中的五种单例模式实现方法   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 2 ...

  7. 谈谈java中遍历Map的几种方法

    java中的map遍历有多种方法,从最早的Iterator,到java5支持的foreach,再到java8 Lambda,让我们一起来看下具体的用法以及各自的优缺点 先初始化一个map public ...

  8. Java中产生随机数的两个方法

    Java中产生随机数的两个方法 一.利用random方法来生成Java随机数. 在Java语言中生成Java随机数相对来说比较简单,因为有一个现成的方法可以使用.在Math类中,Java语言提供了一个 ...

  9. Java中创建数组的几种方法

    Java中创建数组的几种方法 public static void main(String[] args) { //创建数组的第一种方法 int[] arr=new int[6]; int intVa ...

随机推荐

  1. 表空间移动(transporting tablespaces)

    --表空间移动(transporting tablespaces) --------------------------------------2014/01/15   1. 表空间传输步骤简介.   ...

  2. 轻松学JVM(四)——垃圾回收算法

    我们都知道java语言与C语言最大的区别就是内存自动回收,那么JVM是怎么控制内存回收的,这篇文章将介绍JVM垃圾回收的几种算法,从而了解内存回收的基本原理. stop the world 在介绍垃圾 ...

  3. webgl自学笔记——矩阵变换

    这章主要探讨矩阵,这些矩阵代表了应用在我们场景上的变换,允许我们移动物体.然而在webGL api中并没有一个专门的camera对象,只有矩阵.好消息是使用矩阵来取代相机对象能让webgl在很多复杂动 ...

  4. HDU 6170----Two strings(DP)

    题目链接 Problem Description Giving two strings and you should judge if they are matched.The first strin ...

  5. 我学习go的五个感悟(译)

    我学习go的五个感悟(译) 原文 5 things about programming I learned with Go By MICHAŁ KONARSKI Go在最近一段时间内开始变得十分流行. ...

  6. EasuUI-js(EasyUI formatter格式化教程) 判断常用:

    EasuUI-js(EasyUI formatter格式化教程) 判断常用: 1.YN标记: formatter : function(c1, row,index) { var a = "& ...

  7. VMware Workstation 12 Pro 之安装林耐斯-LinuxMint-系统

    VMware Workstation 12 Pro 之安装林耐斯-LinuxMint-系统... ---------------------- ---------------------------- ...

  8. return 的返回值与结束功能

    前言:大家好~我是阿飞~在js中return是很重要的基础.一定要彻底掌握理解它哦.否则js学习到中后期很容易懵逼的+_+ 什么是return? 1.在js中return是一个表达式语句,如果后面什么 ...

  9. react后台开发框架搭建

    最近整理了一下自己在用的react框架,主要涉及到的技术有react react-router redux  Es6 webpack less ant-design等技术,可用于快速开发后台类系统. ...

  10. Ubuntu & GitLab CI & Docker & ASP.NET Core 2.0 自动化发布和部署(1)

    相关博文: Ubuntu 简单安装和配置 GitLab Ubuntu 简单安装 Docker Ubuntu Docker 简单安装 GitLab Ubuntu Docker 安装和配置 GitLab ...