淺析java中Pair和Map的區(qū)別
在這篇文章中,我們討論了一個非常有用的編程概念,配對(Pair)。配對提供了一種方便方式來處理簡單的鍵值關(guān)聯(lián),當(dāng)我們想從方法返回兩個值時特別有用。
在核心Java庫中可以使用配對(Pair)的實現(xiàn)。除此之外,某些第三方庫,比如Apache Commons和Vavr,已經(jīng)在各自的api中公開了這個功能。
核心java配對實現(xiàn)
Pair類
Pair類在javafx.util 包中,類構(gòu)造函數(shù)有兩個參數(shù),鍵及對應(yīng)值:
Pair<Integer, String> pair = new Pair<>(1, "One"); Integer key = pair.getKey(); String value = pair.getValue();
示例描述使用Pair類實現(xiàn)簡單Integer到String的映射。示例中g(shù)etKey方法返回key對象,getValue方法返回對應(yīng)值對象。
AbstractMap.SimpleEntry 和 AbstractMap.SimpleImmutableEntry
SimpleEntry定義在抽象類AbstractMap里面,其構(gòu)造方法與Pair類似:
AbstractMap.SimpleEntry<Integer, String> entry = new AbstractMap.SimpleEntry<>(1, "one"); Integer key = entry.getKey(); String value = entry.getValue();
其鍵和值可以通過標(biāo)準(zhǔn)的getter和setter方法獲得。
另外AbstractMap 類還包含一個嵌套類,表示不可變配對:SimpleImmutableEntry 類。
AbstractMap.SimpleImmutableEntry<Integer, String> entry = new AbstractMap.SimpleImmutableEntry<>(1, "one");
應(yīng)用方式與可變的配對一樣,除了配置的值不能修改,嘗試修改會拋出UnsupportedOperationException異常。
Apache Commons
在Apache Commons庫中,org.apache.commons.lang3.tuple 包中提供Pair抽象類,不能被直接實例化,可以通過Pair.of(L,R)實例化,提供了getLeft()和getRight()方法。
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。三者都實現(xiàn)了訪問key/value以及setter和getter方法和getLeft()和getRight()方法:
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 執(zhí)行setValue方法,會拋出UnsupportedOperationException異常。Pair.of是不可變配對。
但在可變配對上執(zhí)行完全正常:
Pair<Integer, String> pair = new MutablePair<>(3, "Three");
pair.setValue("New Three");
Vavr庫
Vavr庫中不可變的Tuple2類提供配對功能:
Tuple2<Integer, String> pair = new Tuple2<>(4, "Four"); Integer key = pair._1(); String value = pair._2();
在這個實現(xiàn)中,創(chuàng)建對象后不能修改,所以更新方法返回改變后的新實例:
tuplePair = pair.update2("New Four");
舉個例子
需求:分別用Pair和Map來對value做排序并打印結(jié)果。
// 使用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);
到此這篇關(guān)于淺析java中Pair和Map的區(qū)別的文章就介紹到這了,更多相關(guān)java中Pair和Map的區(qū)別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Java使用sqlite 數(shù)據(jù)庫如何生成db文件
這篇文章主要介紹了詳解Java 操作sqllite 數(shù)據(jù)庫如何生成db文件的相關(guān)資料,需要的朋友可以參考下2017-07-07
Java中CountDownLatch進行多線程同步詳解及實例代碼
這篇文章主要介紹了Java中CountDownLatch進行多線程同步詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03
Spring boot打包jar分離lib和resources方法實例
這篇文章主要介紹了Spring boot打包jar分離lib和resources方法實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05
Java獲取調(diào)用當(dāng)前方法的類名或方法名(棧堆信息)的四種方式舉例
在Java編程中我們經(jīng)常需要在運行時獲取當(dāng)前執(zhí)行的方法名稱,這在日志記錄、性能監(jiān)控、調(diào)試等方面非常有用,這篇文章主要給大家介紹了關(guān)于Java獲取調(diào)用當(dāng)前方法的類名或方法名(棧堆信息)的四種方式,需要的朋友可以參考下2024-09-09
Java替換中使用正則表達(dá)式實現(xiàn)中間模糊匹配的方法
今天小編就為大家分享一篇Java替換中使用正則表達(dá)式實現(xiàn)中間模糊匹配的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07

