Java 内置数据类型中是没有 Tuple 元组类型的,需要引入第三方包 vavr,它提供了最多八个元素的 Tuple 类型。
- 添加依赖
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>0.10.4</version>
</dependency>
- 使用示例
package org.example;
import io.vavr.Tuple;
import io.vavr.Tuple2;
import io.vavr.Tuple3;
public class Main {
public static void main(String[] args) {
Tuple2<Integer, Integer> tuple2 = Tuple.of(5, 6);
Tuple3<String, Integer, Integer> tuple3 = Tuple.of("beijing", 50, 60);
System.out.println("tuple3 = " + tuple3);
System.out.println("tuple2 = " + tuple2);
System.out.println("tuple2._1 = " + tuple2._1);
System.out.println("tuple2._2() = " + tuple2._2());
}
}