A new Range type is added to Guava, and the data structure related to the Range is known from the name. According to the official Google Documents, Range defines the Range boundary of the continuous span, which is a Comparable type ). For example, integer data between 1 and 100.
In mathematics, the range is bounded and borderless. Similarly, Guava also has this argument. If the range is bounded, it can be divided into an open set (excluding the endpoint) and a closed set (including the endpoint). If there is no solution, it can be expressed with + ∞. If enumeration is performed, a total of nine ranges are used:
Guava Range concept, Range and Method
Concept |
Range |
Function Methods of guava |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The "guava" column in the preceding table indicates the methods provided by the Range class, which respectively indicate nine possible Range ranges. If there is a range on both sides of the range, in this case, the number on the right of the range cannot be smaller than the number on the left of the range. In extreme cases, the numbers on both sides of the interval are equal, but the precondition is that at least one boundary is closed, otherwise it is not true. For example:
[A. a]: There is only one number;
[A. a); (a. a]: An empty range, but valid;
(A. a): This is invalid. Constructing a Range will throw an exception.
When using Range, note that you should try to use unchangeable types when constructing intervals. If you need a variable type, do not change the number of both sides of the interval when the interval type is constructed.
@Test "open:"+Range.open(1, 10"closed:"+ Range.closed(1, 10"closedOpen:"+ Range.closedOpen(1, 10"openClosed:"+ Range.openClosed(1, 10"greaterThan:"+ Range.greaterThan(10"atLeast:"+ Range.atLeast(10"lessThan:"+ Range.lessThan(10"atMost:"+ Range.atMost(10"all:"+"closed:"+Range.closed(10, 10"closedOpen:"+Range.closedOpen(10, 10 System.out.println("open:"+Range.open(10, 10
In addition, the range can be used to construct an instance explicitly by binding the type, for example:
"downTo:"+Range.downTo(4"upTo:"+Range.upTo(4"range:"+Range.range(1, BoundType.CLOSED, 4
Output:
downTo:(4‥+-∞‥41‥4)
Procedure
1. contains: determines whether the value is in the current Range.
1, 3).contains(21, 3).contains(45).contains(51, 4).containsAll(Ints.asList(1, 2, 3
2. How to query the Endpoint:
"hasLowerBound:"+Range.closedOpen(4, 4"hasUpperBound:"+Range.closedOpen(4, 44, 44, 44, 43, 103, 103, 103, 103, 103, 10 hasLowerBound: 3 3 10 10
3. encloses method: whether the Range in encloses (range) is included in the range to be compared
<Integer> rangeBase=Range.open(1, 4<Integer> rangeClose=Range.closed(2, 3<Integer> rangeCloseOpen=Range.closedOpen(2, 4<Integer> rangeCloseOther=Range.closedOpen(2, 5"rangeBase: "+rangeBase+" Enclose:"+rangeBase.encloses(rangeClose)+" rangeClose:"+"rangeBase: "+rangeBase+" Enclose:"+rangeBase.encloses(rangeCloseOpen)+" rangeClose:"+"rangeBase: "+rangeBase+" Enclose:"+rangeBase.encloses(rangeCloseOther)+" rangeClose:"+ rangeBase: (1‥4) Enclose: rangeClose:[2‥31‥4) Enclose: rangeClose:[2‥41‥4) Enclose: rangeClose:[2‥5)
4. isConnected: whether range can be connected
3, 5).isConnected(Range.open(5, 100, 9).isConnected(Range.closed(3, 40, 5).isConnected(Range.closed(3, 93, 5).isConnected(Range.open(5, 101, 5).isConnected(Range.closed(6, 10
4. intersection: If two ranges are connected, the maximum intersection is returned. If they are not connected, an exception is thrown.
3, 5).intersection(Range.open(5, 100, 9).intersection(Range.closed(3, 40, 5).intersection(Range.closed(3, 93, 5).intersection(Range.open(5, 101, 5).intersection(Range.closed(6, 10 (5‥53‥43‥5
5. span: obtains the union of two ranges. If two ranges are connected, the minimum range is used.
3, 5).span(Range.open(5, 100, 9).span(Range.closed(3, 40, 5).span(Range.closed(3, 93, 5).span(Range.open(5, 101, 5).span(Range.closed(6, 101, 5).span(Range.closed(7, 10