Collection Factory Methods for Vavr – Vavr的收集工厂方法

最后修改: 2017年 9月 24日

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

1. Overview

1.概述

Vavr is a powerful library for Java 8+, built on top of Java lambda expressions. Inspired by the Scala language, Vavr adds functional programming constructs to the Java language, such as pattern-matching, control structures, data types, persistent and immutable collections, and more.

Vavr是一个适用于Java 8+的强大库,建立在Java lambda表达式之上。受Scala语言的启发,Vavr为Java语言增加了函数式编程结构,如模式匹配、控制结构、数据类型、持久化和不可变的集合等等。

In this short article, we’ll show how to use some of the factory methods to create Vavr collections. If you are new to Vavr, you can start with this introductory tutorial which in turn has references to other useful articles.

在这篇短文中,我们将展示如何使用一些工厂方法来创建Vavr集合。如果你是Vavr的新手,你可以从这个介绍性的教程开始,这个教程又提到了其他有用的文章。

2. Maven Dependency

2.Maven的依赖性

To add the Vavr library to your Maven project, edit your pom.xml file to include the following dependency:

要在Maven项目中加入Vavr库,请编辑pom.xml文件,加入以下依赖。

<dependency>
    <groupId>io.vavr</groupId>
    <artifactId>vavr</artifactId>
    <version>0.9.1</version>
</dependency>

You can find the latest version of the library on the Maven Central repository.

您可以在Maven Central资源库上找到该库的最新版本。

3. Static Factory Methods

3.静态工厂方法

Using the static import:

使用静态导入。

static import io.vavr.API.*;

we can create a list using the constructor List(…):

我们可以使用构造函数List(…)创建一个列表:

List numbers = List(1,2,3);

instead of using the static factory method of(…):

而不是使用静态工厂方法of(…):

List numbers = List.of(1,2,3);

or also:

或也是如此。

Tuple t = Tuple('a', 3);

instead of:

而不是。

Tuple t = Tuple.of('a', 3);

This syntactic sugar is similar to the constructs in Scala/Kotlin. From now on, we’ll use these abbreviations in the article.

这种语法糖类似于Scala/Kotlin中的构造。从现在开始,我们将在文章中使用这些缩略语。

4. Creation of Option Elements

4.创建选项元素

The Option elements are not collections but they can be very useful constructs of the Vavr library. It’s a type that allows us to hold either an object or a None element (the equivalent of a null object):

Option元素不是集合,但它们可以成为Vavr库中非常有用的结构。它是一种类型,允许我们持有一个对象或一个None元素(相当于一个null对象)。

Option<Integer> none = None();
Option<Integer> some = Some(1);

5. Vavr Tuples

5.Vavr Tuples

Similarly, Java doesn’t come with tuples, like ordered pairs, triples, etc. In Vavr we can define a Tuple that holds up to eight objects of different types. Here’s an example that holds a Character, a String and an Integer object:

同样地,Java也没有配备图元,比如有序对、三元组等。在Vavr中,我们可以定义一个最多容纳8个不同类型对象的Tuple。下面是一个例子,它可以容纳一个字符、一个字符串和一个整数对象。

Tuple3<Character, String, Integer> tuple
  = Tuple('a', "chain", 2);

6. The Try Type

6.尝试

The Try type can be used to model computations that may or may not raise an exception:

Try类型可用于模型计算,可能会或不可能引发异常

Try<Integer> integer
  = Success(55);
Try<Integer> failure
  = Failure(new Exception("Exception X encapsulated here"));

In this case, if we evaluate integer.get() we’ll obtain the integer object 55. If we evaluate failure.get(), an exception will be thrown.

在这种情况下,如果我们评估integer.get(),我们将获得整数对象55。如果我们评估failure.get(),将会抛出一个异常。

7. Vavr Collections

7.Vavr收藏

We can create collections in many different ways. For Lists, we can use List.of(), List.fill(), List.tabulate(), etc. As mentioned before, the default factory method is List.of() that can be abbreviated using the Scala style constructor:

我们可以通过许多不同的方式来创建集合。对于Lists,我们可以使用List.of()、List.fill()、List.tabulate(),等等。如前所述,默认的工厂方法是List.of(),可以使用Scala风格的构造函数进行简略的描述。

List<Integer> list = List(1, 2, 3, 4, 5);

We can also create an empty list (called a Nil object in Vavr):

我们还可以创建一个空列表(在Vavr中称为Nil对象)。

List()

In an analogous way, we can create other kinds of Collections:

以类似的方式,我们可以创建其他类型的Collections。

Array arr = Array(1, 2, 3, 4, 5);
Stream stm = Stream(1, 2, 3, 4, 5);
Vector vec = Vector(1, 2, 3, 4, 5);

8. Conclusion

8.结论

We’ve seen the most common constructors for the Vavr types and collections. The syntactic sugar provided by the static imports mentioned in section 3 makes it easy to create all the types in the library.

我们已经看到了Vavr类型和集合的最常见的构造函数。在第3节中提到的静态导入所提供的语法糖,使我们可以轻松地创建库中的所有类型。

You can find all the code samples used in this article in the GitHub project.

你可以在GitHub项目中找到本文使用的所有代码样本。