浅析java中Pair和Map的区别

网友投稿 238 2023-01-25

浅析java中Pair和Map的区别

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

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

核心java配对实现

Pair类

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

Pair 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类似:

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

Integer key = entry.getKey();

String value = entry.getValue();

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

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

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

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

Apache Commons

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

Pair 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()方法:

ImmutablePair 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是不可变配对。

但在可变配对上执行完全正常:

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

pair.setValue("New Three");

Vavr库

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

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

Integer key = pair._1();

String value = pair._2();

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

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

举个例子

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

// 使用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> pairs = new ArrayList<>();

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

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

pairs.add(Phttp://air.of(3, jsonObject3));

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

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

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

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

// 使用map来排序

HashMap integerJSONObjectHashMap = new HashMap<>();

integerJSONObjectHashMap.put(1, jsonObject1);

integerJSONObjectHashMap.put(2, jsohttp://nObject2);

integerJSONObjectHashMap.put(3, jsonObject3);

integerJSONObjectHashMap.put(4, jsonObject4);

List> result2 = integerJSONObjectHashMap.entrySet().stream().sorted(Comparator.comparingInt(o -> o.getValue().getInteger("a"))).collect(Collectors.toList());

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring Boot 接口参数加密解密的实现方法
下一篇:有道翻译开放api接口(有道文档翻译功能)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~