企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Java 偶对 > 原文: [https://howtodoinjava.com/java/basics/pairs-in-java/](https://howtodoinjava.com/java/basics/pairs-in-java/) 使用**偶对**类,例如`javafx.util.Pair`,`ImmutablePair`,`MmutablePair`(通用语言)和`io.vavr.Tuple2`类,在 Java 中学习如何使用键值对 > 阅读更多: [Java 中的元组](https://howtodoinjava.com/java/basics/java-tuples/) ## 1\. 为什么需要偶对? 偶对提供了一种将简单键值关联的便捷方法。 在 Java 中,映射用于存储键值对。 映射存储成对的集合并作为整体进行操作。 有时,我们需要处理一些要求,其中键值对应独立存在。 例如 * 偶对需要在方法中作为参数传递 * 方法需要以偶对形式返回两个值 ## 2\. `javafx.util.Pair`类 Java 核心 API 具有`javafx.util.Pair`作为最接近的匹配,用于具有两个值作为**名称-值对**的目的。 单击此链接以了解在 Eclipse 中添加 [JavaFx 支持。](https://www.eclipse.org/efxclipse/install.html) `Pair`类提供以下方法。 * `boolean equals​(Object o)` – 测试此对与另一个对象的相等性。 * `K getKey()` – 获取该对的键。 * `V getValue()` – 获取该对的值。 * `int hashCode()` – 为此对生成哈希码。 * `String.toString()` – 此对的字符串表示形式。 让我们看一个 **Java 程序来创建和使用**对。 ```java Pair<Integer, String> pair = new Pair<>(100, "howtodoinjava.com"); Integer key = pair.getKey(); //100 String value = pair.getValue(); //howtodoinjava.com pair.equals(new Pair<>(100, "howtodoinjava.com")); //true - same name and value pair.equals(new Pair<>(222, "howtodoinjava.com")); //false - different name pair.equals(new Pair<>(100, "example.com")); //false - different value ``` ## 3\. `Pair`,`ImmutablePair`和`MutablePair` – Apache 公共语言 Commons lang 库有一个有用的类,可以用于偶对,即[`org.apache.commons.lang3.tuple.Pair`](https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/tuple/Pair.html)。 它有两个子类,也可以用于相同目的,即[`ImmutablePair`](https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/tuple/ImmutablePair.html)和[`MutablePair`](https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/tuple/MutablePair.html)。 * `Pair`类是由两个元素组成的偶对。 * `Pair`将元素称为“左”和“右”。 * `Pair`还实现了`Map.Entry`接口,其中键为“左”,值为“右”。 * `ImmutablePair`是`Pair`上的[不可变](https://howtodoinjava.com/java/basics/how-to-make-a-java-class-immutable/)表示。 如果将可变的对象存储在该对中,那么该对本身将有效地变为可变的。 该类也不是`final`,因此子类可能会添加不良行为。 * 如果存储的对象是线程安全的,则`ImmutablePair`是[线程安全的](https://howtodoinjava.com/java/multi-threading/what-is-thread-safety/)。 ```java ImmutablePair<Integer, String> pair = ImmutablePair.of(100, "howtodoinjava.com"); Integer key = pair.getKey(); //100 String value = pair.getValue(); //howtodoinjava.com //Integer key = pair.getLeft(); //100 //String value = pair.getRight(); //howtodoinjava.com pair.equals(ImmutablePair.of(100, "howtodoinjava.com")); //true - same name and value pair.equals(ImmutablePair.of(222, "howtodoinjava.com")); //false - different name pair.equals(ImmutablePair.of(100, "example.com")); //false - different value ``` 不要忘记将库导入应用程序类路径。 ```java <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.8.1</version> </dependency> ``` ## 4\. `io.vavr.Tuple2` - Vavr 用于存储键值对的另一个有用的类是[`Tuple2`](https://static.javadoc.io/io.vavr/vavr/0.9.0/io/vavr/Tuple2.html)。 `Tuple2`提供了许多有用的方法来处理其中存储的数据。 例如: * `T1 _1()` – 此元组的第一个元素的获取器。 * `T2 _2()` – 此元组的第二个元素的获取器。 * `Tuple2 update1(T1 value)` – 将此元组的第一个元素设置为给定值。 * `Tuple2 update2(T2 value)` – 将此元组的第二个元素设置为给定值。 * `Map.Entry toEntry()` – 将元组转换为`java.util.Map.Entry`元组。 * `Tuple2 swap()` – 交换此元组的元素。 * `Tuple2 map(BiFunction mapper)` – 使用映射器函数映射该元组的组件。 * `int compareTo(Tuple2 that)` – 比较两个`Tuple2`实例。 ```java Tuple2<Integer, String> pair = new Tuple2<>(100, "howtodoinjava.com"); Integer key = pair._1(); //100 String value = pair._2(); //howtodoinjava.com pair.equals(new Tuple2<>(100, "howtodoinjava.com")); //true - same name and value pair.equals(new Tuple2<>(222, "howtodoinjava.com")); //false - different name pair.equals(new Tuple2<>(100, "example.com")); //false - different value ``` 不要忘记将库导入应用程序类路径。 ```java <dependency> <groupId>io.vavr</groupId> <artifactId>vavr</artifactId> <version>0.10.2</version> </dependency> ``` 向我提供有关 Java 中的名称/值对的问题。 学习愉快!