Google Guava学习 -Guava Range类

短命女 2022-11-09 14:10 362阅读 0赞

1.功能:数据范围处理

2.代码:

  1. package com.example.google.guava.demo.clazz;
  2. import com.google.common.collect.ContiguousSet;
  3. import com.google.common.collect.DiscreteDomain;
  4. import com.google.common.collect.Range;
  5. import com.google.common.primitives.Ints;
  6. /**
  7. * <p>
  8. * <code>RangeTest</code>
  9. * </p>
  10. * Description:
  11. *
  12. * @author Mcchu
  13. * @date 2017/10/19 9:06
  14. */
  15. public class RangeTest {
  16. public static void main(String[] args) {
  17. testRange();
  18. }
  19. private static void testRange(){
  20. // 1.使用Range.closed()创建范围: [a,b] = { x | a <= x <= b}
  21. Range<Integer> range1 = Range.closed(0, 9);
  22. System.out.println("新建数据范围range1:"+range1);
  23. printRange(range1);
  24. // 1.1包含关系
  25. Boolean containVal = range1.contains(1);
  26. Boolean containAllVal1 = range1.containsAll(Ints.asList(1,3,5));
  27. Boolean containAllVal2 = range1.containsAll(Ints.asList(1,3,5,12));
  28. System.out.println("是否包含1:"+containVal);
  29. System.out.println("是否包含1,3,5:"+containAllVal1);
  30. System.out.println("是否包含1,3,5,12:"+containAllVal2);
  31. // 1.2边界值
  32. Boolean bol1 = range1.hasLowerBound();
  33. Boolean bol2 = range1.hasUpperBound();
  34. System.out.println("是否存在最小边界值:"+bol1);
  35. System.out.println("是否存在最大边界值:"+bol2);
  36. Integer lower = range1.lowerEndpoint();
  37. Integer upper = range1.upperEndpoint();
  38. System.out.println("最小边界值:"+lower);
  39. System.out.println("最大边界值:"+upper);
  40. System.out.println();
  41. // 2.使用Range.open()创建范围: (a,b) = { x | a < x < b}
  42. Range<Integer> range2 = Range.open(0,9);
  43. System.out.println("新建数据范围range2:"+range2);
  44. printRange(range2);
  45. System.out.println();
  46. // 3.使用Range.openClosed()创建范围: (a,b] = { x | a < x <= b}
  47. Range<Integer> range3 = Range.openClosed(0, 9);
  48. System.out.println("新建数据范围range3:"+range3);
  49. printRange(range3);
  50. System.out.println();
  51. // 4.使用Range.closedOpen()创建范围: [a,b) = { x | a <= x < b}
  52. Range<Integer> range4 = Range.closedOpen(0, 9);
  53. System.out.println("新建数据范围range4:"+range4);
  54. printRange(range4);
  55. System.out.println();
  56. // 5.右无穷大 a>9
  57. Range<Integer> range5 = Range.greaterThan(9);
  58. System.out.println("新建数据范围range5:"+range5);
  59. // 5.1边界值
  60. Boolean bol3 = range5.hasLowerBound();
  61. Boolean bol4 = range5.hasUpperBound();
  62. System.out.println("是否存在最小边界值:"+bol3);
  63. System.out.println("是否存在最大边界值:"+bol4);
  64. Integer lower1 = range5.lowerEndpoint();
  65. //Integer upper1 = range5.upperEndpoint(); //抛java.lang.IllegalStateException: range unbounded on this side
  66. System.out.println("最小边界值:"+lower1);
  67. //System.out.println("最大边界值:"+upper1);
  68. System.out.println();
  69. // 6.子范围
  70. Range<Integer> range6 = Range.closed(3, 5);
  71. System.out.println("新建数据范围:"+range6);
  72. printRange(range6);
  73. Boolean subRange = range1.encloses(range6);
  74. System.out.println("范围range1是否包含range6:"+subRange);
  75. System.out.println();
  76. // 7.承接关系
  77. Range<Integer> range7 = Range.closed(9, 20);
  78. System.out.println("新建数据范围:"+range7);
  79. printRange(range7);
  80. Boolean connected = range7.isConnected(range1);
  81. System.out.println("范围range7是否承接range1:"+connected);
  82. System.out.println();
  83. // 8.范围交叉,取交集、并集
  84. Range<Integer> range8 = Range.closed(5, 15);
  85. Range<Integer> intersection = range1.intersection(range8);
  86. System.out.println("范围range1和range8的交集:"+intersection);
  87. printRange(intersection);
  88. Range<Integer> span = range1.span(range8);
  89. System.out.println("范围range1和range8的并集:"+span);
  90. printRange(span);
  91. }
  92. private static void printRange(Range<Integer> range){
  93. System.out.print("具体数据范围:");
  94. System.out.print("[ ");
  95. for(int grade : ContiguousSet.create(range, DiscreteDomain.integers())) {
  96. System.out.print(grade +" ");
  97. }
  98. System.out.println("]");
  99. }
  100. }

3.输出结果:

  1. 新建数据范围range1:[0..9]
  2. 具体数据范围:[ 0 1 2 3 4 5 6 7 8 9 ]
  3. 是否包含1true
  4. 是否包含135true
  5. 是否包含13512false
  6. 是否存在最小边界值:true
  7. 是否存在最大边界值:true
  8. 最小边界值:0
  9. 最大边界值:9
  10. 新建数据范围range2:(0..9)
  11. 具体数据范围:[ 1 2 3 4 5 6 7 8 ]
  12. 新建数据范围range3:(0..9]
  13. 具体数据范围:[ 1 2 3 4 5 6 7 8 9 ]
  14. 新建数据范围range4:[0..9)
  15. 具体数据范围:[ 0 1 2 3 4 5 6 7 8 ]
  16. 新建数据范围range5:(9..+∞)
  17. 是否存在最小边界值:true
  18. 是否存在最大边界值:false
  19. 最小边界值:9
  20. 新建数据范围:[3..5]
  21. 具体数据范围:[ 3 4 5 ]
  22. 范围range1是否包含range6true
  23. 新建数据范围:[9..20]
  24. 具体数据范围:[ 9 10 11 12 13 14 15 16 17 18 19 20 ]
  25. 范围range7是否承接range1true
  26. 范围range1range8的交集:[5..9]
  27. 具体数据范围:[ 5 6 7 8 9 ]
  28. 范围range1range8的并集:[0..15]
  29. 具体数据范围:[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ]

发表评论

表情:
评论列表 (有 0 条评论,362人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Guava学习笔记:Range

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

    相关 Guava学习笔记:Range

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