1. 程式人生 > >Java Collections的min和max方法

Java Collections的min和max方法

方法一

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;
    }
}
轉載於:http://www.cnblogs.com/stonesingsong/p/6548894.html