Guava – Lists – 番石榴 – 列表

最后修改: 2014年 11月 2日

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

1. Overview

1.概述

In this tutorial – we will illustrate the most common and useful ways to work with Lists using the Guava library.

在本教程中–我们将说明使用Guava库处理Lists的最常见和最有用的方法。

Let’s start simple – and take a look at just creating a new ArrayList using Guava syntax – without new:

让我们从简单的开始–只看一下创建一个新的ArrayList使用Guava语法–没有new

List<String> names = Lists.newArrayList("John", "Adam", "Jane");

2. Reverse a List

2.反转一个列表

First, let’s reverse a List using Lists.reverse() as in the following example:

首先,让我们使用Lists.reverse()来反转一个List,如下例。

@Test
public void whenReverseList_thenReversed() {
    List<String> names = Lists.newArrayList("John", "Adam", "Jane");

    List<String> reversed = Lists.reverse(names);
    assertThat(reversed, contains("Jane", "Adam", "John"));
}

3. Generate Character List from a String

3.从字符串生成列表

Now – let’s see how to break a String apart into a list of Characters.

现在–让我们看看如何将一个字符串分割成一个字符的列表。

In the following example – we use the Lists.CharactersOf() API to create a Character List from a String “John”:

在下面的例子中,我们使用Lists.CharactersOf() API从String “John”创建一个Character List

@Test
public void whenCreateCharacterListFromString_thenCreated() {
    List<Character> chars = Lists.charactersOf("John");

    assertEquals(4, chars.size());
    assertThat(chars, contains('J', 'o', 'h', 'n'));
}

4. Partition a List

4.分割一个列表

Next – Let’s see how to partition a List.

下一步–让我们看看如何分割一个List

In the following example – we use Lists.partition() to get consecutive sublists each of size two:

在下面的例子中–我们使用Lists.partition()来获得连续的子列表,每个大小为2。

@Test
public void whenPartitionList_thenPartitioned(){
    List<String> names = Lists.newArrayList("John","Jane","Adam","Tom","Viki","Tyler");

    List<List<String>> result = Lists.partition(names, 2);

    assertEquals(3, result.size());
    assertThat(result.get(0), contains("John", "Jane"));
    assertThat(result.get(1), contains("Adam", "Tom"));
    assertThat(result.get(2), contains("Viki", "Tyler"));
}

5. Remove Duplicates From List

5.从列表中删除重复的内容

Now – let’s use a simple trick to remove duplicates from a List.

现在–让我们使用一个简单的技巧来删除List中的重复内容。

In the following example – we copy the elements into a Set and then we create a List back out of the remaining elements:

在下面的例子中–我们将元素复制到一个Set中,然后我们从剩余的元素中创建一个List回来。

@Test
public void whenRemoveDuplicatesFromList_thenRemoved() {
    List<Character> chars = Lists.newArrayList('h','e','l','l','o');
    assertEquals(5, chars.size());

    List<Character> result = ImmutableSet.copyOf(chars).asList();
    assertThat(result, contains('h', 'e', 'l', 'o'));
}

6. Remove Null Values from List

6.从列表中删除空值

Next – let’s see how to remove null values from a List.

接下来–让我们看看如何List中删除null值。

In the following example – we remove all null values using the highly useful Iterables.removeIf() API and a predicate provided by the library itself:

在下面的例子中,我们使用非常有用的Iterables.removeIf() API和库本身提供的谓词来删除所有null值。

@Test
public void whenRemoveNullFromList_thenRemoved() {
    List<String> names = Lists.newArrayList("John", null, "Adam", null, "Jane");
    Iterables.removeIf(names, Predicates.isNull());

    assertEquals(3, names.size());
    assertThat(names, contains("John", "Adam", "Jane"));
}

7. Convert a List to an ImmutableList

7.将一个List转换为ImmutableList

Finally – let’s see how to create an immutable copy of a List – an ImmutableList – using the ImmutableList.copyOf() API:

最后–让我们看看如何使用ImmutableList.copyOf() API来创建一个List的不可变副本–ImmutableList

@Test
public void whenCreateImmutableList_thenCreated() {
    List<String> names = Lists.newArrayList("John", "Adam", "Jane");

    names.add("Tom");
    assertEquals(4, names.size());

    ImmutableList<String> immutable = ImmutableList.copyOf(names);
    assertThat(immutable, contains("John", "Adam", "Jane", "Tom"));
}

8. Conclusion

8.结论

And here we are – a quick tutorial going over most of the useful things you can do with Lists using Guava.

我们在这里–一个快速的教程,介绍了你可以用Guava的Lists做的大部分有用的事情。

To dig into Lists even further, check out the Predicates and Functions guide to lists as well as the in-depth guide to Joining and Splitting lists in Guava.

要进一步了解列表,请查看列表的谓词和函数指南,以及深入的Guava中连接和分割列表指南

The implementation of all these examples and code snippets can be found in my Guava github project – this is an Eclipse based project, so it should be easy to import and run as it is.

所有这些例子和代码片段的实现可以在我的Guava github项目中找到 – 这是一个基于Eclipse的项目,所以它应该很容易导入和运行,如实。