浅析java中Pair和Map的区别

2025-05-29 0 55

在这篇文章中,我们讨论了一个非常有用的编程概念,配对(Pair)。配对提供了一种方便方式来处理简单的键值关联,当我们想从方法返回两个值时特别有用。

在核心Java库中可以使用配对(Pair)的实现。除此之外,某些第三方库,比如Apache Commons和Vavr,已经在各自的api中公开了这个功能。

核心java配对实现

Pair类

Pair类在javafx.util 包中,类构造函数有两个参数,键及对应值:

?

1

2

3
Pair<Integer, String> pair = new Pair<>(1, "One");

Integer key = pair.getKey();

String value = pair.getValue();

示例描述使用Pair类实现简单Integer到String的映射。示例中getKey方法返回key对象,getValue方法返回对应值对象。

AbstractMap.SimpleEntry 和 AbstractMap.SimpleImmutableEntry

SimpleEntry定义在抽象类AbstractMap里面,其构造方法与Pair类似:

?

1

2

3
AbstractMap.SimpleEntry<Integer, String> entry = new AbstractMap.SimpleEntry<>(1, "one");

Integer key = entry.getKey();

String value = entry.getValue();

其键和值可以通过标准的getter和setter方法获得。

另外AbstractMap 类还包含一个嵌套类,表示不可变配对:SimpleImmutableEntry 类。

?

1
AbstractMap.SimpleImmutableEntry<Integer, String> entry = new AbstractMap.SimpleImmutableEntry<>(1, "one");

应用方式与可变的配对一样,除了配置的值不能修改,尝试修改会抛出UnsupportedOperationException异常。

Apache Commons

在Apache Commons库中,org.apache.commons.lang3.tuple 包中提供Pair抽象类,不能被直接实例化,可以通过Pair.of(L,R)实例化,提供了getLeft()和getRight()方法。

?

1

2

3

4

5
Pair<Integer, String> pair= Pair.of(2, "two");

Integer key = pair.getKey();

String value = pair.getValue();

Integer key = pair.getLeft();

String value = pair.getRight();

其有两个子类,分别代表可变与不可变配对:ImmutablePair 和 MutablePair。三者都实现了访问key/value以及setter和getter方法和getLeft()和getRight()方法:

?

1

2

3

4

5
ImmutablePair<Integer, String> pair = new ImmutablePair<>(2, "Two");

Integer key = pair.getKey();

String value = pair.getValue();

Integer key = pair.getLeft();

String value = pair.getRight();

尝试在ImmutablePair 执行setValue方法,会抛出UnsupportedOperationException异常。Pair.of是不可变配对。
但在可变配对上执行完全正常:

?

1

2
Pair<Integer, String> pair = new MutablePair<>(3, "Three");

pair.setValue("New Three");

Vavr库

Vavr库中不可变的Tuple2类提供配对功能:

?

1

2

3
Tuple2<Integer, String> pair = new Tuple2<>(4, "Four");

Integer key = pair._1();

String value = pair._2();

在这个实现中,创建对象后不能修改,所以更新方法返回改变后的新实例:

?

1
tuplePair = pair.update2("New Four");

举个例子

需求:分别用Pair和Map来对value做排序并打印结果。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29
// 使用Pair来排序

JSONObject jsonObject1 = new JSONObject();

jsonObject1.put("a", 9);

JSONObject jsonObject2 = new JSONObject();

jsonObject2.put("a", 4);

JSONObject jsonObject3 = new JSONObject();

jsonObject3.put("a", 7);

JSONObject jsonObject4 = new JSONObject();

jsonObject4.put("a", 8);

ArrayList<Pair<Integer, JSONObject>> pairs = new ArrayList<>();

pairs.add(Pair.of(1, jsonObject1));

pairs.add(Pair.of(2, jsonObject2));

pairs.add(Pair.of(3, jsonObject3));

pairs.add(Pair.of(4, jsonObject4));

List<Pair<Integer, JSONObject>> result = pairs.stream().sorted(Comparator.comparingInt(o -> o.getRight().getInteger("a"))).collect(Collectors.toList());

result.forEach(System.out::println);

System.out.println("==============================");

// 使用map来排序

HashMap<Integer, JSONObject> integerJSONObjectHashMap = new HashMap<>();

integerJSONObjectHashMap.put(1, jsonObject1);

integerJSONObjectHashMap.put(2, jsonObject2);

integerJSONObjectHashMap.put(3, jsonObject3);

integerJSONObjectHashMap.put(4, jsonObject4);

List<Map.Entry<Integer, JSONObject>> result2 = integerJSONObjectHashMap.entrySet().stream().sorted(Comparator.comparingInt(o -> o.getValue().getInteger("a"))).collect(Collectors.toList());

result2.forEach(System.out::println);

到此这篇关于浅析java中Pair和Map的区别的文章就介绍到这了,更多相关java中Pair和Map的区别内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

原文链接:https://blog.csdn.net/Scoful/article/details/114097175

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 浅析java中Pair和Map的区别 https://www.kuaiidc.com/107757.html

相关文章

发表评论
暂无评论