Guava – Lists

https://www.baeldung.com/guava-lists

 

1. Overview

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

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

1

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

2. Reverse a List

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

1

2

3

4

5

6

7

@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

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”:

1

2

3

4

5

6

7

@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

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

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

1

2

3

4

5

6

7

8

9

10

11

@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

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

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

1

2

3

4

5

6

7

8

@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

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

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

1

2

3

4

5

6

7

8

@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

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

1

2

3

4

5

6

7

8

9

10

@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

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

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值