@Beta @GwtCompatible public final class ImmutableDoubleArray extends Object implements Serializable
double
values, with an API resembling List
.
Advantages compared to double[]
:
equals(java.lang.Object)
, hashCode()
, and toString()
behavior you expect.
get
and length
, so you don't have to
hunt through classes like Arrays
and Doubles
for them.
subArray(int, int)
view, so methods that accept this type don't need to
add overloads that accept start and end indexes.
foo.getBarDoubles().stream()...
.
asList()
(though at the cost of
allocating garbage).
Disadvantages compared to double[]
:
double[]
(though the most common
utilities do have replacements here).
com.google.common
/ Guava.
Advantages compared to ImmutableList
<Double>
:
DoubleStream
features (like DoubleStream.sum()
) using stream()
instead of the awkward stream().mapToDouble(v -> v)
.
Disadvantages compared to ImmutableList<Double>
:
Iterable
, Collection
, or
List
(though the most common utilities do have replacements here, and there is a
lazy asList()
view).
Modifier and Type | Class and Description |
---|---|
static class |
ImmutableDoubleArray.Builder
A builder for
ImmutableDoubleArray instances; obtained using builder(int) . |
Modifier and Type | Method and Description |
---|---|
List<Double> |
asList()
Returns an immutable view of this array's values as a
List ; note that double values are boxed into Double instances on demand, which can be very expensive. |
static ImmutableDoubleArray.Builder |
builder()
Returns a new, empty builder for
ImmutableDoubleArray instances, with a default initial
capacity. |
static ImmutableDoubleArray.Builder |
builder(int initialCapacity)
Returns a new, empty builder for
ImmutableDoubleArray instances, sized to hold up to
initialCapacity values without resizing. |
boolean |
contains(double target)
Returns
true if target is present at any index in this array. |
static ImmutableDoubleArray |
copyOf(Collection<Double> values)
Returns an immutable array containing the given values, in order.
|
static ImmutableDoubleArray |
copyOf(double[] values)
Returns an immutable array containing the given values, in order.
|
static ImmutableDoubleArray |
copyOf(DoubleStream stream)
Returns an immutable array containing all the values from
stream , in order. |
static ImmutableDoubleArray |
copyOf(Iterable<Double> values)
Returns an immutable array containing the given values, in order.
|
boolean |
equals(Object object)
Returns
true if object is an ImmutableDoubleArray containing the same
values as this one, in the same order. |
void |
forEach(DoubleConsumer consumer)
Invokes
consumer for each value contained in this array, in order. |
double |
get(int index)
Returns the
double value present at the given index. |
int |
hashCode()
Returns an unspecified hash code for the contents of this immutable array.
|
int |
indexOf(double target)
|
boolean |
isEmpty()
Returns
true if there are no values in this array (length() is zero). |
int |
lastIndexOf(double target)
|
int |
length()
Returns the number of values in this array.
|
static ImmutableDoubleArray |
of()
Returns the empty array.
|
static ImmutableDoubleArray |
of(double e0)
Returns an immutable array containing a single value.
|
static ImmutableDoubleArray |
of(double first,
double... rest)
Returns an immutable array containing the given values, in order.
|
static ImmutableDoubleArray |
of(double e0,
double e1)
Returns an immutable array containing the given values, in order.
|
static ImmutableDoubleArray |
of(double e0,
double e1,
double e2)
Returns an immutable array containing the given values, in order.
|
static ImmutableDoubleArray |
of(double e0,
double e1,
double e2,
double e3)
Returns an immutable array containing the given values, in order.
|
static ImmutableDoubleArray |
of(double e0,
double e1,
double e2,
double e3,
double e4)
Returns an immutable array containing the given values, in order.
|
static ImmutableDoubleArray |
of(double e0,
double e1,
double e2,
double e3,
double e4,
double e5)
Returns an immutable array containing the given values, in order.
|
DoubleStream |
stream()
Returns a stream over the values in this array, in order.
|
ImmutableDoubleArray |
subArray(int startIndex,
int endIndex)
Returns a new immutable array containing the values in the specified range.
|
double[] |
toArray()
Returns a new, mutable copy of this array's values, as a primitive
double[] . |
String |
toString()
Returns a string representation of this array in the same form as
Arrays.toString(double[]) , for example "[1, 2, 3]" . |
ImmutableDoubleArray |
trimmed()
Returns an immutable array containing the same values as
this array. |
public static ImmutableDoubleArray of()
public static ImmutableDoubleArray of(double e0)
public static ImmutableDoubleArray of(double e0, double e1)
public static ImmutableDoubleArray of(double e0, double e1, double e2)
public static ImmutableDoubleArray of(double e0, double e1, double e2, double e3)
public static ImmutableDoubleArray of(double e0, double e1, double e2, double e3, double e4)
public static ImmutableDoubleArray of(double e0, double e1, double e2, double e3, double e4, double e5)
public static ImmutableDoubleArray of(double first, double... rest)
public static ImmutableDoubleArray copyOf(double[] values)
public static ImmutableDoubleArray copyOf(Collection<Double> values)
public static ImmutableDoubleArray copyOf(Iterable<Double> values)
Performance note: this method delegates to copyOf(Collection)
if values
is a Collection
. Otherwise it creates a builder(int)
and uses ImmutableDoubleArray.Builder.addAll(Iterable)
, with all the performance implications associated with that.
public static ImmutableDoubleArray copyOf(DoubleStream stream)
stream
, in order.public static ImmutableDoubleArray.Builder builder(int initialCapacity)
ImmutableDoubleArray
instances, sized to hold up to
initialCapacity
values without resizing. The returned builder is not thread-safe.
Performance note: When feasible, initialCapacity
should 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, the ImmutableDoubleArray
that is built will very likely occupy more memory than strictly
necessary; to trim memory usage, build using builder.build().trimmed()
.
public static ImmutableDoubleArray.Builder builder()
ImmutableDoubleArray
instances, with a default initial
capacity. The returned builder is not thread-safe.
Performance note: The ImmutableDoubleArray
that is built will very likely
occupy more memory than necessary; to trim memory usage, build using builder.build().trimmed()
.
public int length()
public boolean isEmpty()
true
if there are no values in this array (length()
is zero).public double get(int index)
double
value present at the given index.IndexOutOfBoundsException
- if index
is negative, or greater than or equal to
length()
public int indexOf(double target)
get(int)
returns target
, or -1
if no
such index exists. Values are compared as if by Double.equals(java.lang.Object)
. Equivalent to asList().indexOf(target)
.public int lastIndexOf(double target)
get(int)
returns target
, or -1
if no
such index exists. Values are compared as if by Double.equals(java.lang.Object)
. Equivalent to asList().lastIndexOf(target)
.public boolean contains(double target)
true
if target
is present at any index in this array. Values are
compared as if by Double.equals(java.lang.Object)
. Equivalent to asList().contains(target)
.public void forEach(DoubleConsumer consumer)
consumer
for each value contained in this array, in order.public DoubleStream stream()
public double[] toArray()
double[]
.public ImmutableDoubleArray subArray(int startIndex, int endIndex)
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()
.
public List<Double> asList()
List
; note that double
values are boxed into Double
instances 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 to ImmutableList.copyOf
and use that list instead.public boolean equals(@Nullable Object object)
true
if object
is an ImmutableDoubleArray
containing the same
values as this one, in the same order. Values are compared as if by Double.equals(java.lang.Object)
.equals
in class Object
object
- the reference object with which to compare.true
if this object is the same as the obj
argument; false
otherwise.Object.hashCode()
,
HashMap
public int hashCode()
hashCode
in class Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
public String toString()
Arrays.toString(double[])
, for example "[1, 2, 3]"
.public ImmutableDoubleArray trimmed()
this
array. This is logically
a no-op, and in some circumstances this
itself is returned. However, if this instance
is a subArray(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.Copyright © 2010–2017. All rights reserved.