淺析java中Pair和Map的區(qū)別
在這篇文章中,我們討論了一個(gè)非常有用的編程概念,配對(Pair)。配對提供了一種方便方式來處理簡單的鍵值關(guān)聯(lián),當(dāng)我們想從方法返回兩個(gè)值時(shí)特別有用。
在核心Java庫中可以使用配對(Pair)的實(shí)現(xiàn)。除此之外,某些第三方庫,比如Apache Commons和Vavr,已經(jīng)在各自的api中公開了這個(gè)功能。
核心java配對實(shí)現(xiàn)
Pair類
Pair類在javafx.util 包中,類構(gòu)造函數(shù)有兩個(gè)參數(shù),鍵及對應(yīng)值:
Pair<Integer, String> pair = new Pair<>(1, "One"); Integer key = pair.getKey(); String value = pair.getValue();
示例描述使用Pair類實(shí)現(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 類還包含一個(gè)嵌套類,表示不可變配對:SimpleImmutableEntry 類。
AbstractMap.SimpleImmutableEntry<Integer, String> entry = new AbstractMap.SimpleImmutableEntry<>(1, "one");
應(yīng)用方式與可變的配對一樣,除了配置的值不能修改,嘗試修改會拋出UnsupportedOperationException異常。
Apache Commons
在Apache Commons庫中,org.apache.commons.lang3.tuple 包中提供Pair抽象類,不能被直接實(shí)例化,可以通過Pair.of(L,R)實(shí)例化,提供了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();
其有兩個(gè)子類,分別代表可變與不可變配對:ImmutablePair 和 MutablePair。三者都實(shí)現(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();
在這個(gè)實(shí)現(xiàn)中,創(chuàng)建對象后不能修改,所以更新方法返回改變后的新實(shí)例:
tuplePair = pair.update2("New Four");
舉個(gè)例子
需求:分別用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-07Java中CountDownLatch進(jìn)行多線程同步詳解及實(shí)例代碼
這篇文章主要介紹了Java中CountDownLatch進(jìn)行多線程同步詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03SpringBoot自定義bean綁定實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot自定義bean綁定,最常見的配置綁定的場景,是在自定義的bean中通過@Value注解將某個(gè)屬性和對應(yīng)的配置綁定2022-10-10Spring boot打包jar分離lib和resources方法實(shí)例
這篇文章主要介紹了Spring boot打包jar分離lib和resources方法實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05java中實(shí)現(xiàn)分頁的幾種常見方式總結(jié)
在項(xiàng)目中經(jīng)常會查詢大量數(shù)據(jù),這就要用到分頁展示,下面這篇文章主要給大家介紹了關(guān)于java中實(shí)現(xiàn)分頁的幾種常見方式,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12Java獲取調(diào)用當(dāng)前方法的類名或方法名(棧堆信息)的四種方式舉例
在Java編程中我們經(jīng)常需要在運(yùn)行時(shí)獲取當(dāng)前執(zhí)行的方法名稱,這在日志記錄、性能監(jiān)控、調(diào)試等方面非常有用,這篇文章主要給大家介紹了關(guān)于Java獲取調(diào)用當(dāng)前方法的類名或方法名(棧堆信息)的四種方式,需要的朋友可以參考下2024-09-09Java替換中使用正則表達(dá)式實(shí)現(xiàn)中間模糊匹配的方法
今天小編就為大家分享一篇Java替換中使用正則表達(dá)式實(shí)現(xiàn)中間模糊匹配的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07