Package com.github.tommyettinger.gand.ds
Interface IntCollection
public interface IntCollection
Analogous to
Collection
but for primitive ints, this is built around
the primitive-specialized iterator IntIterator
instead of the generic Iterator
.
This is not necessarily a modifiable collection.-
Method Summary
Modifier and TypeMethodDescriptionboolean
add
(int item) default boolean
addAll
(int[] array) default boolean
addAll
(int[] array, int offset, int length) default boolean
addAll
(IntCollection other) default boolean
Compares this IntCollection with another IntCollection by checking their identity, their types (both must implement IntCollection), and their sizes, before checking if other contains each item in this IntCollection, in any order or quantity.void
clear()
boolean
contains
(int item) default boolean
containsAll
(int[] array) default boolean
containsAll
(int[] array, int offset, int length) default boolean
containsAll
(IntCollection other) default boolean
containsAny
(int[] array) default boolean
containsAny
(int[] array, int offset, int length) default boolean
containsAny
(IntCollection other) boolean
default int
first()
Attempts to get the first item in this PrimitiveCollection, where "first" is only defined meaningfully if this type is ordered.default void
forEach
(IntConsumer action) Performs the given action for each element of theIntCollection
until all elements have been processed or the action throws an exception.int
hashCode()
default boolean
isEmpty()
iterator()
default boolean
notEmpty()
boolean
remove
(int item) default boolean
removeAll
(int[] array) default boolean
removeAll
(int[] array, int offset, int length) default boolean
removeAll
(IntCollection other) Removes from this collection all occurrences of any elements contained in the specified other collection.default boolean
removeEach
(int[] array) default boolean
removeEach
(int[] array, int offset, int length) default boolean
removeEach
(IntCollection other) Removes from this collection element-wise occurrences of elements contained in the specified other collection.default boolean
removeIf
(IntPredicate filter) Removes all the elements of this collection that satisfy the given predicate.default boolean
retainAll
(IntCollection other) int
size()
default int[]
toArray()
Allocates a new int array with exactlysize()
items, fills it with the contents of this PrimitiveCollection, and returns it.default int[]
toArray
(int[] receiver)
-
Method Details
-
size
int size() -
isEmpty
default boolean isEmpty() -
notEmpty
default boolean notEmpty() -
add
boolean add(int item) -
remove
boolean remove(int item) -
contains
boolean contains(int item) -
addAll
-
addAll
default boolean addAll(int[] array) -
addAll
default boolean addAll(int[] array, int offset, int length) -
removeAll
Removes from this collection all occurrences of any elements contained in the specified other collection.- Parameters:
other
- a primitive collection of int items to remove fully, such as an IntList or an IntSet- Returns:
- true if this collection was modified.
-
removeAll
default boolean removeAll(int[] array) -
removeAll
default boolean removeAll(int[] array, int offset, int length) -
removeEach
Removes from this collection element-wise occurrences of elements contained in the specified other collection. Note that if a value is present more than once in this collection, only one of those occurrences will be removed for each occurrence of that value inother
. Ifother
has the same contents as this collection or has additional items, then removing each ofother
will clear this.- Parameters:
other
- a primitive collection of int items to remove one-by-one, such as an IntList or an IntSet- Returns:
- true if this collection was modified.
-
removeEach
default boolean removeEach(int[] array) -
removeEach
default boolean removeEach(int[] array, int offset, int length) -
containsAll
-
containsAll
default boolean containsAll(int[] array) -
containsAll
default boolean containsAll(int[] array, int offset, int length) -
containsAny
-
containsAny
default boolean containsAny(int[] array) -
containsAny
default boolean containsAny(int[] array, int offset, int length) -
removeIf
Removes all the elements of this collection that satisfy the given predicate. Errors or runtime exceptions thrown during iteration or by the predicate are relayed to the caller.- Parameters:
filter
- a predicate which returnstrue
for elements to be removed- Returns:
true
if any elements were removed- Throws:
UnsupportedOperationException
- if elements cannot be removed from this collection. Implementations may throw this exception if a matching element cannot be removed or if, in general, removal is not supported.
-
retainAll
-
iterator
IntIterator iterator() -
clear
void clear() -
hashCode
int hashCode() -
equals
-
toArray
default int[] toArray()Allocates a new int array with exactlysize()
items, fills it with the contents of this PrimitiveCollection, and returns it.- Returns:
- a new int array
-
toArray
default int[] toArray(int[] receiver) Fills the given array with the entire contents of this PrimitiveCollection, up tosize()
items, or if receiver is not large enough, then this allocates a new int array withsize()
items and returns that.- Parameters:
receiver
- an int array that will be filled with the items from this, if possible- Returns:
receiver
, if it was modified, or a new int array otherwise
-
forEach
Performs the given action for each element of theIntCollection
until all elements have been processed or the action throws an exception. Actions are performed in the order of iteration, if that order is specified. Exceptions thrown by the action are relayed to the caller.- Parameters:
action
- The action to be performed for each element
-
first
default int first()Attempts to get the first item in this PrimitiveCollection, where "first" is only defined meaningfully if this type is ordered. Many times, this applies to a class that is not ordered, and in those cases it can get an arbitrary item, and that item is permitted to be different for different calls to first().
This is useful for cases where you would normally be able to call something likeList.get(int)
to get an item, any item, from a collection, but whatever class you're using doesn't necessarily provide a get(), first(), peek(), or similar method.
The default implementation usesiterator()
, tries to get the first item, or throws an IllegalStateException if this is empty.- Returns:
- the first item in this PrimitiveCollection, as produced by
iterator()
- Throws:
IllegalStateException
- if this is empty
-
areEqual
Compares this IntCollection with another IntCollection by checking their identity, their types (both must implement IntCollection), and their sizes, before checking if other contains each item in this IntCollection, in any order or quantity. This is most useful for the key "set" or value collection in a primitive-backed map, since quantity doesn't matter for keys and order doesn't matter for either. Many implementations may need to reset the iterator on this IntCollection, but that isn't necessary forother
.- Parameters:
other
- another Object that should be a IntCollection- Returns:
- true if other is another IntCollection with exactly the same items, false otherwise
-