Primitive Collections in Eclipse Collections – Eclipse集合中的原始集合

最后修改: 2019年 9月 7日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

In this tutorial, we’ll talk about primitive collections in Java and how Eclipse Collections can help.

在本教程中,我们将讨论Java中的原始集合,以及Eclipse集合如何帮助我们。

2. Motivation

2.动机

Suppose we want to create a simple list of integers:

假设我们想创建一个简单的整数列表。

List<Integer> myList = new ArrayList<>; 
int one = 1; 
myList.add(one);

Since collections can only hold object references, behind the scenes, the one is converted to an Integer in the process. The boxing and unboxing aren’t free, of course. As a result, there’s a performance loss in this process.

由于集合只能容纳对象引用,在幕后,one在这个过程中被转换为Integer当然,装箱和拆箱并不是免费的。因此,在这个过程中会出现性能损失。

So, first, using primitive collections from Eclipse Collections can give us a speed boost.

因此,首先,使用Eclipse Collections中的原始集合可以给我们带来速度上的提升。

Second, it reduces memory footprint. The graph below compares memory usage between the traditional ArrayList and IntArrayList from Eclipse Collections:

其次,它减少了内存的占用。下图比较了传统的 ArrayList 和来自 Eclipse Collections 的 IntArrayList 之间的内存用量。

ints

*Image extracted from https://www.eclipse.org/collections/#concept

*图片摘自https://www.eclipse.org/collections/#concept

And of course, let’s not forget, the variety of implementations is a big seller for Eclipse Collections.

当然,我们也不要忘记,实现的多样性是Eclipse Collections的一大卖点。

Note also that Java up to this point doesn’t have support for primitive collections. However, Project Valhalla through JEP 218 aims to add it.

还要注意的是,到目前为止,Java还没有对原始集合的支持。然而,Valhalla项目通过JEP 218旨在添加它。

3. Dependencies

3.依赖性

We’ll use Maven to include the required dependencies:

我们将使用Maven来包含所需的依赖项。

<dependency>
    <groupId>org.eclipse.collections</groupId>
    <artifactId>eclipse-collections-api</artifactId>
    <version>10.0.0</version>
</dependency>

<dependency>
    <groupId>org.eclipse.collections</groupId>
    <artifactId>eclipse-collections</artifactId>
    <version>10.0.0</version>
</dependency>

4. long List

4.长篇列表

Eclipse Collections has memory-optimized lists, sets, stacks, maps, and bags for all the primitive types. Let’s jump into a few examples.

Eclipse Collections为所有primitive类型提供了内存优化的列表、集合、堆栈、地图和包。让我们跳到几个例子。

First, let’s take a look at a list of longs:

首先,让我们看一下longs的列表。

@Test
public void whenListOfLongHasOneTwoThree_thenSumIsSix() {
    MutableLongList longList = LongLists.mutable.of(1L, 2L, 3L);
    assertEquals(6, longList.sum());
}

5. int List

5.intList

Likewise, we can create an immutable list of ints:

同样地,我们可以创建一个不可变的ints的列表。

@Test
public void whenListOfIntHasOneTwoThree_thenMaxIsThree() {
    ImmutableIntList intList = IntLists.immutable.of(1, 2, 3);
    assertEquals(3, intList.max());
}

6. Maps

6.地图

In addition to the Map interface methods, Eclipse Collections present new ones for each primitive pairing:

除了Map接口方法外,Eclipse集合还为每个基元配对提出了新的方法。

@Test
public void testOperationsOnIntIntMap() {
    MutableIntIntMap map = new IntIntHashMap();
    assertEquals(5, map.addToValue(0, 5));
    assertEquals(5, map.get(0));
    assertEquals(3, map.getIfAbsentPut(1, 3));
}

7. From Iterable to Primitive Collections

7.从Iterable到原始集合

Also, Eclipse Collections works with Iterable:

另外,Eclipse Collections也可以使用Iterable

@Test
public void whenConvertFromIterableToPrimitive_thenValuesAreEqual() {
    Iterable<Integer> iterable = Interval.oneTo(3);
    MutableIntSet intSet = IntSets.mutable.withAll(iterable);
    IntInterval intInterval = IntInterval.oneTo(3);
    assertEquals(intInterval.toSet(), intSet);
}

Further, we can create a primitive map from Iterable:

此外,我们可以从Iterable:中创建一个原始地图。

@Test
public void whenCreateMapFromStream_thenValuesMustMatch() {
    Iterable<Integer> integers = Interval.oneTo(3);
    MutableIntIntMap map = 
      IntIntMaps.mutable.from(
        integers,
        key -> key,
        value -> value * value);
    MutableIntIntMap expected = IntIntMaps.mutable.empty()
      .withKeyValue(1, 1)
      .withKeyValue(2, 4)
      .withKeyValue(3, 9);
    assertEquals(expected, map);
}

8. Streams on Primitives

8.基元上的流媒体

Since Java already comes with primitive streams, and Eclipse Collections integrates nicely with them:

因为Java已经有了原始流,而且Eclipse Collections与它们很好地整合在一起。

@Test
public void whenCreateDoubleStream_thenAverageIsThree() {
    DoubleStream doubleStream = DoubleLists
      .mutable.with(1.0, 2.0, 3.0, 4.0, 5.0)
      .primitiveStream();
    assertEquals(3, doubleStream.average().getAsDouble(), 0.001);
}

9. Conclusion

9.结语

In conclusion, this tutorial presented primitive collections from Eclipse Collections. We demonstrated reasons to utilize it and presented how easily we can add it to our applications.

最后,本教程介绍了Eclipse Collections的原始集合。我们演示了利用它的原因,并介绍了如何轻松地将它添加到我们的应用程序中。

As always the code is available over on GitHub.

像往常一样,代码可在GitHub上获得over。