Guava – Range类

Guava – Range 类

Range代表一个区间或一个序列。它被用来获得一组位于特定范围内的数字/字符串.

Range 类声明

以下是com.google.common.collection.Range <C>类的声明 −

@GwtCompatible
public final class Range<C extends Comparable>
   extends Object
      implements Predicate<C>, Serializable

Range 类函数

序号 函数和描述
1 static <C extends Comparable<?>> Range<C> all()
返回一个包含C类型的每个值的范围.
2 boolean apply(C input)Deprecated.
仅为满足谓词接口而提供;使用contains(C)代替.
3 static <C extends Comparable<?>> Range<C> atLeast(C endpoint)
返回一个包含所有大于或等于端点的值的范围.
4 static <C extends Comparable<?>> Range<C> atMost(C endpoint)
返回一个包含所有小于或等于端点的值的范围.
5 Range<C> canonical(DiscreteDomain<C> domain)
返回这个范围在给定域中的经典形式.
6 static <C extends Comparable<?>> Range<C> closed(C lower, C upper)
返回一个包含所有大于或等于下限和小于或等于上限的数值的范围.
7 static <C extends Comparable<?>> Range<C> closedOpen(C lower, C upper)
返回一个包含所有大于或等于下限且严格小于上限的值的范围.
8 boolean contains(C value)
如果值在这个范围内,则返回真.
9 boolean containsAll(Iterable<? extends C> values)
如果values中的每个元素都包含在这个范围内,则返回true.
10 static <C extends Comparable<?>> Range<C> downTo(C endpoint, BoundType boundType)
返回一个从给定端点开始的范围,这个范围可以是包容的(封闭的)或排他的(开放的),没有上限.
11 static <C extends Comparable<?>> Range<C> encloseAll(Iterable<C> values)
返回包含所有给定值的最小范围.
12 boolean encloses(Range<C> other)
如果其他人的边界没有延伸到这个范围的边界之外,则返回真.
13 boolean equals(Object object)
如果对象是一个具有与此范围相同的端点和绑定类型的范围,则返回真.
14 static <C extends Comparable<?>> Range<C> greaterThan(C endpoint)
返回一个包含所有严格大于端点的值的范围.
15 int hashCode()
返回该范围的哈希代码.
16 boolean hasLowerBound()
如果这个范围有一个较低的端点,则返回true.
17 boolean hasUpperBound()
如果这个范围有一个上端点,则返回true.
18 Range<C> intersection(Range<C> connectedRange)
如果存在这样的范围,返回这个范围和connectedRange所包围的最大范围.
19 boolean isConnected(Range<C> other)
如果存在一个(可能是空的)范围,并被这个范围和其他范围所包围,则返回真.
20 boolean isEmpty()
如果这个范围是[v…v]或(v…v)的形式,返回真.
21 static <C extends Comparable<?>> Range<C> lessThan(C endpoint)
返回一个包含所有严格小于端点的值的范围.
22 BoundType lowerBoundType()
返回此范围的下限的类型。如果该范围包括其下端点,则返回BoundType.CLOSED,如果不包括则返回BoundType.OPEN.
23 C lowerEndpoint()
返回该范围的下端点.
24 static <C extends Comparable<?>> Range<C> open(C lower, C upper)
返回一个包含所有严格大于下限和严格小于上限的值的范围.
25 static <C extends Comparable<?>> Range<C> openClosed(C lower, C upper)
返回一个包含所有严格大于下限且小于或等于上限的数值的范围.
26 static <C extends Comparable<?>> Range<C> range(C lower, BoundType lowerType, C upper, BoundType upperType)
返回一个包含从低到高任何数值的范围,其中每个端点可以是包容的(封闭的)或排他的(开放的).
27 static <C extends Comparable<?>> Range<C> singleton(C value)
返回一个只包含给定值的范围.
28 Range<C> span(Range<C> other)
返回包含这个范围和其他范围的最小范围.
29 String toString()
返回这个范围的字符串表示,例如”[3…5]” (其他例子在类的文档中列出).
30 BoundType upperBoundType()
返回此范围的上界的类型。如果该范围包括其上端点,则返回BoundType.CLOSED;如果不包括,则返回BoundType.OPEN.
31 C upperEndpoint()
返回该范围的上端点.
32 static <C extends Comparable<?>> Range<C> upTo(C endpoint, BoundType boundType)
返回一个没有下限的范围,直到给定的端点,这个范围可以是包容的(封闭的)或排他的(开放的).

Range 类继承

该类继承了以下类的方法 −

  • java.lang.Object

Range 类示例

C:/ >Guava中使用你选择的任何编辑器创建以下java程序

GuavaTester.java

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("]");
   }
}

