简体   繁体   English

如何从 ArrayList 的前 N 个值中获取最大值?

[英]How to get maximum value from of the first N values of an ArrayList?

There is an ArrayList which stores integer values.有一个ArrayList存储 integer 值。 Suppose the arrayList stored values are: 10, 20, 30, 40, 50 .假设 arrayList 存储的值为: 10, 20, 30, 40, 50

I know how to find the maximum value of the collection.我知道如何找到集合的最大值。 I would have to do:我必须这样做:

Collections.max(arrayList); 

But what should I do to find the maximum of the first 3 elements of the collection?但是我应该怎么做才能找到集合的前 3 个元素中的最大值呢? So in this example, it would be 30. Is there a sublist function for collections?所以在这个例子中,它将是 30。collections 是否有子列表 function?

List has a subList method that you can use: List有一个可以使用的subList方法:

Collections.max(arrayList.subList(0, 3))

There is no subList for Collection s in general, as not all collections are lists and, for some, the "first N elements" doesn't make a lot of sense, as they don't maintain any meaningful order (eg HashSet ). Collection通常没有subList ,因为并非所有 collections 都是列表,并且对于某些人来说,“前 N 个元素”没有多大意义,因为它们不保持任何有意义的顺序(例如HashSet )。

You can take the first three elements of any Collection (in whatever order the collection will provide), by iterating through it with a limit.您可以通过限制迭代来获取任何Collection的前三个元素(以集合提供的任何顺序)。 It's probably best to use a stream for this:为此,最好使用 stream:

yourCollection.stream().limit(3).collect(Collectors.toList());

Or you can find what you're looking for directly on the stream, without collecting the elements in some collection:或者您可以直接在 stream 上找到您要查找的内容,而无需收集某些集合中的元素:

Optional<Integer> max = yourCollection.stream.limit(3).max(Comparator.naturalOrder());

You can do the following:您可以执行以下操作:

public static void main(String[] arg) {
    List<Integer> list = new ArrayList<>();
    list.add(10);
    list.add(20);
    list.add(30);
    list.add(40);
    list.add(50);
    System.out.println(Collections.max(list.subList(0, 3)));
}

As ControlAltDel have pointed out in the comments use the subList method to extract the part of the list that you want to calculate the max.正如ControlAltDel在评论中指出的那样,使用 subList 方法来提取要计算最大值的列表部分。

From source one can read:源代码可以阅读:

List subList(int fromIndex, int toIndex)列表子列表(int fromIndex,int toIndex)

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.返回此列表在指定的 fromIndex(包括)和 toIndex(不包括)之间的部分的视图。 (...) (...)

You could make another array list in a method and store all the values you WANT so you could add 10, 20, 30 from the original array list, stop adding and then finding the max of that new array list with the 3 elements.您可以在方法中创建另一个数组列表并存储您想要的所有值,以便您可以从原始数组列表中添加 10、20、30,停止添加,然后找到具有 3 个元素的新数组列表的最大值。

Hope this helped!希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 ArrayList 中获取最大值 - How to get the maximum value from an ArrayList 如何从集合中获取最大值(例如 ArrayList)? - How to get maximum value from the Collection (for example ArrayList)? 将值从一个Arraylist复制到另一个时,所有值均设置为在第一个Arraylist中输入的最后一个值 - When copying the values from one Arraylist to another, all values get set as the last value entered in the first Arraylist 如何从Java 8中的Iterator获取n个第一个值? - How to get n first values from an Iterator in Java 8? 如何从ArrayList获取值 - How to get values from ArrayList 如何从arraylist获得价值? - How to get value from arraylist? 如何从通用ArrayList查找最小值,最大值和平均值 - How to find minimum, maximum and average value from a Generic ArrayList 如果第二个数组中的值与第一个数组中的值相同,如何从另一个数组中删除一个值 - How to remove one arraylist value from another arraylist if the value in second array is same in first arraylist 如何将 Arraylist 的 ArrayList 中的值与 hashmap 中的值相同 - How to put values from ArrayList of Arraylist with same value in hashmap 如果我从第一个ArrayList中删除了一个值,如何在两个ArrayList上获得相同的位置 - How to get the same position on two ArrayListif I have removed a value from the first ArrayList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM