com.google.common.collect.Range 集合计算

com.google.common.collect.Range 集合计算
强烈推介IDEA2021.1.3破解激活,IntelliJ IDEA 注册码,2021.1.3IDEA 激活码  

大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说com.google.common.collect.Range 集合计算,希望能够帮助大家进步!!!

1.构造Range实例必须使用Range中的静态方法,因为Range的构造方法被设置为private,当a > b使,或者当a == b且为(a,b)时会抛出IllegalArgumentException

 

                (a,b)           Range.open(a, b)
               (a, b]           Range.openClosed(a, b)
               [a, b]           Range.Closed(a, b)
               [a, b)           Range.closedOpen(a, b)
             [a, +∞)           Range.atLeast(a)
             (a, +∞)           Range.greatThan(a)
             (-∞, b]           Range.atMost(b)
             (-∞, b)           Range.lessThan(b)
            (-∞, +∞)           Range.all()

 

 

2. Range的一些常用方法

    <1>. contains()方法用来判断区间是否包含一个给定的值

       Range.closed(13).contains(2);//return true

       Range.closed(13).contains(4);//return false

  <2>.hasLowerBound()方法和hasUpperBound()方法是用来判断边界是否为无穷

       Range.greaterThan(3).hasLowerBound() //return true

       Range.greaterThan(3).hasUpperBound() //return false

       Range.lessThan(3).hasLowerBound() //return false

       Range.lessThan(3).hasUpperBound() //return true  

  <3>.lowerBoundType()方法和upperBoundType()方法用来返回边界类型(OPEN, CLOSED),

若边界是无穷,则抛出 IllegalStateException

  <4>.lowerEndpoint()和upperEndpoint():返回区间的端点值;如果区间的端点值为无穷,抛出                 IllegalStateException

  <5>.isconnect()判断连个区间是否能连在一起

  <6>.intersection(Range)返回两个区间的交集:既包含于第一个区间,又包含于另一个区间的最大区间。当且仅当两个区间是相连的,它们才有交集。如果两个区间没有交集,该方法将抛出IllegalArgumentException。

      <7>.span(Range)返回”同时包括两个区间的最小区间”

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

public class GuavaTester {

   public static void main(String args[]){
      GuavaTester tester = new GuavaTester();
      tester.testRange();
   }

   private void testRange(){

      //create a range [a,b] = { x | a <= x <= b}
      Range<Integer> range1 = Range.closed(0, 9);	
      System.out.print("[0,9] : ");
      printRange(range1);		
      System.out.println("5 is present: " + range1.contains(5));
      System.out.println("(1,2,3) is present: " + range1.containsAll(Ints.asList(1, 2, 3)));
      System.out.println("Lower Bound: " + range1.lowerEndpoint());
      System.out.println("Upper Bound: " + range1.upperEndpoint());

      //create a range (a,b) = { x | a < x < b}
      Range<Integer> range2 = Range.open(0, 9);
      System.out.print("(0,9) : ");
      printRange(range2);

      //create a range (a,b] = { x | a < x <= b}
      Range<Integer> range3 = Range.openClosed(0, 9);
      System.out.print("(0,9] : ");
      printRange(range3);

      //create a range [a,b) = { x | a <= x < b}
      Range<Integer> range4 = Range.closedOpen(0, 9);
      System.out.print("[0,9) : ");
      printRange(range4);

      //create an open ended range (9, infinity
      Range<Integer> range5 = Range.greaterThan(9);
      System.out.println("(9,infinity) : ");
      System.out.println("Lower Bound: " + range5.lowerEndpoint());
      System.out.println("Upper Bound present: " + range5.hasUpperBound());

      Range<Integer> range6 = Range.closed(3, 5);	
      printRange(range6);

      //check a subrange [3,5] in [0,9]
      System.out.println("[0,9] encloses [3,5]:" + range1.encloses(range6));

      Range<Integer> range7 = Range.closed(9, 20);	
      printRange(range7);
      //check ranges to be connected		
      System.out.println("[0,9] is connected [9,20]:" + range1.isConnected(range7));

      Range<Integer> range8 = Range.closed(5, 15);	

      //intersection
      printRange(range1.intersection(range8));

      //span
      printRange(range1.span(range8));
   }

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

 

本文来源huayang183,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/17963

发表评论