Javatuples 简明教程
JavaTuples - KeyValue Class
Introduction
org.javatuples.KeyValue 类表示一个元组,其两个元素置为 0 和 1 的位置,分别重命名为“key”和“value”。
The org.javatuples.KeyValue class represents a Tuple with two elements with positions 0 and 1 renamed as "key" and "value", respectively.
Class Declaration
以下是 org.javatuples.KeyValue 类的声明 −
Following is the declaration for org.javatuples.KeyValue class −
public final class KeyValue<A,B>
extends Tuple
implements IValue0<A>, IValue1<B>
Class Constructor
Sr.No. |
Constructor & Description |
1 |
KeyValue(A value0, B value1) This creates a KeyValue Tuple. |
Class Methods
Sr.No. |
Method & Description |
1 |
static <X> KeyValue<X,X> fromArray(X[] array) Create tuple from array. |
2 |
static <X> KeyValue<X,X> fromCollection(Collection<X> collection) Create tuple from collection. |
3 |
static <X> KeyValue<X,X> fromIterable(Iterable<X> iterable) Create tuple from iterable. |
4 |
static <X> KeyValue<X,X> fromIterable(Iterable<X> 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 |
<X> KeyValue<X,B> setKey(X key) set the label and return the tuple. |
9 |
<X> KeyValue<A,Y> setValue(Y value) set the value and return the tuple. |
10 |
static <A,B> KeyValue<A,B> with(A value0, B value1) Create the tuple using given value. |
Methods inherite
此类从以下类中继承方法:
This class inherits methods from the following classes −
-
org.javatuples.Tuple
-
Object
Example
让我们看看 KeyValue 类的实际操作。我们将在其中了解如何使用各种方法。
Let’s see KeyValue Class in action. Here we’ll see how to use various methods.
在 C:>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.KeyValue;
public class TupleTester {
public static void main(String args[]){
KeyValue<Integer, Integer> keyValue = KeyValue.with(5,6);
System.out.println(keyValue);
List<Integer> 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<Integer, Integer> keyValue1 = KeyValue.fromCollection(list);
System.out.println(keyValue1);
}
}
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