Javatuples 简明教程

JavaTuples - Pair Class

Introduction

org.javatuples.Pair 类表示一个包含两个元素的元组。

The org.javatuples.Pair class represents a Tuple with two elements.

Class Declaration

以下是 org.javatuples.Pair 类的声明:

Following is the declaration for org.javatuples.Pair class −

public final class Pair<A,B>
   extends Tuple
      implements IValue0<A>, IValue1<B>

Class Constructor

Sr.No.

Constructor & Description

1

Pair(A value0, B value1) This creates a Pair Tuple.

Class Methods

Sr.No.

Method & Description

1

Triplet add(Unit tuple) This method returns a Triplet tuple. Similarly other methods to add tuples are available e.g. add(Pair tuple) returns Quartet and upto add(Octet tuple) returns Decade tuple.

2

Triplet add(X0 value) This method add a value to the tuple and returns a Triplet tuple. Similarly other methods to add values are available e.g. add(X0 value0, X1 value1) returns Quartet and so on upto add() with eight parameters.

3

Triplet addAt0(Unit value) This method add a Unit tuple at index 0 and returns a Triplet tuple. Similarly other methods to add tuples are available e.g. addAt0(Pair value) returns Quartet and so on upto addAt0(Octet). Other similar method are addAt1(Unit value) which add a unit at index0 and have similar methods upto addAt2(Octet).

4

Triplet addAt0(X0 value) This method add a value at index 0 and returns a Triplet tuple. Similarly other methods to add values are available e.g. addAt0(X0 value0, X1 value1) returns Quartet and so on upto addAt0() with eight parameters. Other similar method are addAt1(X0 value) which add a value at index0 and have similar methods upto addAt2() with eight parameters.

5

static <X> Pair<X,X> fromArray(X[] array) Create tuple from array.

6

static <X> Pair<X,X> fromCollection(Collection<X> collection) Create tuple from collection.

7

static <X> Pair<X,X> fromIterable(Iterable<X> iterable) Create tuple from iterable.

8

static <X> Pair<X,X> fromIterable(Iterable<X> iterable, int index) Create tuple from iterable, starting from the specified index.

9

int getSize() Return the size of the tuple.

10

A getValue0() Returns the value of the tuple at index 0. Similarly getValue1() returns the value at index 1.

11

Unit<B> removeFrom0() Return the tuple after removing value of the tuple at index 0. Similarly removeFrom1() returns the tuple after removing value of the tuple at index 1.

12

<X> Pair<X,B> setAt0(X value) Set the value of the tuple at index 0.

13

static <A,B> Pair<A,B> with(A value0, B value1) Create the tuple using given value.

Methods inherite

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. org.javatuples.Tuple

  2. Object

Example

让我们看看 Pair 类在实际中的使用。在此,我们将了解如何使用各种方法。

Let’s see Pair Class in action. Here we’ll see how to use various methods.

C:&gt;JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。

Create a java class file named TupleTester in C:\>JavaTuples.

文件:TupleTester.java

File: TupleTester.java

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.List;
import org.javatuples.Pair;
import org.javatuples.Triplet;
import org.javatuples.Unit;

public class TupleTester {
   public static void main(String args[]){
      Pair<Integer, Integer> pair = Pair.with(5,6);
      System.out.println(pair);
      boolean isPresent = pair.contains(5);
      System.out.println("5 is present: " + isPresent);
      List<Integer> list = new ArrayList<>();
      list.add(1);
      list.add(2);
      Triplet<Integer,Integer, String> triplet = pair.add("Test");
      System.out.println(triplet);
      Integer value = pair.getValue0();
      System.out.println(value);
      Unit<Integer> unit = pair.removeFrom0();
      System.out.println(unit);
      Pair<Integer, Integer> pair1 = Pair.fromCollection(list);
      System.out.println(pair1);
   }
}

Verify the result

Verify the result

使用以下 javac 编译器编译类:

Compile the classes using javac compiler as follows −

C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java

现在运行 TupleTester 查看结果:

Now run the TupleTester to see the result −

C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester

Output

验证输出

Verify the Output

[5, 6]
5 is present: true
[5, 6, Test]
5
[6]
[1, 2]