Java元組Tuple使用例項
阿新 • • 發佈:2019-02-14
http://www.cnblogs.com/flyingeagle/articles/6696780.html 原文地址
一.為什麼使用元組tuple?
元組和列表list一樣,都可能用於資料儲存,包含多個數據;但是和列表不同的是:列表只能儲存相同的資料型別,而元組不一樣,它可以儲存不同的資料型別,比如同時儲存int、string、list等,並且可以根據需求無限擴充套件。比如說在web應用中,經常會遇到一個問題就是資料分頁問題,查詢分頁需要包含幾點資訊:當前頁數、頁大小;查詢結果返回資料為:當前頁的資料記錄,但是如果需要在前臺顯示當前頁、頁大小、總頁數等資訊的時候,就必須有另外一個資訊就是:資料記錄總數,然後根據上面的資訊進行計算得到總頁數等資訊。這個時候查詢某一頁資訊的時候需要返回兩個資料型別,一個是list(當前也的資料記錄),一個是int(記錄總數)。當然,完全可以在兩個方法、兩次資料庫連線中得到這兩個值。事實上在查詢list的時候,已經通過sql查詢得到總計錄數,如果再開一個方法,再做一次資料庫連線來查詢總計錄數,不免有點多此一舉、浪費時間、浪費程式碼、浪費生命。言重了~在這種情況下,我們就可以利用二元組,在一次資料庫連線中,得到總計錄數、當前頁記錄,並存儲到其中,簡單明瞭!
二.原始碼例項
1.二元組
package com.bijian.test; /** * 兩個元素的元組,用於在一個方法裡返回兩種型別的值 */ public class TwoTuple<A, B> { public final A first; public final B second; public TwoTuple(A a, B b) { this.first = a; this.second = b; } }
2.擴充套件為三元組(按此可以任意擴充套件)
package com.bijian.test; /** * 三個元素的元組,用於在一個方法裡返回三種類型的值*/ public class ThreeTuple<A, B, C> extends TwoTuple<A, B> { public final C third; public ThreeTuple(A a, B b, C c) { super(a, b); this.third = c; } }
3.實體DTO
package com.bijian.test; public class GoodsBean { private int goodsId; public int getGoodsId() {return goodsId; } public void setGoodsId(int goodsId) { this.goodsId = goodsId; } }
4.元組操作工具類、測試類(可按需自定義)
package com.bijian.test; import java.util.ArrayList; import java.util.List; /** * 元組輔助類,用於多種型別值的返回,如在分頁的時候,後臺儲存過程既返回了查詢得到的 * 當頁的資料(List型別),又得到了資料表中總共的資料總數(Integer型別),然後將這 * 兩個引數封裝到該類中返回到action中使用 * 使用泛型方法實現,利用引數型別推斷,編譯器可以找出具體的型別 */ public class TupleUtil { public static <A, B> TwoTuple<A, B> tuple(A a, B b) { return new TwoTuple<A, B>(a, b); } public static <A, B, C> ThreeTuple<A, B, C> tuple(A a, B b, C c) { return new ThreeTuple<A, B, C>(a, b, c); } // 測試 public static void main(String[] args) { List<GoodsBean> goodsBeans = new ArrayList<GoodsBean>(); for(int i = 1; i < 26; i++) { GoodsBean goodsBean = new GoodsBean(); goodsBean.setGoodsId(i); goodsBeans.add(goodsBean); } Integer totalProperty = 47; // TupleUtil<List<GoodsBean>, Integer> tupleUtil = new TupleUtil<List<GoodsBean>, Integer>(goodsBeans, totalProperty); TwoTuple<List<GoodsBean>, Integer> twoTuple = TupleUtil.tuple(goodsBeans, totalProperty); List<GoodsBean> list = twoTuple.first; System.out.println(list); System.out.println(twoTuple.second); } }
執行結果:
三.org.apache.commons.lang3.tuple
用於處理一對鍵值的物件pair類似於Map.entry,commons lang3增加了可以處理3個值的Triple基類,此包下定義了Pair<L,R>抽象基類,及MutablePair,MutableTriple,ImmutablePair,ImmutableTriple子類。
一個執行緒非安全,另一個執行緒安全
介面:
1.Pair:封裝一對鍵值對。
實現類:可變:MutablePair<L,R>,不可變:ImmutablePair
2.Triple:封裝3個值的類
實現類:ImmutableTriple; MuttableTriple<L,M,R>
如上例項修改如下:
package com.bijian.test; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.tuple.Pair; public class TupleUtil2 { // 測試 public static void main(String[] args) { List<GoodsBean> goodsBeans = new ArrayList<GoodsBean>(); for(int i = 1; i < 26; i++) { GoodsBean goodsBean = new GoodsBean(); goodsBean.setGoodsId(i); goodsBeans.add(goodsBean); } Integer totalProperty = 47; Pair<List<GoodsBean>, Integer> twoTuple = Pair.of(goodsBeans, totalProperty); List<GoodsBean> list = twoTuple.getLeft(); System.out.println(list); System.out.println(twoTuple.getRight()); } }
執行結果:
PS:org.apache.commons.lang3.tuple.Pair的compareTo方法
org.apache.commons.lang3.tuple.Pair的compareTo方法的入參不能為空,例項如下。
package com.bijian.test; import org.apache.commons.lang3.tuple.Pair; public class PairTest { private Pair<Long, Long> pair = Pair.of(100l, 60l); public static void main(String[] args) { PairTest pairTest = new PairTest(); pairTest.test1(); pairTest.test2(); } public void test1() { System.out.println("pair" + pair); //pair(100,60) Pair<Long, Long> newPair = Pair.of(3000l, 30l); System.out.println("newPair" + newPair); //newPair(3000,30) System.out.println(newPair != pair); //true System.out.println(pair.compareTo(newPair)); //-1 } public void test2() { System.out.println("pair" + pair); //pair(100,60) Pair<Long, Long> newPair = null; System.out.println("newPair" + newPair); //newPairnull System.out.println(newPair != pair); //true System.out.println(pair.compareTo(newPair)); //Exception in thread "main" java.lang.NullPointerException } }