• 介绍java中Pair和Map的区别


    目录

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

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

    Pair和Map共通点

    Pair和Map都是以key,value进行存储

    Pair和Map不同点

    • Pair通过getKey()/getValue()获取对应的key值和value值,没有添加键值对的操作
    • Map是通过get()获取对应的value,通过values()获取所有的value,而且还可以通过put进行新增键值对。
    • pair保存的是一对key value,而map可以保存多对key value。

    核心java配对实现

    Pair类

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

    	Pair pair = new Pair<>(1, "One");
    	Integer key = pair.getKey();
    	String value = pair.getValue();
    
    • 1
    • 2
    • 3

    示例描述使用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();
    
    • 1
    • 2
    • 3

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

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

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

    应用方式与可变的配对一样,除了配置的值不能修改,尝试修改会抛出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();
    
    • 1
    • 2
    • 3
    • 4
    • 5

    其有两个子类,分别代表可变与不可变配对: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();
    
    • 1
    • 2
    • 3
    • 4
    • 5

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

    	Pair pair = new MutablePair<>(3, "Three");
    	pair.setValue("New Three");
    
    • 1
    • 2

    Vavr库

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

        Tuple2 pair = new Tuple2<>(4, "Four");
        Integer key = pair._1();
        String value = pair._2();
    
    • 1
    • 2
    • 3

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

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

    举个例子

    需求:分别用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(Pair.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, jsonObject2);
            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);
    
    • 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
  • 相关阅读:
    LeetCode刷题 309 :最佳买卖股票的时机含冷冻时期
    PHP:名称解析规则
    python神经网络编程 豆瓣,神经网络算法python代码
    flink中使用异步函数的几个注意事项
    设计模式中Monoid/Foldables
    如何使用设计模式优化并发编程?
    光伏无人机巡检主要有些什么功能和特点?
    Java入门第112课——使用Iterator的hasNext方法、next方法遍历集合
    算法训练day37|贪心算法 part06(LeetCode738.单调递增的数字)
    QianBase MPP之qb_toolkit管理模式
  • 原文地址:https://blog.csdn.net/sebeefe/article/details/126553576