在arraylists的数组中,获取最大的arraylist

In an array of arraylists, get the largest arraylist

我有一个包含多个不同大小的数组列表的数组。

我想找到数组中最大数组列表的索引。我试过这个:

Collections.max(disc);

disc 是数组列表的名称。

ArrayList 我相信没有实现 Comparable 接口,所以我不能这样做。有没有一种方法可以使自定义的 ArrayList 的大小具有可比性?

您不能向现有 class 添加接口。如果您希望 ArrayList 实现 Comparable,您需要编写一个包含 ArrayList 并实现接口的适配器 class。

但是,在您的情况下,您不需要那个。 Comparable 有一个名为 Comparator which is an external object specifying how to compare some type. It can be used to provide alternative sorting mechanisms (descending rather than ascending, for instance) or, as in your case, to add comparison capabilities to an existing class that lacks them. And Collections.max has an overload 的姊妹接口,它带有一个比较器。

Collections.max(disc, (a, b) -> a.size() - b.size())

请注意,如果您使用的是非常旧的 Java 版本,则需要显式创建一个 Comparator 实例,而不是像我上面那样使用 SAM conversion

如果您想知道最大的 sub-ArrayList 的索引,那么您基本上是在寻找与最大的 ArrayList 存储位置相关的信息。这项研究不仅仅基于最大的 ArrayList 的内在特征,还基于它所在的位置。

使用 Collections.max 对您没有帮助,即使您重新定义它的自然顺序以提供新的 Comparator,因为您只会获得最大的 ArrayList 而不是存储它的索引。如果你想找到你必须手动循环外部 ArrayList 的索引,每当你找到一个新的最大 sub-ArrayList 保存它的索引。

当然,我所说的所有内容都是基于您对检索索引感兴趣的唯一条件,而不是最大的 sub-ArrayList 本身。如果您只是在寻找最大的 sub-ArrayList,那么 Collections.max 和 Comparator 实现就是您所需要的。

I want to find the index of the largest arraylist in the array.

Collections.max 不带 array 也不带 return 和 index。它 return 是 Collection 中的大元素。

在不知道你的确切数据结构的情况下,我使用的是列表列表。

List<List<Integer>> ss = 
    List.of(
        List.of(1, 2, 3),
        List.of(1, 2, 3, 4), 
        List.of(1, 2)
    );

这只是比较大小和 return 以最大列表为目标的索引

int maxIndex = IntStream.range(0, ss.size()).boxed().collect(Collectors
            .maxBy(Comparator.comparing(i -> ss.get(i).size()))).get();

打印

1

maxBy 收集器 return 是一个 Optional 我立即取了值。您可能希望将其分配给一个 Optional,然后针对空集合进行适当处理。

一种更简单的方法是使用循环。只需找到最大大小并将索引与其相关联即可。

maxIndex = 0;
int maxSize = 0;
for (int i = 0; i < ss.size(); i++) {
    int size = ss.get(i).size();
    if (size> maxSize) {
        maxIndex = i;
        maxSize = size;
    }
}
System.out.println(maxIndex);

打印

1

我更喜欢更聪明的 ,但这里还有另一种解决方案。这里的这个不是那么简短,但在第一次阅读时可能更容易理解。

你说:

I have an array with multiple arraylists of different sizes. I want to find the index of the largest arraylist in the array.

正如他们所讨论的那样,Collections.max returns 你是对最大对象的引用,而不是该对象在数组中的索引。

所以我们需要两个步骤:

  1. 确定最大的元素。
  2. 获取那个最大对象的索引。

首先,一些示例数据。

List[] arrayOfListsOfIntegers =
        List.of(
                        List.of( 1 ) ,
                        List.of( 1001 , 1002 , 1003 , 1004 ) ,
                        List.of( 101 , 102 , 103 ) ,
                        List.of( 11 , 12 )
                )
                .toArray( List[] :: new );

确定最大元素

使用Arrays.asList to make a view onto that array that appears as a List. This way we can call convenient List方法。

然后制作该列表的流。通过获取每个元素的大小对流中的元素进行排序。从已排序的元素创建一个新列表。我们知道排序列表的最后一个元素具有最大的元素。

List sorted =
        Arrays
                .asList( arrayOfListsOfIntegers )
                .stream()
                .sorted( Comparator.comparingInt( List :: size ) )
                .toList();

获取最大元素的引用,即我们已排序列表列表的最后一个元素。

Object target = sorted.get( sorted.size() - 1 );

获取那个最大对象的索引

目标对象就是我们要在原始列表中定位的对象。我们可以通过调用 List#indexOf. Here again we use Arrays.asList 来定位我们的数组作为 List.

int index = Arrays.asList( arrayOfListsOfIntegers ).indexOf( target );