Java 中的偶对
顾名思义,偶对是存储偶对数据的一种方式。偶对数据由两个字段组成——一个用于键,另一个用于值。偶对是从一个方法返回多个值的好方法。在本教程中,我们将学习在 Java 中使用对的不同方法。
使用 javafx.util 包的偶对类
javafx.util
包有一个方便的 Pair 类,可以用来处理 Pair。我们可以使用以下语法初始化这个类的对象。
Pair<Key Type, Value Type> pairName = new Pair<>(key, value);
这个类提供了 getKey() 和 getValue() 方法从这对中获取数据。下面的代码显示了这些方法的工作原理。
import javafx.util.Pair;
public class PairsInJava
{
public static void main(String[] args)
{
Pair<String, Double> studentNameGPAPair1 = new Pair<>("Justin", 8.76);
Pair<String, Double> studentNameGPAPair2 = new Pair<>("Jessica", 8.76);
System.out.println("The Pair is: " + studentNameGPAPair1.toString());
System.out.println("The Key is: " + studentNameGPAPair1.getKey());
System.out.println("The Value is: " + studentNameGPAPair1.getValue());
}
}
这对是:贾斯汀=8.76 关键是:贾斯汀 数值是:8.76
使用抽象映射。简单入口类
Java 中的AbstractMap.SimpleEntry
类可以用来创建一对。与javafx.util
的 Pair 类不同,该类提供了 setValue() 方法,可用于更改 Pair 的值。但是,我们不能修改密钥。
import java.util.AbstractMap;
public class PairsInJava
{
public static void main(String[] args)
{
AbstractMap.SimpleEntry<String, Double> pair = new AbstractMap.SimpleEntry<>("Justin", 8.78);
System.out.println("Key = " + pair.getKey() + " Value = " + pair.getValue());
pair.setValue(8.8);
System.out.println("Key = " + pair.getKey() + " Updated Value = " + pair.getValue());
}
}
键=贾斯汀值= 8.78 键=贾斯汀更新值= 8.8
使用抽象映射。simpleimumplaceenttry
SimpleImmutatedEntry
班和SimpleEntry
班非常相似。唯一不同的是这个类的对象是不变的。我们不能在这个课堂上使用setValue()
方法。如果我们使用这种方法,我们会得到一个**UnsupportedOperationException**
。
在下面的代码中,前两行执行,但是当我们试图修改该值时,我们得到一个错误。
import java.util.AbstractMap;
public class PairsInJava
{
public static void main(String[] args)
{
AbstractMap.SimpleImmutableEntry<String, Double> pair = new AbstractMap.SimpleImmutableEntry<>("Justin", 8.78);
System.out.println("Key = " + pair.getKey() + " Value = " + pair.getValue());
pair.setValue(8.8);//error
}
}
Key = Justin Value = 8.78 线程“main”Java . lang . unsupportedoperationexception 处的异常
使用阿帕奇公共图书馆
阿帕奇公共图书馆为我们提供了一个 MutablePair
类和一个 ImmutablePair
类。这两个类都有标准的 getter 方法来获取键和值对。
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.MutablePair;
public class PairsInJava
{
public static void main(String[] args)
{
MutablePair<String, Double> studentNameGPAPair1 = new MutablePair<>("Justin", 8.76);
ImmutablePair<String, Double> studentNameGPAPair2 = new ImmutablePair<>("Jessica", 8.13);
System.out.println("The MutablePair Key is: " + studentNameGPAPair1.getKey());
System.out.println("The MutablePair Value is: " + studentNameGPAPair1.getValue());
System.out.println("The ImmutablePair Key is: " + studentNameGPAPair2.getKey());
System.out.println("The ImmutablePair Value is: " + studentNameGPAPair2.getValue());
}
}
可变空气键为:贾斯汀 可变空气值为:8.76 不变空气键为:杰西卡 不变空气值为:8.13
ImmutablePair
类没有 setter 方法,因为它创建了一个不可变的对,创建后不能修改。但是我们有**setLeft()**
和**setRight()**
方法给MutablePair
类。setLeft()
方法设置左元素或键对。setRight()方法设置右元素或对的值。
import org.apache.commons.lang3.tuple.MutablePair;
public class PairsInJava
{
public static void main(String[] args)
{
MutablePair<String, Double> studentNameGPAPair = new MutablePair<>("Justin", 8.76);
System.out.println("Pair before using setters: " + studentNameGPAPair.toString());
studentNameGPAPair.setLeft("Jessica");
studentNameGPAPair.setRight(8.13);
System.out.println("Pair After using setters: " + studentNameGPAPair.toString());
}
}
使用沉降片前偶对:(贾斯汀,8.76) 使用沉降片后偶对:(杰西卡,8.13)
使用 javatuples 偶对类
javatuples 库提供了不同的类来实现 Java 中的不可变元组。这个库的 Pair 类可以用来创建两个元素的元组。我们有 getValue() 和 getValueX() 方法(其中 X 表示索引)来按索引获取对元素。我们通常将键存储在索引 0 处,将值存储在索引 1 处。我们需要使用 getValue()方法进行强制转换。
import org.javatuples.Pair;
public class PairDemo
{
public static void main(String[] args)
{
Pair<String, Double> studentNameGPAPair1 = Pair.with("Justin", 8.76);
Pair<String, Double> studentNameGPAPair2 = Pair.with("Jessica", 8.13);
String key1 = studentNameGPAPair1.getValue0();
Double value1 = studentNameGPAPair1.getValue1();
//casting
String key2 = (String) studentNameGPAPair2.getValue(0);
Double value2 = (Double) studentNameGPAPair2.getValue(1);
System.out.println("Key-1 " + key1 + " Value-1 " + value1);
System.out.println("Key-2 " + key2 + " Value-2 " + value2);
}
}
键-1 贾斯汀值-1 8.76 键-2 杰西卡值-2 8.13
我们还有一个setAtX()
方法,它不会改变原来的偶对,但会返回一个有变化的新偶对。
import org.javatuples.Pair;
public class PairDemo
{
public static void main(String[] args)
{
Pair<String, Double> studentNameGPAPair1 = Pair.with("Justin", 8.76);
Pair<String, Double> studentNameGPAPair2 = studentNameGPAPair1.setAt1(8.80);
System.out.println("Original Pair: " + studentNameGPAPair1);
System.out.println("Modified Pair: " + studentNameGPAPair2);
}
}
原始对:[贾斯汀,8.76] 修改对:[贾斯汀,8.8]
使用虚拟图书馆的 Tuple2 类
Vavr 库的 Tuple2 类可以用来创建一个不可变的对。我们可以使用 _1() 方法获取密钥。同样,可以使用 _2() 方法从该对中获取值。
import io.vavr.Tuple2;
public class PairsInJava
{
public static void main(String[] args)
{
Tuple2<String, Double> studentNameGPA = new Tuple2<>("Justin", 8.78);
String key = studentNameGPA._1();
Double value = studentNameGPA._2();
System.out.println("Key = " + key);
System.out.println("Value = " + value);
}
}
键=贾斯汀 值= 8.78
我们还有 update1() 和 update2() 方法,它们用更新返回一个新的 tuple2 对象。请注意,这些方法不会改变原始对。
import io.vavr.Tuple2;
public class PairsInJava
{
public static void main(String[] args)
{
Tuple2<String, Double> studentNameGPA1 = new Tuple2<>("Justin", 8.78);
Tuple2<String, Double> studentNameGPA2 = studentNameGPA1.update2(8.80);
System.out.println("Original pair: " + studentNameGPA1);
System.out.println("Modified pair: " + studentNameGPA2);
}
}
原始对:(贾斯汀,8.78) 修改对:(贾斯汀,8.8)
用户定义的偶对类
如果我们不想使用外部库,那么我们可以实现自己的自定义类来创建对。它将有两个所需数据类型的成员变量。我们还将添加 getter 和 setter 方法。我们可以根据自己的需求创建额外的方法。
class Pair
{
private String name;
private Double gpa;
Pair(String name, Double gpa)
{
this.name = name;
this.gpa = gpa;
}
public String getKey()
{
return name;
}
public void setKey(String name)
{
this.name = name;
}
public Double getValue()
{
return gpa;
}
public void setValue(Double gpa)
{
this.gpa = gpa;
}
}
public class PairsInJava
{
public static void main(String[] args)
{
Pair studentNameGPA = new Pair("Justin", 8.76);
System.out.println("Key: " + studentNameGPA.getKey());
System.out.println("Value: " + studentNameGPA.getValue());
studentNameGPA.setValue(8.80);
System.out.println("New Value: " + studentNameGPA.getValue());
}
}
键:贾斯汀 值:8.76 新值:8.8
用对象数组在 Java 中创建对
外部库的另一种替代方法是使用对象数组。因为所有类都扩展了对象类,所以我们可以很容易地在这个数组中存储任何类对象。
public class PairsInJava
{
public static void main(String[] args)
{
String key = "Justin";
Double value = 8.78;
Object[] objArr = new Object[2];
objArr[0] = key;
objArr[1] = value;
System.out.println("Key = " + objArr[0]);
System.out.println("Value = " + objArr[1]);
}
}
键=贾斯汀 值= 8.78
摘要
对是存储两个数据项的一种简单方法,这两个数据项之间有一个映射。偶对通常存储一个键及其相应的值。在 Java 中有很多使用 Pairs 的方法。我们可以使用外部库,如 Apache Commons 或 Vavr 库来实现对。我们还可以定义自己的 pair 类,它可以有额外的方法来满足我们的需求。