天天看點

Google Guava學習(4)-Guava Range類

1.功能:資料範圍處理

2.代碼:

package com.example.google.guava.demo.clazz;

import com.google.common.collect.ContiguousSet;
import com.google.common.collect.DiscreteDomain;
import com.google.common.collect.Range;
import com.google.common.primitives.Ints;

/**
 * <p>
 * <code>RangeTest</code>
 * </p>
 * Description:
 *
 * @author Mcchu
 * @date 2017/10/19 9:06
 */
public class RangeTest {

    public static void main(String[] args) {
        testRange();
    }

    private static void testRange(){
        // 1.使用Range.closed()建立範圍: [a,b] = { x | a <= x <= b}
        Range<Integer> range1 = Range.closed(0, 9);
        System.out.println("建立資料範圍range1:"+range1);
        printRange(range1);

        // 1.1包含關系
        Boolean containVal = range1.contains(1);
        Boolean containAllVal1 = range1.containsAll(Ints.asList(1,3,5));
        Boolean containAllVal2 = range1.containsAll(Ints.asList(1,3,5,12));
        System.out.println("是否包含1:"+containVal);
        System.out.println("是否包含1,3,5:"+containAllVal1);
        System.out.println("是否包含1,3,5,12:"+containAllVal2);

        // 1.2邊界值
        Boolean bol1 = range1.hasLowerBound();
        Boolean bol2 = range1.hasUpperBound();
        System.out.println("是否存在最小邊界值:"+bol1);
        System.out.println("是否存在最大邊界值:"+bol2);

        Integer lower = range1.lowerEndpoint();
        Integer upper = range1.upperEndpoint();
        System.out.println("最小邊界值:"+lower);
        System.out.println("最大邊界值:"+upper);
        System.out.println();


        // 2.使用Range.open()建立範圍: (a,b) = { x | a < x < b}
        Range<Integer> range2 = Range.open(0,9);
        System.out.println("建立資料範圍range2:"+range2);
        printRange(range2);
        System.out.println();


        // 3.使用Range.openClosed()建立範圍: (a,b] = { x | a < x <= b}
        Range<Integer> range3 = Range.openClosed(0, 9);
        System.out.println("建立資料範圍range3:"+range3);
        printRange(range3);
        System.out.println();


        // 4.使用Range.closedOpen()建立範圍: [a,b) = { x | a <= x < b}
        Range<Integer> range4 = Range.closedOpen(0, 9);
        System.out.println("建立資料範圍range4:"+range4);
        printRange(range4);
        System.out.println();


        // 5.右無窮大 a>9
        Range<Integer> range5 = Range.greaterThan(9);
        System.out.println("建立資料範圍range5:"+range5);

        // 5.1邊界值
        Boolean bol3 = range5.hasLowerBound();
        Boolean bol4 = range5.hasUpperBound();
        System.out.println("是否存在最小邊界值:"+bol3);
        System.out.println("是否存在最大邊界值:"+bol4);

        Integer lower1 = range5.lowerEndpoint();
        //Integer upper1 = range5.upperEndpoint(); //抛java.lang.IllegalStateException: range unbounded on this side
        System.out.println("最小邊界值:"+lower1);
        //System.out.println("最大邊界值:"+upper1);
        System.out.println();

        // 6.子範圍
        Range<Integer> range6 = Range.closed(3, 5);
        System.out.println("建立資料範圍:"+range6);
        printRange(range6);
        Boolean subRange = range1.encloses(range6);
        System.out.println("範圍range1是否包含range6:"+subRange);
        System.out.println();


        // 7.承接關系
        Range<Integer> range7 = Range.closed(9, 20);
        System.out.println("建立資料範圍:"+range7);
        printRange(range7);
        Boolean connected = range7.isConnected(range1);
        System.out.println("範圍range7是否承接range1:"+connected);
        System.out.println();


        // 8.範圍交叉,取交集、并集
        Range<Integer> range8 = Range.closed(5, 15);
        Range<Integer> intersection = range1.intersection(range8);
        System.out.println("範圍range1和range8的交集:"+intersection);
        printRange(intersection);
        Range<Integer> span = range1.span(range8);
        System.out.println("範圍range1和range8的并集:"+span);
        printRange(span);

    }

    private static void printRange(Range<Integer> range){
        System.out.print("具體資料範圍:");
        System.out.print("[ ");
        for(int grade : ContiguousSet.create(range, DiscreteDomain.integers())) {
            System.out.print(grade +" ");
        }
        System.out.println("]");
    }
}
           

2.輸出結果:

建立資料範圍range1:[0..9]
具體資料範圍:[ 0 1 2 3 4 5 6 7 8 9 ]
是否包含1:true
是否包含1,3,5:true
是否包含1,3,5,12:false
是否存在最小邊界值:true
是否存在最大邊界值:true
最小邊界值:0
最大邊界值:9

建立資料範圍range2:(0..9)
具體資料範圍:[ 1 2 3 4 5 6 7 8 ]

建立資料範圍range3:(0..9]
具體資料範圍:[ 1 2 3 4 5 6 7 8 9 ]

建立資料範圍range4:[0..9)
具體資料範圍:[ 0 1 2 3 4 5 6 7 8 ]

建立資料範圍range5:(9..+∞)
是否存在最小邊界值:true
是否存在最大邊界值:false
最小邊界值:9

建立資料範圍:[3..5]
具體資料範圍:[ 3 4 5 ]
範圍range1是否包含range6:true

建立資料範圍:[9..20]
具體資料範圍:[ 9 10 11 12 13 14 15 16 17 18 19 20 ]
範圍range7是否承接range1:true

範圍range1和range8的交集:[5..9]
具體資料範圍:[ 5 6 7 8 9 ]
範圍range1和range8的并集:[0..15]
具體資料範圍:[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ]
           

附錄:

參考:http://www.yiibai.com/guava/guava_range_class.html

繼續閱讀