📜  JavaTuples-单元类

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


介绍

org.javatuples.Unit类表示具有单个元素的元组。

类声明

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

public final class Unit
   extends Tuple
      implements IValue0

类的构造函数

Sr.No. Constructor & Description
1

Unit(A value0)

This creates a Unit Tuple.

类方法

Sr.No. Method & Description
1

Pair add(Unit tuple)

This method returns a Pair tuple.

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

2

Pair add(X0 value)

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

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

3

Pair addAt0(Unit value)

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

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

4

Pair addAt0(X0 value)

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

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

5

static Unit fromArray(X[] array)

Create tuple from array.

6

static Unit fromCollection(Collection collection)

Create tuple from collection.

7

static Unit fromIterable(Iterable iterable)

Create tuple from iterable.

8

static Unit 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()

Return the value of the tuple.

11

Unit setAt0(X value)

Set the value of the tuple.

12

static Unit with(A value0)

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.Unit;
public class TupleTester {
   public static void main(String args[]){
      Unit unit = Unit.with(5);
      System.out.println(unit);
      boolean isPresent = unit.contains(5);
      System.out.println("5 is present: " + isPresent);
      List list = new ArrayList<>();
      list.add(1);
      Pair pair = unit.add("Test");
      System.out.println(pair);
      Integer value = unit.getValue0();
      System.out.println(value);
      Unit unit1 = Unit.fromCollection(list);   
      System.out.println(unit1);
   }
}

验证结果

使用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]
5 is present: true
[5, Test]
5
[1]