如何从Collection中获取最大值(例如ArrayList)?

use*_*399 121 java

有一个存储整数值的ArrayList.我需要在此列表中找到最大值.例如,假设arrayList存储的值是:10, 20, 30, 40, 50并且最大值将是50.

找到最大值的有效方法是什么?

@Edit:我刚刚发现了一个我不太确定的解决方案

ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(100); /* add(200), add(250) add(350) add(150) add(450)*/

Integer i = Collections.max(arrayList)
Run Code Online (Sandbox Code Playgroud)

这会返回最高值.

另一种比较每个值的方法,例如 selection sort or binary sort algorithm  

got*_*ers 266

您可以使用它Collections API来轻松实现您想要的 - 有效读取 - 足够的 Javadoc for Collections.max

Collections.max(arrayList);
Run Code Online (Sandbox Code Playgroud)

根据元素的自然顺序返回给定集合的最大元素.集合中的所有元素都必须实现Comparable接口.

  • 为什么这是公认的答案?这不是**最有效的解决方案.最好的情况,它是O(n log(n))并通过检查它们来选择最大值仅为O(n) (6认同)

Rob*_*inn 28

这个问题差不多用了一年,但我发现如果你为对象创建一个自定义比较器,你可以使用Collections.max作为对象的数组列表.

import java.util.Comparator;

public class compPopulation implements Comparator<Country> {
    public int compare(Country a, Country b) {
        if (a.getPopulation() > b.getPopulation())
            return -1; // highest value first
        if (a.getPopulation() == b.Population())
            return 0;
        return 1;
    }
}
ArrayList<Country> X = new ArrayList<Country>();
// create some country objects and put in the list
Country ZZ = Collections.max(X, new compPopulation());
Run Code Online (Sandbox Code Playgroud)

  • 感谢您分享您对比较器的了解.你的帖子让我写了一篇博客文章:http://fenon.de/kleinsten-und-groessten-wert-einer-arraylist-ermitteln/ (2认同)

小智 18

public int getMax(ArrayList list){
    int max = Integer.MIN_VALUE;
    for(int i=0; i<list.size(); i++){
        if(list.get(i) > max){
            max = list.get(i);
        }
    }
    return max;
}
Run Code Online (Sandbox Code Playgroud)

根据我的理解,这基本上是Collections.max()所做的,尽管它们使用比较器,因为列表是通用的.


Bha*_*hah 13

我们可以简单地使用Collections.max()Collections.min()方法.

public class MaxList {
    public static void main(String[] args) {
        List l = new ArrayList();
        l.add(1);
        l.add(2);
        l.add(3);
        l.add(4);
        l.add(5);
        System.out.println(Collections.max(l)); // 5
        System.out.println(Collections.min(l)); // 1
    }
}
Run Code Online (Sandbox Code Playgroud)


Kic*_*ski 7

Comparator.comparing

在Java 8中,使用lambda增强了集合.因此,使用以下方法可以实现以下最大值和最小值Comparator.comparing:

码:

List<Integer> ints = Stream.of(12, 72, 54, 83, 51).collect(Collectors.toList());
System.out.println("the list: ");
ints.forEach((i) -> {
    System.out.print(i + " ");
});
System.out.println("");
Integer minNumber = ints.stream()
        .min(Comparator.comparing(i -> i)).get();
Integer maxNumber = ints.stream()
        .max(Comparator.comparing(i -> i)).get();

System.out.println("Min number is " + minNumber);
System.out.println("Max number is " + maxNumber);
Run Code Online (Sandbox Code Playgroud)

输出:

 the list: 12 72 54 83 51  
 Min number is 12 
 Max number is 83
Run Code Online (Sandbox Code Playgroud)


Avi*_*kar 7

Integer类实现Comparable.So我们可以轻松获取Integer列表的最大值或最小值.

public int maxOfNumList() {
    List<Integer> numList = new ArrayList<>();
    numList.add(1);
    numList.add(10);
    return Collections.max(numList);
}
Run Code Online (Sandbox Code Playgroud)

如果一个类没有实现Comparable,我们必须找到max和min值,那么我们必须编写自己的Comparator.

List<MyObject> objList = new ArrayList<MyObject>();
objList.add(object1);
objList.add(object2);
objList.add(object3);
MyObject maxObject = Collections.max(objList, new Comparator<MyObject>() {
    @Override
    public int compare(MyObject o1, MyObject o2) {
        if (o1.getValue() == o2.getValue()) {
            return 0;
        } else if (o1.getValue() > o2.getValue()) {
            return -1;
        } else if (o1.getValue() < o2.getValue()) {
            return 1;
        }
        return 0;
    }
});
Run Code Online (Sandbox Code Playgroud)


Rei*_*ica 5

没有特别有效的方法来查找未排序列表中的最大值 - 您只需要检查它们并返回最高值.