Range 类验证结果

使用javac编译器编译该类,如下所示 −

C:\Guava>javac GuavaTester.java

现在运行GuavaTester来看看结果.

C:\Guava>java GuavaTester

查看结果.

[0,9] : [ 0 1 2 3 4 5 6 7 8 9 ]
5 is present: true
(1,2,3) is present: true
Lower Bound: 0
Upper Bound: 9
(0,9) : [ 1 2 3 4 5 6 7 8 ]
(0,9] : [ 1 2 3 4 5 6 7 8 9 ]
[0,9) : [ 0 1 2 3 4 5 6 7 8 ]
(9,infinity) : 
Lower Bound: 9
Upper Bound present: false
[ 3 4 5 ]
[0,9] encloses [3,5]:true
[ 9 10 11 12 13 14 15 16 17 18 19 20 ]
[0,9] is connected [9,20]:true
[ 5 6 7 8 9 ]
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ]

赞(1)

评论 抢沙发

评论前必须登录!

 

Guava 教程
Guava 教程Guava - 概述Guava - 环境搭建Guava - Optional类Guava - Preconditions类Guava - Ordering类Guava - Objects类Guava - Range类Guava - Throwables类Guava - Collections 工具类Guava - Caching 工具类Guava - Multiset接口Guava - bimapGuava - MultiMap
Guava Ints类
Guava Ints类Guava Ints - concat() 函数Guava Ints - lastIndexOf() 函数Guava Ints - contains() 函数Guava Ints - asList() 函数Guava Ints - max() 函数Guava Ints - min() 函数Guava Ints - join() 函数Guava Ints - toArray() 函数Guava Ints - indexOf() 函数
Guava Chars类
Guava Chars类Guava - Chars.compare()方法与实例Guava - Chars.hashCode()方法与实例Guava - Chars.min()方法与实例Guava - Chars.max()方法与实例Guava - Chars.toArray()方法与实例Guava - Chars.concat()方法与实例Guava - Chars.contains()方法与实例Guava - Chars.lastIndexOf()方法与实例Guava - Chars.join()方法与实例Guava - Chars.indexOf()方法与实例Guava - Chars.asList()方法与实例
Guava LongMath类
Guava LongMath类Guava - LongMath.binomial方法与实例Guava - LongMath.log2方法与实例Guava - LongMath.pow方法与实例Guava - LongMath.isPowerOfTwo方法与实例Guava - LongMath.gcd方法与实例Guava - LongMath.mean方法与实例Guava - LongMath.sqrt方法与实例Guava - LongMath.mod方法与实例Guava - LongMath.log10方法与实例Guava - LongMath.checkedMultiply方法与实例Guava - LongMath.checkedAdd方法与实例Guava - LongMath.checkedPow方法与实例Guava - LongMath.divide方法与实例
Guava Longs类
Guava Longs类Guava - Longs.compare()方法与实例Guava - Longs.hashCode()方法与实例Guava - Longs.factorial方法与实例Guava - Longs.contains()方法与实例Guava - Longs.min()方法与实例Guava - Longs.max()方法与实例Guava - Longs.toArray()方法与实例Guava - Longs.lastIndexOf()方法与实例Guava - Longs.join()方法与实例Guava - Longs.asList()方法与实例Guava - Longs.checkedSubtract()方法与实例Guava - Longs.concat()方法与实例Guava - Longs.indexOf()方法与实例
Guava Booleans类
Guava Booleans类Guava - Booleans.asList()方法及示例Guava - Booleans.compare()方法及示例Guava - Booleans.concat()方法及示例Guava - Booleans.contains()方法及示例Guava - Booleans.countTrue()方法及示例Guava - Booleans.hashCode()方法及示例Guava - Booleans.indexOf()方法及示例Guava - Booleans.join()方法及示例Guava - Booleans.lastIndexOf()方法及示例Guava - Booleans.toArray()方法及示例
Guava Shorts类
Guava Shorts类Guava - Shorts.toArray()方法及实例Guava - Shorts.min()方法及实例Guava - Shorts.max()方法及实例Guava - Shorts.lastIndexOf()方法及实例Guava - Shorts.join()方法及实例Guava - Shorts.indexOf()方法及实例Guava - Shorts.hashCode()方法及实例Guava - Shorts.contains()方法及实例Guava - Shorts.concat()方法及实例Guava - Shorts.asList()方法及实例
Guava IntMath类
Guava IntMath类Guava - IntMath.checkedAdd()方法及实例Guava - IntMath.checkedMultiply()方法及实例Guava - IntMath.checkedPow()方法及实例Guava - IntMath.checkedSubtract()方法及实例Guava - IntMath.divide()方法及实例Guava - IntMath.log10()方法及实例