📅  最后修改于: 2020-11-15 03:31:20             🧑  作者: Mango
org.javatuples.KeyValue类表示一个具有两个元素的元组,其位置0和1分别重命名为“键”和“值”。
以下是org.javatuples.KeyValue类的声明-
public final class KeyValue
extends Tuple
implements IValue0, IValue1
Sr.No. | Constructor & Description |
---|---|
1 |
KeyValue(A value0, B value1) This creates a KeyValue Tuple. |
Sr.No. | Method & Description |
---|---|
1 |
static Create tuple from array. |
2 |
static Create tuple from collection. |
3 |
static Create tuple from iterable. |
4 |
static 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 |
set the label and return the tuple. |
9 |
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]