欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺析java中Pair和Map的區(qū)別

 更新時(shí)間:2021年03月23日 14:42:37   作者:Scoful  
這篇文章主要介紹了java中Pair和Map的區(qū)別,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

在這篇文章中,我們討論了一個(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)文章

最新評論