Guava RangeSet Example

0

Guava’s Range represents an interval, for example, a < range < b. A RangeSet contains zero or more ranges.

As we add new range to RangeSet, any connected ranges are merged together, and empty ranges are ignored.

Let’s look into a brief description of some of its methods and then an example which makes use of the below methods.

  1. RangeSet.add(range) – Adds the specified range to this RangeSet.
  2. RangeSet.remove(range) – Removes the specified range from this RangeSet.
  3. Ranget.compliment() – Returns a view of the complement of this RangeSet, the compliment set contains all those ranges which are not in the current RangeSet.
  4. RangeSet.span() – Returns the minimal Range that encloses every range in this RangeSet.
  5. RangeSet.subRangeSet(range) – Returns a view of the intersection of this RangeSet with the passed in range.
  6. RangeSet.asRanges() – Returns ranges in the form of Set<Range>.
  7. RangeSet.contains(value) – True if the RangeSet contains the value passed in.

In our example, we will use TreeMap based implementation of RangeSet.

RangeSet rangeSet = TreeRangeSet.create();

RangeSetExample:

package com.javarticles.guava;

import com.google.common.collect.Range;
import com.google.common.collect.RangeSet;
import com.google.common.collect.TreeRangeSet;


public class RangeSetExample {
    
    public static void main(String[] args) {
        RangeSet rangeSet = TreeRangeSet.create();
        System.out.println("Add range 1 < x < 4");
        //2, 3
        rangeSet.add(Range.open(1, 4));
        
        System.out.println("Range: " + rangeSet);
        
        System.out.println("Add range 6 <= x <= 9");
        
        //6, 7, 8, 9
        rangeSet.add(Range.closed(6, 9));
        
        System.out.println("Range: " + rangeSet);
        
        System.out.println("Add range 5 <= x <= 10");
        
        //5, 6, 7, 8, 9, 10
        rangeSet.add(Range.closed(5, 10));
        
        //[2, 3], [5, 6, 7, 8, 9, 10]
        System.out.println("Range: " + rangeSet);
        
        System.out.println("Add range 3 <= x <= 5");
        
        rangeSet.add(Range.closed(3, 5));
        
        //[2, 3, 4, 5, 6, 7, 8, 9, 10]
        System.out.println("Range: " + rangeSet);
        
        System.out.println("Contains 1?" + rangeSet.contains(1));
        System.out.println("Contains 10?" + rangeSet.contains(10));
        
        System.out.println("Range complement: " + rangeSet.complement());
        
        System.out.println("Add range 13 <= x <= 15");
        
        rangeSet.add(Range.closed(13, 15));
        
        System.out.println("Range: " + rangeSet);
        
        System.out.println("Range complement: " + rangeSet.complement());
        
        System.out.println("Remove range 3 <= x <= 5");
        
        rangeSet.remove(Range.closed(3, 5));
        
        System.out.println("Range: " + rangeSet);
        
        System.out.println("rangeSet.span(): " + rangeSet.span());
        
        Range otherRange = Range.closed(6, 14);
        
        System.out.println("Other Range: " + otherRange);
        
        System.out.println("Intersection of ranges: " + rangeSet.subRangeSet(otherRange));
        
        System.out.println("Range set: " + rangeSet.asRanges()); 
    }   
}

Output:

Add range 1 < x < 4
Range: [(1?4)]
Add range 6 <= x <= 9
Range: [(1?4), [6?9]]
Add range 5 <= x <= 10
Range: [(1?4), [5?10]]
Add range 3 <= x <= 5
Range: [(1?10]]
Contains 1?false
Contains 10?true
Range complement: [(-??1], (10?+?)]
Add range 13 <= x <= 15
Range: [(1?10], [13?15]]
Range complement: [(-??1], (10?13), (15?+?)]
Remove range 3 <= x <= 5
Range: [(1?3), (5?10], [13?15]]
rangeSet.span(): (1?15]
Other Range: [6?14]
Intersection of ranges: [[6?10], [13?14]]
Range set: [(1?3), (5?10], [13?15]]

Download the source code

This was an example about Guava RangeSet.

You can download the source code here: guavaRangeSetExample.zip
Share.

Comments are closed.