Class ImmutableLongArray
- java.lang.Object
- 
- com.google.common.primitives.ImmutableLongArray
 
- 
- All Implemented Interfaces:
- java.io.Serializable
 
 @GwtCompatible @Immutable public final class ImmutableLongArray extends java.lang.Object implements java.io.Serializable An immutable array oflongvalues, with an API resemblingList.Advantages compared to long[]:- All the many well-known advantages of immutability (read Effective Java, third edition, Item 17).
- Has the value-based (not identity-based) equals(java.lang.Object),hashCode(), andtoString()behavior you expect.
- Offers useful operations beyond just getandlength, so you don't have to hunt through classes likeArraysandLongsfor them.
- Supports a copy-free subArray(int, int)view, so methods that accept this type don't need to add overloads that accept start and end indexes.
- Access to all collection-based utilities via asList()(though at the cost of allocating garbage).
 Disadvantages compared to long[]:- Memory footprint has a fixed overhead (about 24 bytes per instance).
- Some construction use cases force the data to be copied (though several construction APIs are offered that don't).
- Can't be passed directly to methods that expect long[](though the most common utilities do have replacements here).
- Dependency on com.google.common/ Guava.
 Advantages compared to ImmutableList<Long>:- Improved memory compactness and locality.
- Can be queried without allocating garbage.
 Disadvantages compared to ImmutableList<Long>:- Can't be passed directly to methods that expect Iterable,Collection, orList(though the most common utilities do have replacements here, and there is a lazyasList()view).
 - Since:
- 22.0
- See Also:
- Serialized Form
 
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static classImmutableLongArray.BuilderA builder forImmutableLongArrayinstances; obtained usingbuilder(int).
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.List<java.lang.Long>asList()Returns an immutable view of this array's values as aList; note thatlongvalues are boxed intoLonginstances on demand, which can be very expensive.static ImmutableLongArray.Builderbuilder()Returns a new, empty builder forImmutableLongArrayinstances, with a default initial capacity.static ImmutableLongArray.Builderbuilder(int initialCapacity)Returns a new, empty builder forImmutableLongArrayinstances, sized to hold up toinitialCapacityvalues without resizing.booleancontains(long target)Returnstrueiftargetis present at any index in this array.static ImmutableLongArraycopyOf(long[] values)Returns an immutable array containing the given values, in order.static ImmutableLongArraycopyOf(java.lang.Iterable<java.lang.Long> values)Returns an immutable array containing the given values, in order.static ImmutableLongArraycopyOf(java.util.Collection<java.lang.Long> values)Returns an immutable array containing the given values, in order.booleanequals(java.lang.Object object)Returnstrueifobjectis anImmutableLongArraycontaining the same values as this one, in the same order.longget(int index)Returns thelongvalue present at the given index.inthashCode()Returns an unspecified hash code for the contents of this immutable array.intindexOf(long target)booleanisEmpty()Returnstrueif there are no values in this array (length()is zero).intlastIndexOf(long target)intlength()Returns the number of values in this array.static ImmutableLongArrayof()Returns the empty array.static ImmutableLongArrayof(long e0)Returns an immutable array containing a single value.static ImmutableLongArrayof(long e0, long e1)Returns an immutable array containing the given values, in order.static ImmutableLongArrayof(long first, long... rest)Returns an immutable array containing the given values, in order.static ImmutableLongArrayof(long e0, long e1, long e2)Returns an immutable array containing the given values, in order.static ImmutableLongArrayof(long e0, long e1, long e2, long e3)Returns an immutable array containing the given values, in order.static ImmutableLongArrayof(long e0, long e1, long e2, long e3, long e4)Returns an immutable array containing the given values, in order.static ImmutableLongArrayof(long e0, long e1, long e2, long e3, long e4, long e5)Returns an immutable array containing the given values, in order.ImmutableLongArraysubArray(int startIndex, int endIndex)Returns a new immutable array containing the values in the specified range.long[]toArray()Returns a new, mutable copy of this array's values, as a primitivelong[].java.lang.StringtoString()Returns a string representation of this array in the same form asArrays.toString(long[]), for example"[1, 2, 3]".ImmutableLongArraytrimmed()Returns an immutable array containing the same values asthisarray.
 
- 
- 
- 
Method Detail- 
ofpublic static ImmutableLongArray of() Returns the empty array.
 - 
ofpublic static ImmutableLongArray of(long e0) Returns an immutable array containing a single value.
 - 
ofpublic static ImmutableLongArray of(long e0, long e1) Returns an immutable array containing the given values, in order.
 - 
ofpublic static ImmutableLongArray of(long e0, long e1, long e2) Returns an immutable array containing the given values, in order.
 - 
ofpublic static ImmutableLongArray of(long e0, long e1, long e2, long e3) Returns an immutable array containing the given values, in order.
 - 
ofpublic static ImmutableLongArray of(long e0, long e1, long e2, long e3, long e4) Returns an immutable array containing the given values, in order.
 - 
ofpublic static ImmutableLongArray of(long e0, long e1, long e2, long e3, long e4, long e5) Returns an immutable array containing the given values, in order.
 - 
ofpublic static ImmutableLongArray of(long first, long... rest) Returns an immutable array containing the given values, in order.The array restmust not be longer thanInteger.MAX_VALUE - 1.
 - 
copyOfpublic static ImmutableLongArray copyOf(long[] values) Returns an immutable array containing the given values, in order.
 - 
copyOfpublic static ImmutableLongArray copyOf(java.util.Collection<java.lang.Long> values) Returns an immutable array containing the given values, in order.
 - 
copyOfpublic static ImmutableLongArray copyOf(java.lang.Iterable<java.lang.Long> values) Returns an immutable array containing the given values, in order.Performance note: this method delegates to copyOf(Collection)ifvaluesis aCollection. Otherwise it creates abuilder(int)and usesImmutableLongArray.Builder.addAll(Iterable), with all the performance implications associated with that.
 - 
builderpublic static ImmutableLongArray.Builder builder(int initialCapacity) Returns a new, empty builder forImmutableLongArrayinstances, sized to hold up toinitialCapacityvalues without resizing. The returned builder is not thread-safe.Performance note: When feasible, initialCapacityshould be the exact number of values that will be added, if that knowledge is readily available. It is better to guess a value slightly too high than slightly too low. If the value is not exact, theImmutableLongArraythat is built will very likely occupy more memory than strictly necessary; to trim memory usage, build usingbuilder.build().trimmed().
 - 
builderpublic static ImmutableLongArray.Builder builder() Returns a new, empty builder forImmutableLongArrayinstances, with a default initial capacity. The returned builder is not thread-safe.Performance note: The ImmutableLongArraythat is built will very likely occupy more memory than necessary; to trim memory usage, build usingbuilder.build().trimmed().
 - 
lengthpublic int length() Returns the number of values in this array.
 - 
isEmptypublic boolean isEmpty() Returnstrueif there are no values in this array (length()is zero).
 - 
getpublic long get(int index) Returns thelongvalue present at the given index.- Throws:
- java.lang.IndexOutOfBoundsException- if- indexis negative, or greater than or equal to- length()
 
 - 
indexOfpublic int indexOf(long target) Returns the smallest index for whichget(int)returnstarget, or-1if no such index exists. Equivalent toasList().indexOf(target).
 - 
lastIndexOfpublic int lastIndexOf(long target) Returns the largest index for whichget(int)returnstarget, or-1if no such index exists. Equivalent toasList().lastIndexOf(target).
 - 
containspublic boolean contains(long target) Returnstrueiftargetis present at any index in this array. Equivalent toasList().contains(target).
 - 
toArraypublic long[] toArray() Returns a new, mutable copy of this array's values, as a primitivelong[].
 - 
subArraypublic ImmutableLongArray subArray(int startIndex, int endIndex) Returns a new immutable array containing the values in the specified range.Performance note: The returned array has the same full memory footprint as this one does (no actual copying is performed). To reduce memory usage, use subArray(start, end).trimmed().
 - 
asListpublic java.util.List<java.lang.Long> asList() Returns an immutable view of this array's values as aList; note thatlongvalues are boxed intoLonginstances on demand, which can be very expensive. The returned list should be used once and discarded. For any usages beyond that, pass the returned list toImmutableList.copyOfand use that list instead.
 - 
equalspublic boolean equals(@CheckForNull java.lang.Object object) Returnstrueifobjectis anImmutableLongArraycontaining the same values as this one, in the same order.- Overrides:
- equalsin class- java.lang.Object
 
 - 
hashCodepublic int hashCode() Returns an unspecified hash code for the contents of this immutable array.- Overrides:
- hashCodein class- java.lang.Object
 
 - 
toStringpublic java.lang.String toString() Returns a string representation of this array in the same form asArrays.toString(long[]), for example"[1, 2, 3]".- Overrides:
- toStringin class- java.lang.Object
 
 - 
trimmedpublic ImmutableLongArray trimmed() Returns an immutable array containing the same values asthisarray. This is logically a no-op, and in some circumstancesthisitself is returned. However, if this instance is asubArray(int, int)view of a larger array, this method will copy only the appropriate range of values, resulting in an equivalent array with a smaller memory footprint.
 
- 
 
-