Java元組Tuple使用實例


一.為什么使用元組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
    }
}

 

參考文章:http://50vip.com/article/35


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM