Class IntRangeSet
- All Implemented Interfaces:
Cloneable,Iterable<Integer>,Collection<Integer>,NavigableSet<Integer>,Set<Integer>,SortedSet<Integer>,IntOrderedIterable
Set implementation based on ordered and disjoint integer ranges.
This set is optimized for integer sets where ranges of consecutive integers
are common. Instead of storing individual integer values, it stores an
ordered list of IntRange objects whose ranges do not overlap. The
ranges() method can be used to get the list of ranges.
- Since:
- 1.58
- Version:
- 1.0
- Author:
- matt
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.IntRangeSet(int initialCapacity) Construct with an initial capacity.Construct from an existing set of values.IntRangeSet(IntRange... ranges) Construct from an existing set of ranges. -
Method Summary
Modifier and TypeMethodDescriptionbooleanadd(int v) Add a single integer to this set.booleanAdd an integer to this set.booleanaddAll(Collection<? extends Integer> col) booleanaddRange(int min, int max) Add a range of integers, inclusive.booleanAdd a range of integers, inclusive.voidclear()clone()Comparator<? super Integer>first()voidforEachOrdered(int min, int max, IntConsumer action) Iterate over a range of values in this collection.voidforEachOrdered(IntConsumer action) Iterate over all primitive values in this collection.Get an immutable copy of this set.booleanisEmpty()iterator()last()pollLast()ranges()Get the ranges of this set.booleanbooleanremoveAll(Collection<?> c) intsize()toString()Methods inherited from class java.util.AbstractSet
equals, hashCodeMethods inherited from class java.util.AbstractCollection
contains, containsAll, retainAll, toArray, toArrayMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.Set
contains, containsAll, equals, hashCode, retainAll, toArray, toArrayMethods inherited from interface java.util.SortedSet
spliterator
-
Constructor Details
-
IntRangeSet
public IntRangeSet()Default constructor. -
IntRangeSet
public IntRangeSet(int initialCapacity) Construct with an initial capacity.- Parameters:
initialCapacity- the initial capacity, which refers to the number of discreet ranges, not elements
-
IntRangeSet
Construct from an existing set of values.- Parameters:
c- the integer collection to copy into this set
-
IntRangeSet
Construct from an existing set of ranges.- Parameters:
ranges- the ranges to copy into this set
-
-
Method Details
-
immutableCopy
Get an immutable copy of this set.- Returns:
- an immutable copy of this set
-
clone
-
toString
- Overrides:
toStringin classAbstractCollection<Integer>
-
forEachOrdered
Description copied from interface:IntOrderedIterableIterate over all primitive values in this collection.This method of iteration can be more efficient than iterating via the
IteratorAPI because no unboxing of primitives is necessary.- Specified by:
forEachOrderedin interfaceIntOrderedIterable- Parameters:
action- the consumer to handle the values
-
forEachOrdered
Description copied from interface:IntOrderedIterableIterate over a range of values in this collection.This method of iteration can be more efficient than iterating via the
IteratorAPI because no unboxing of primitives is necessary.- Specified by:
forEachOrderedin interfaceIntOrderedIterable- Parameters:
min- the minimum key value (inclusive)max- the maximum key value (exclusive)action- the consumer to handle the values
-
add
Add an integer to this set.- Specified by:
addin interfaceCollection<Integer>- Specified by:
addin interfaceSet<Integer>- Overrides:
addin classAbstractCollection<Integer>- See Also:
-
add
public boolean add(int v) Add a single integer to this set.This method requires a linear search of all existing discreet ranges to maintain ordering and possibly merge the value into an existing range or cause two ranges to merge together.
- Parameters:
v- the integer to add- Returns:
- true if this set changed as a result
-
addAll
- Specified by:
addAllin interfaceCollection<Integer>- Specified by:
addAllin interfaceSet<Integer>- Overrides:
addAllin classAbstractCollection<Integer>
-
addRange
public boolean addRange(int min, int max) Add a range of integers, inclusive.- Parameters:
min- the starting value to addmax- the last value to add- Returns:
- true if any changes resulted from adding the given range
- See Also:
-
addRange
Add a range of integers, inclusive.This method requires a linear search of all existing discreet ranges to maintain ordering and possibly merge the given range into an existing range or cause existing ranges to merge together.
- Parameters:
range- the range to add- Returns:
- true if any changes resulted from adding the given range
-
clear
public void clear()- Specified by:
clearin interfaceCollection<Integer>- Specified by:
clearin interfaceSet<Integer>- Overrides:
clearin classAbstractCollection<Integer>
-
iterator
- Specified by:
iteratorin interfaceCollection<Integer>- Specified by:
iteratorin interfaceIterable<Integer>- Specified by:
iteratorin interfaceNavigableSet<Integer>- Specified by:
iteratorin interfaceSet<Integer>- Specified by:
iteratorin classAbstractCollection<Integer>
-
ranges
Get the ranges of this set.This returns a "live" reference to the ranges in this set; mutations to this set will impact the values in the returned collection.
- Returns:
- the disjoint ranges in this set, ordered from least to greatest, never null
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<Integer>- Specified by:
isEmptyin interfaceSet<Integer>- Overrides:
isEmptyin classAbstractCollection<Integer>
-
size
public int size()- Specified by:
sizein interfaceCollection<Integer>- Specified by:
sizein interfaceSet<Integer>- Specified by:
sizein classAbstractCollection<Integer>
-
comparator
- Specified by:
comparatorin interfaceSortedSet<Integer>
-
first
-
last
-
lower
- Specified by:
lowerin interfaceNavigableSet<Integer>
-
floor
- Specified by:
floorin interfaceNavigableSet<Integer>
-
ceiling
- Specified by:
ceilingin interfaceNavigableSet<Integer>
-
higher
- Specified by:
higherin interfaceNavigableSet<Integer>
-
remove
- Specified by:
removein interfaceCollection<Integer>- Specified by:
removein interfaceSet<Integer>- Overrides:
removein classAbstractCollection<Integer>
-
removeAll
- Specified by:
removeAllin interfaceCollection<Integer>- Specified by:
removeAllin interfaceSet<Integer>- Overrides:
removeAllin classAbstractSet<Integer>
-
pollFirst
- Specified by:
pollFirstin interfaceNavigableSet<Integer>
-
pollLast
- Specified by:
pollLastin interfaceNavigableSet<Integer>
-
descendingSet
- Specified by:
descendingSetin interfaceNavigableSet<Integer>
-
descendingIterator
- Specified by:
descendingIteratorin interfaceNavigableSet<Integer>
-
subSet
-
subSet
public NavigableSet<Integer> subSet(Integer fromElement, boolean fromInclusive, Integer toElement, boolean toInclusive) - Specified by:
subSetin interfaceNavigableSet<Integer>
-
headSet
- Specified by:
headSetin interfaceNavigableSet<Integer>
-
tailSet
- Specified by:
tailSetin interfaceNavigableSet<Integer>
-
headSet
-
tailSet
-