📜  JavaTuples-KeyValue类

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


介绍

org.javatuples.KeyValue类表示一个具有两个元素的元组,其位置0和1分别重命名为“键”和“值”。

类声明

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

类构造器

Sr.No. Constructor & Description
1

KeyValue(A value0, B value1)

This creates a KeyValue Tuple.

类方法

Sr.No. Method & Description
1

static KeyValue fromArray(X[] array)

Create tuple from array.

2

static KeyValue fromCollection(Collection collection)

Create tuple from collection.

3

static KeyValue fromIterable(Iterable iterable)

Create tuple from iterable.

4

static KeyValue fromIterable(Iterable iterable, int index)

Create tuple from iterable, starting from the specified index.

5

A getKey()

Return the key.

6

int getSize()

Return the size of the tuple.

7

A getValue()

Returns the value of the tuple.

8

KeyValue setKey(X key)

set the label and return the tuple.

9

KeyValue setValue(Y value)

set the value and return the tuple.

10

static KeyValue with(A value0, B value1)

Create the tuple using given value.

方法继承

此类从以下类继承方法-

  • org.javatuples.Tuple

  • 目的

让我们来看看KeyValue类的作用。在这里,我们将看到如何使用各种方法。

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

文件:TupleTester.java

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.List;
import org.javatuples.KeyValue;
public class TupleTester {
   public static void main(String args[]){
      KeyValue keyValue = KeyValue.with(5,6);
      System.out.println(keyValue);
      List list = new ArrayList<>();
      list.add(1);
      list.add(2);
      Integer key = KeyValue.getKey();
      System.out.println(key);
      Integer value = KeyValue.getValue();
      System.out.println(value);
      KeyValue keyValue1 = KeyValue.fromCollection(list);   
      System.out.println(keyValue1);
   }
}

验证结果

使用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]
5
6
[1, 2]