📜  JavaTuples-Triplet类

📅  最后修改于: 2020-11-15 03:27:47             🧑  作者: Mango


介绍

org.javatuples.Triplet类表示具有三个元素的元组。

类声明

以下是org.javatuples.Triplet类的声明-

类构造器

Sr.No. Constructor & Description
1

Triplet(A value0, B value1, C value2)

This creates a Triplet Tuple.

类方法

Similarly setAt1() upto setAt2() set the value at index 1 and so on.

Sr.No. Method & Description
1

Quartet add(Unit tuple)

This method returns a Quartet tuple.

Similarly other methods to add tuples are available e.g. add(Pair tuple) returns Quintet and upto add(Septet tuple) returns Decade tuple.

2

Quartet add(X0 value)

This method add a value to the tuple and returns a Quartet tuple.

Similarly other methods to add values are available e.g. add(X0 value0, X1 value1) returns Quintet and so on upto add() with seven parameters.

3

Quartet addAt0(Unit value)

This method add a Unit tuple at index 0 and returns a Quartet tuple.

Similarly other methods to add tuples are available e.g. addAt0(Pair value) returns Quintet and so on upto addAt0(Septet). Other similar method are addAt1(Unit value) which add a unit at index0 and have similar methods upto addAt2(Septet).

4

Quartet addAt0(X0 value)

This method add a value at index 0 and returns a Quartet tuple.

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

5

static Triplet fromArray(X[] array)

Create tuple from array.

6

static Triplet fromCollection(Collection collection)

Create tuple from collection.

7

static Triplet fromIterable(Iterable iterable)

Create tuple from iterable.

8

static Triplet fromIterable(Iterable 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() upto getValue2() returns the value at index 1 and so on.

11

Pair removeFrom0()

Return the tuple after removing value of the tuple at index 0.

Similarly removeFrom1() upto removeFrom2() returns the tuple after removing value of the tuple at index 1 and so on.

12

Triplet setAt0(X value)

Set the value of the tuple at index 0.

13

static Triplet with(A value0, B value1, C value2)

Create the tuple using given value.

方法继承

此类从以下类继承方法-

  • org.javatuples.Tuple

  • 目的

让我们看看三重奏类的实际应用。在这里,我们将看到如何使用各种方法。

C:\> JavaTuples中创建一个名为TupleTester的Java类文件。

文件:TupleTester.java

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

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

验证结果

使用javac编译器编译类,如下所示:

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

现在运行TupleTester以查看结果-

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

输出

验证输出

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