原文链接:http://www.cnblogs.com/peida/p/Guava_Range.html  

在Guava中新增了一个新的类型Range,从名字就可以了解到,这个是和区间有关的数据结构。从Google官方文档可以得到定义:Range定义了连续跨度的范围边界,这个连续跨度是一个可以比较的类型(Comparable type)。比如1到100之间的整型数据。

  在数学里面的范围是有边界和无边界之分的;同样,在Guava中也有这个说法。如果这个范围是有边界的,那么这个范围又可以分为包括开集(不包括端点)和闭集(包括端点);如果是无解的可以用+∞表示。如果枚举的话,一共有九种范围表示:

Guava Range 概念,范围和方法
概念 表示范围 guava对应功能方法
(a..b) {x | a < x < b} open(C, C)
[a..b] {x | a <= x <= b}  closed(C, C)
[a..b) {x | a <= x < b} closedOpen(C, C)
(a..b] {x | a < x <= b} openClosed(C, C)
(a..+∞) {x | x > a} greaterThan(C)
[a..+∞) {x | x >= a} atLeast(C)
(-∞..b) {x | x < b} lessThan(C)
(-∞..b] {x | x <= b} atMost(C)
(-∞..+∞) all values all()

  上表中的guava对应功能方法那一栏表示Range类提供的方法,分别来表示九种可能出现的范围区间。如果区间两边都存在范围,在这种情况下,区间右边的数不可能比区间左边的数小。在极端情况下,区间两边的数是相等的,但前提条件是最少有一个边界是闭集的,否则是不成立的。比如:
  [a..a] : 里面只有一个数a;
  [a..a); (a..a] : 空的区间范围,但是是有效的;
  (a..a) : 这种情况是无效的,构造这样的Range将会抛出异常。
  在使用Range时需要注意:在构造区间时,尽量使用不可改变的类型。如果你需要使用可变的类型,在区间类型构造完成的情况下,请不要改变区间两边的数。

  实例:

public class TestBaseRange {
@Test
public void testRange(){
System.out.println("open:"+Range.open(1, 10));
System.out.println("closed:"+ Range.closed(1, 10));
System.out.println("closedOpen:"+ Range.closedOpen(1, 10));
System.out.println("openClosed:"+ Range.openClosed(1, 10));
System.out.println("greaterThan:"+ Range.greaterThan(10));
System.out.println("atLeast:"+ Range.atLeast(10));
System.out.println("lessThan:"+ Range.lessThan(10));
System.out.println("atMost:"+ Range.atMost(10));
System.out.println("all:"+ Range.all());
System.out.println("closed:"+Range.closed(10, 10));
System.out.println("closedOpen:"+Range.closedOpen(10, 10));
//会抛出异常
System.out.println("open:"+Range.open(10, 10));
}
}

  此外,范围可以构造实例通过绑定类型显式,例如:

public class TestBaseRange {

    @Test
public void testRange(){
System.out.println("downTo:"+Range.downTo(4, BoundType.OPEN));
System.out.println("upTo:"+Range.upTo(4, BoundType.CLOSED));
System.out.println("range:"+Range.range(1, BoundType.CLOSED, 4, BoundType.OPEN));
}
}

  输出:

downTo:(4‥+∞)
upTo:(-∞‥4]
range:[1‥4)

  操作方法

  1.contains:判断值是否在当前Range内

    @Test
public void testContains(){
System.out.println(Range.closed(1, 3).contains(2));
System.out.println(Range.closed(1, 3).contains(4));
System.out.println(Range.lessThan(5).contains(5));
System.out.println(Range.closed(1, 4).containsAll(Ints.asList(1, 2, 3)));
}   //=====输出=====
  true
  false
  false
  true

  2.Endpoint相关查询方法:

    @Test
public void testQuery(){
System.out.println("hasLowerBound:"+Range.closedOpen(4, 4).hasLowerBound());
System.out.println("hasUpperBound:"+Range.closedOpen(4, 4).hasUpperBound());
System.out.println(Range.closedOpen(4, 4).isEmpty());
System.out.println(Range.openClosed(4, 4).isEmpty());
System.out.println(Range.closed(4, 4).isEmpty());
// Range.open throws IllegalArgumentException
//System.out.println(Range.open(4, 4).isEmpty()); System.out.println(Range.closed(3, 10).lowerEndpoint());
System.out.println(Range.open(3, 10).lowerEndpoint());
System.out.println(Range.closed(3, 10).upperEndpoint());
System.out.println(Range.open(3, 10).upperEndpoint());
System.out.println(Range.closed(3, 10).lowerBoundType());
System.out.println(Range.open(3, 10).upperBoundType());
}   //======输出=======
  hasLowerBound:true
  hasUpperBound:true
  true
  true
  false
  3
  3
  10
  10
  CLOSED
  OPEN

  3.encloses方法:encloses(Range range)中的range是否包含在需要比较的range中

    @Test
