E - Indicates the type of the value to be stored.Cloneablepublic class LongPlainArray<E> extends Object implements Cloneable
long as the key and
any object as the value.
Values in this class are directly stored in arrays, without being wrapped into other types. This simplifies the data
storage structure and saves memory.
All key-value pairs in this class are sorted in ascending order when they are stored, and the binary search algorithm
is used to efficiently search for a specified key.
In such data storage structures, keys and values can be iterated over based on indexes.
This class implements the Cloneable interface and supports shallow copy of stored values.
Pay attention to the following when using the LongPlainArray class:
null object.| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
INVALID_INDEX |
Indicates an invalid index used by index-related operations.
|
| 构造器 | 说明 |
|---|---|
LongPlainArray() |
A constructor used to create a
LongPlainArray instance with an initial capacity. |
LongPlainArray(int capacity) |
A constructor used to create a
LongPlainArray instance with a custom initial capacity. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
append(long key,
E value) |
Appends a key-value pair to
LongPlainArray. |
void |
clear() |
Clears the current
LongPlainArray object. |
LongPlainArray<E> |
clone() |
Obtains a clone of the current
LongPlainArray object. |
boolean |
contains(long key) |
Checks whether the current
LongPlainArray object contains the specified key. |
Optional<E> |
get(long key) |
Queries the value associated with a specified
key. |
E |
get(long key,
E defaultValue) |
Queries the value associated with the specified
key with the defaultValue parameter passed. |
int |
indexOfKey(long key) |
Queries the index for a specified
key. |
int |
indexOfValue(E value) |
Queries the index for a specified
value. |
boolean |
isEmpty() |
Checks whether the current
LongPlainArray object is empty. |
long |
keyAt(int index) |
Queries the key of a specified index.
|
int |
locate(long key) |
Searches for the index of a specified key.
|
void |
put(long key,
E value) |
Adds a key-value pair to
LongPlainArray. |
Optional<E> |
remove(long key) |
Removes the key-value pair based on a specified key.
|
E |
removeAt(int index) |
Removes the key-value pair of a specified index.
|
void |
setValueAt(int index,
E value) |
Update value on specified index.
|
int |
size() |
Obtains the total number of values stored in the current
LongPlainArray. |
String |
toString() |
Obtains the string format of the
LongPlainArray object. |
E |
valueAt(int index) |
Queries the value of a specified index.
|
public static final int INVALID_INDEX
public LongPlainArray()
LongPlainArray instance with an initial capacity.public LongPlainArray(int capacity)
LongPlainArray instance with a custom initial capacity.
This constructor allows you to customize the initial capacity of the LongPlainArray object.
capacity - Indicates the initial capacity to customize for the object.IllegalArgumentException - Throws this exception if the specified capacity is less than or equal to
0.public int locate(long key)
The binary search algorithm is used for searching. If no result is found, a negative value is returned. However, you can invert this negative value to obtain the index where the key should be stored in the array.
key - Indicates the key to search for.public int indexOfKey(long key)
key.key - Indicates the key to query.INVALID_INDEX if the queried key does not exist.public int indexOfValue(E value)
value.value - Indicates the value to query.INVALID_INDEX if the queried value does not exist.NullPointerException - Throws this exception if the queried value is null.public long keyAt(int index)
index - Indicates the index to query.IndexOutOfBoundsException - Throws this exception if the specified index is beyond the valid range.public E valueAt(int index)
index - Indicates the index to query.IndexOutOfBoundsException - Throws this exception if the specified index is beyond the valid range.public E get(long key, E defaultValue)
key with the defaultValue parameter passed.key - Indicates the key of the value to query.defaultValue - Indicates the default value.key; returns defaultValue if the key does not exist.public void put(long key,
E value)
LongPlainArray.
If the specified key already exists, its value will be replaced by value.
key - Indicates the key to be added.value - Indicates the value associated with the key.NullPointerException - Throws this exception if value is null.public void setValueAt(int index,
E value)
index - Indicates the index to insert.value - Indicates the value associated with the key.IndexOutOfBoundsException - Throws this exception if the specified index is beyond the valid range.NullPointerException - Throws this exception if the specified value is null.public void append(long key,
E value)
LongPlainArray.
If the key to be added is greater than any existing key in the array, this method is more efficient than
the put(long,java.lang.Object) method.
If the specified key is not the largest one in the array, the put(long,java.lang.Object) method is
automatically called to add the key-value pair.
key - Indicates the key to be added.value - Indicates the value associated with the key.NullPointerException - Throws this exception if value is null.public E removeAt(int index)
index - Indicates the index of the key-value pair to remove.Optional object with the removed value encapsulated.IndexOutOfBoundsException - Throws this exception if the specified index is beyond the valid range.public LongPlainArray<E> clone()
LongPlainArray object.
Note that values stored in the array are shallowly copied.
public boolean contains(long key)
LongPlainArray object contains the specified key.key - Indicates the key to check.true if the specified key exists; returns false otherwise.public void clear()
LongPlainArray object.public boolean isEmpty()
LongPlainArray object is empty.true if no value is contained; returns false otherwise.public int size()
LongPlainArray.public String toString()
LongPlainArray object.
The returned string format is JSON-compliant, and the values in JSON name/value pairs are returned by
toString() method of every elements in the LongPlainArray object.