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

有一个存储整数值的ArrayList。 我需要在此列表中找到最大值。 例如。 假设arrayList存储的值是:selection sort or binary sort algorithm和最大值  值将是50。

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

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

ArrayList arrayList = new ArrayList();

arrayList.add(100); /* add(200), add(250) add(350) add(150) add(450)*/

Integer i = Collections.max(arrayList)

这会返回最高值。

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

user1010399 asked 2019-07-01T17:26:14Z

13个解决方案

256 votes

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

Collections.max(arrayList);

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

gotomanners answered 2019-07-01T17:26:36Z

27 votes

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

import java.util.Comparator;

public class compPopulation implements Comparator {

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 X = new ArrayList();

// create some country objects and put in the list

Country ZZ = Collections.max(X, new compPopulation());

Robert Quinn answered 2019-07-01T17:27:03Z

18 votes

public int getMax(ArrayList list){

int max = Integer.MIN_VALUE;

for(int i=0; i

if(list.get(i) > max){

max = list.get(i);

}

}

return max;

}

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

John answered 2019-07-01T17:27:27Z

13 votes

我们可以简单地使用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

}

}

Bhavin Shah answered 2019-07-01T17:27:54Z

7 votes

Comparator.comparing

在Java 8中,使用lambda增强了集合。 因此,使用Comparator.comparing可以如下完成查找最大值和最小值:

码:

List 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);

输出:

the list: 12 72 54 83 51

Min number is 12

Max number is 83

Kick Buttowski answered 2019-07-01T17:28:34Z

6 votes

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

public int maxOfNumList() {

List numList = new ArrayList<>();

numList.add(1);

numList.add(10);

return Collections.max(numList);

}

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

List objList = new ArrayList();

objList.add(object1);

objList.add(object2);

objList.add(object3);

MyObject maxObject = Collections.max(objList, new Comparator() {

@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;

}

});

Avijit Karmakar answered 2019-07-01T17:29:07Z

5 votes

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

Brendan Long answered 2019-07-01T17:29:34Z

4 votes

以下是使用流来查找列表中最大值的另外三种方法:

List nums = Arrays.asList(-1, 2, 1, 7, 3);

Optional max1 = nums.stream().reduce(Integer::max);

Optional max2 = nums.stream().max(Comparator.naturalOrder());

OptionalInt max3 = nums.stream().mapToInt(p->p).max();

System.out.println("max1: " + max1.get() + ", max2: "

+ max2.get() + ", max3: " + max3.getAsInt());

所有这些方法,就像Collections.max一样,迭代整个集合,因此它们需要与集合大小成比例的时间。

Ida Bucić answered 2019-07-01T17:30:09Z

2 votes

Java 8

由于整数可比,我们可以使用以下一个班轮:

List ints = Stream.of(22,44,11,66,33,55).collect(Collectors.toList());

Integer max = ints.stream().mapToInt(i->i).max().orElseThrow(NoSuchElementException::new); //66

Integer min = ints.stream().mapToInt(i->i).min().orElseThrow(NoSuchElementException::new); //11

需要注意的另一点是我们不能使用Funtion.identity()代替i->i作为mapToInt预期ToIntFunction这是一个完全不同的接口并且与Function无关。而且这个接口只有一种方法applyAsInt而没有identity()方法。

i_am_zero answered 2019-07-01T17:30:47Z

1 votes

这是功能

public int getIndexOfMax(ArrayList arr){

int MaxVal = arr.get(0); // take first as MaxVal

int indexOfMax = -1; //returns -1 if all elements are equal

for (int i = 0; i < arr.size(); i++) {

//if current is less then MaxVal

if(arr.get(i) < MaxVal ){

MaxVal = arr.get(i); // put it in MaxVal

indexOfMax = i; // put index of current Max

}

}

return indexOfMax;

}

SAM answered 2019-07-01T17:31:18Z

1 votes

package in.co.largestinarraylist;

import java.util.ArrayList;

import java.util.Scanner;

public class LargestInArrayList {

public static void main(String[] args) {

int n;

ArrayList L = new ArrayList();

int max;

Scanner in = new Scanner(System.in);

System.out.println("Enter Size of Array List");

n = in.nextInt();

System.out.println("Enter elements in Array List");

for (int i = 0; i < n; i++) {

L.add(in.nextInt());

}

max = L.get(0);

for (int i = 0; i < L.size(); i++) {

if (L.get(i) > max) {

max = L.get(i);

}

}

System.out.println("Max Element: " + max);

in.close();

}

}

Tarun Jadhav answered 2019-07-01T17:31:39Z

1 votes

除了gotomanners的答案,如果有人来到这里寻找同样问题的空安全解决方案,这就是我最终的结果

Collections.max(arrayList, Comparator.nullsFirst(Comparator.naturalOrder()))

Chris Dons Johansen answered 2019-07-01T17:32:10Z

-3 votes

根据数组的大小,多线程解决方案也可能加快速度

niklas answered 2019-07-01T17:32:39Z

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