Package org.javimmutable.collections
Interface IArray<T>
- All Superinterfaces:
ICollection<IMapEntry<Integer,
,T>> Indexed<T>
,InvariantCheckable
,IStreamable<IMapEntry<Integer,
,T>> Iterable<IMapEntry<Integer,
,T>> Serializable
,SplitableIterable<IMapEntry<Integer,
T>>
- All Known Implementing Classes:
TrieArray
@Immutable
public interface IArray<T>
extends Indexed<T>, ICollection<IMapEntry<Integer,T>>, InvariantCheckable, Serializable
Immutable sparse array implementation using integers as keys. Keys are traversed in signed integer
order by Iterators so negative values are visited before positive values. Implementations
are allowed to restrict the range of allowable indexes for performance or other reasons.
Implementations should throw IndexOutOfBounds exceptions if presented with an invalid index.
Arrays are sparse meaning that they can contain elements at any valid index with no need to keep them consecutive (like a List). Memory is managed to use no more than necessary for the number of elements currently in the array.
-
Method Summary
Modifier and TypeMethodDescriptionSets the value associated with a specific index.delete
(int index) Deletes the entry for the specified index (if any).find
(int index) Return a Holder containing the value associated wth the index or an empty Holder if no value is associated with the index.findEntry
(int index) Search for an Entry within the map and return a Holder indicating if the Entry was found and, if it was found, the Entry itself.void
forEach
(IndexedProc1<T> proc) Visits every element in the array in order by index and passes the index and value to the provided lambda.<E extends Exception>
voidforEachThrows
(IndexedProc1Throws<T, E> proc) Visits every element in the array in order by index and passes the index and value to the provided lambda.get
(int index) Return the value associated with index or null if no value is associated.getMap()
Creates an unmodifiable java.util.Map reflecting the values of theIMap
backing the array.getValueOr
(int index, T defaultValue) Return the value associated with index or defaultValue if no value is associated.Add value to the container in some manner appropriate to the implementation.Add all values to the container in some manner appropriate to the implementation.Add all values to the container in some manner appropriate to the implementation.keys()
Creates an IStreamable to access all of the array's keys.Creates and returns a new Builder object for the same value type as this array.values()
Creates an IStreamable to access all of the array's values.Methods inherited from interface org.javimmutable.collections.ICollection
isEmpty, isNonEmpty, size
Methods inherited from interface org.javimmutable.collections.Indexed
prefix, reversed, size, transformed
Methods inherited from interface org.javimmutable.collections.InvariantCheckable
checkInvariants
Methods inherited from interface org.javimmutable.collections.IStreamable
getSpliteratorCharacteristics, iterator, parallelStream, spliterator, stream
Methods inherited from interface org.javimmutable.collections.SplitableIterable
forEachThrows, indexedForEach, indexedForEachThrows, reduce, reduceThrows
-
Method Details
-
get
Return the value associated with index or null if no value is associated. Note that if null is an acceptable value to the container then this method will be ambiguous and find() should be used instead. -
getValueOr
Return the value associated with index or defaultValue if no value is associated. Note that if defaultValue is an acceptable value to the container then this method will be ambiguous and find() should be used instead.- Parameters:
index
- identifies the value to retrievedefaultValue
- value to return if no entry exists for index- Returns:
- value associated with index or defaultValue if no value is associated
-
find
Return a Holder containing the value associated wth the index or an empty Holder if no value is associated with the index. -
findEntry
Search for an Entry within the map and return a Holder indicating if the Entry was found and, if it was found, the Entry itself.- Parameters:
index
- index to search for- Returns:
- empty Holder if not found, otherwise filled Holder with Entry
-
insert
Description copied from interface:ICollection
Add value to the container in some manner appropriate to the implementation.- Specified by:
insert
in interfaceICollection<T>
-
insertAll
Description copied from interface:ICollection
Add all values to the container in some manner appropriate to the implementation.- Specified by:
insertAll
in interfaceICollection<T>
-
insertAll
Description copied from interface:ICollection
Add all values to the container in some manner appropriate to the implementation.- Specified by:
insertAll
in interfaceICollection<T>
-
assign
Sets the value associated with a specific index. Index must be non-null but value can be null. If the index already has a value in the map the old value is discarded and the new value is stored in its place. Returns a newIMap
reflecting any changes. The original map is always left unchanged.- Parameters:
index
- indexvalue
- possibly null value- Returns:
- new map reflecting the change
-
delete
Deletes the entry for the specified index (if any). Returns a new map if the value was deleted or the current map if the index was not contained in the map.- Parameters:
index
- index- Returns:
- same or different map depending on whether index was removed
-
deleteAll
- Specified by:
deleteAll
in interfaceICollection<T>
- Returns:
- an equivalent collection with no values
-
getMap
Creates an unmodifiable java.util.Map reflecting the values of theIMap
backing the array.- Returns:
- Map view of this
IMap
-
keys
Creates an IStreamable to access all of the array's keys. -
values
Creates an IStreamable to access all of the array's values. -
forEach
Visits every element in the array in order by index and passes the index and value to the provided lambda. Unlike indexedForEach method the index values passed to the lambda are the actual key value in the array. -
forEachThrows
Visits every element in the array in order by index and passes the index and value to the provided lambda. Unlike indexedForEach method the index values passed to the lambda are the actual key value in the array.- Throws:
E
-
toBuilder
Creates and returns a new Builder object for the same value type as this array.- Returns:
- An empty Builder object ready for use to create a new
IArray
.
-