public void testEncloses(){
Range<Integer> rangeBase=Range.open(1, 4);
Range<Integer> rangeClose=Range.closed(2, 3);
Range<Integer> rangeCloseOpen=Range.closedOpen(2, 4);
Range<Integer> rangeCloseOther=Range.closedOpen(2, 5);
System.out.println("rangeBase: "+rangeBase+" Enclose:"+rangeBase.encloses(rangeClose)+" rangeClose:"+rangeClose);
System.out.println("rangeBase: "+rangeBase+" Enclose:"+rangeBase.encloses(rangeCloseOpen)+" rangeClose:"+rangeCloseOpen);
System.out.println("rangeBase: "+rangeBase+" Enclose:"+rangeBase.encloses(rangeCloseOther)+" rangeClose:"+rangeCloseOther);
}   //=======输出========
  rangeBase: (1‥4) Enclose:true rangeClose:[2‥3]
  rangeBase: (1‥4) Enclose:true rangeClose:[2‥4)
  rangeBase: (1‥4) Enclose:false rangeClose:[2‥5)

  4.isConnected:range是否可连接上

    @Test
public void testConnected(){
System.out.println(Range.closed(3, 5).isConnected(Range.open(5, 10)));
System.out.println(Range.closed(0, 9).isConnected(Range.closed(3, 4)));
System.out.println(Range.closed(0, 5).isConnected(Range.closed(3, 9)));
System.out.println(Range.open(3, 5).isConnected(Range.open(5, 10)));
System.out.println(Range.closed(1, 5).isConnected(Range.closed(6, 10)));
}   //======输出=========
  true
  true
  true
  false
  false

  4.intersection:如果两个range相连时,返回最大交集,如果不相连时,直接抛出异常

    @Test
public void testIntersection(){
System.out.println(Range.closed(3, 5).intersection(Range.open(5, 10)));
System.out.println(Range.closed(0, 9).intersection(Range.closed(3, 4)));
System.out.println(Range.closed(0, 5).intersection(Range.closed(3, 9)));
System.out.println(Range.open(3, 5).intersection(Range.open(5, 10)));
System.out.println(Range.closed(1, 5).intersection(Range.closed(6, 10)));
}   //=======输出=========
  (5‥5]
  [3‥4]
  [3‥5]   注意:第四和第五行代码,当集合不相连时,会直接报错

  5.span:获取两个range的并集,如果两个range是两连的,则是其最小range

    @Test
public void testSpan(){
System.out.println(Range.closed(3, 5).span(Range.open(5, 10)));
System.out.println(Range.closed(0, 9).span(Range.closed(3, 4)));
System.out.println(Range.closed(0, 5).span(Range.closed(3, 9)));
System.out.println(Range.open(3, 5).span(Range.open(5, 10)));
System.out.println(Range.closed(1, 5).span(Range.closed(6, 10)));
System.out.println(Range.closed(1, 5).span(Range.closed(7, 10)));
}   //=====输出=======
  true
  true
  true
  false
  false

Range(转)的更多相关文章

  1. SQL Server 合并复制遇到identity range check报错的解决

        最近帮一个客户搭建跨洋的合并复制,由于数据库非常大,跨洋网络条件不稳定,因此只能通过备份初始化,在初始化完成后向海外订阅端插入数据时发现报出如下错误: Msg 548, Level 16, S ...

  2. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  3. [LeetCode] Range Addition 范围相加

    Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...

  4. [LeetCode] Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  5. [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  6. [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  7. [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  8. [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  9. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  10. C++11中自定义range

    python中的range功能非常好用 for i in range(100): print(i) 现在利用C++11的基于范围的for循环特性实现C++中的range功能 class range { ...

随机推荐

  1. springmvc日期格式化

    jsp页面String类型转Controller后台Date类型 方法1.在实体中加入日期格式化注解 @DateTimeFormat(pattern="yyyy-MM-dd") p ...

  2. 前端开发必备 - Emmet

    介绍 Emmet (前身为 Zen Coding) 是一个能大幅度提高前端开发效率的一个工具. 基本上,大多数的文本编辑器都会允许你存储和重用一些代码块,我们称之为"片段".虽然片 ...

  3. 1: 介绍Prism5.0(纯汉语版)

      Prism帮助更简单的设计丰富,灵活,易维护的WPF桌面程序.其中使用MVVM,组合式视图,事件聚合等设计模式.这很符合一些重要的架构设计及原则.帮助你创建一个模块化的应用程序——可以独立开发松耦 ...

  4. I/O流+统计文件词频

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  5. Jxl的API概述(转)

    一.Jxl的API Jxl的API主要有三个包,jxl,jxl.format,jxl.write.如果单独的分析API,可能对于更明确的了解此API没有太多的帮助,我们还是从Excel文件的层次来剥离 ...

  6. 201621123005《Java程序设计》第四周学习总结

    201621123005<Java程序设计>第四周学习总结 标签(空格分隔): 1.本章学习总结 1. 面向对象设计 1.1 写出你认为本周学习中比较重要的知识点关键词 继承.多态.覆盖. ...

  7. http和https区别

    超文本传输协议 HTTP 协议被用于在 Web 浏览器和网站服务器之间传递信息,HTTP 协议以明文方式发送内容,不提供任何方式的数据加密,如果攻击者截取了 Web 浏览器和网站服务器之间的传输报文, ...

  8. == 和 equals 的用法

    在java中,boolean.byte.short.int.long.char.float.double这八种是基本数据类型,其余都是引用类型. “==”是比较两个变量的值是否相等, “equals” ...

  9. PHPStorm 使用正则批量查询替换并自动转换大小写的方法

    PHPStorm 的项目查询替换功能那是非常非常强大的, 速度也很快, 配合正则更加灵活强大. 一般的正则查询替换没什么太多好说的, 这里主要说说比较少用的 大小写自动转换的问题, 也是比较少用但很有 ...

  10. CF 148D D. Bag of mice (概率DP||数学期望)

    The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests ...