Cloneablepublic class PlainLongArray extends Object implements Cloneable
int as the key and
long 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 PlainLongArray class:
| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
INVALID_INDEX |
Indicates an invalid index used by index-related operations.
|
| 构造器 | 说明 |
|---|---|
PlainLongArray() |
Default constructor used to create a
PlainLongArray instance. |
PlainLongArray(int capacity) |
A constructor used to create a
PlainLongArray instance with a custom initial capacity. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
append(int key,
long value) |
Appends a key-value pair to
PlainLongArray. |
void |
clear() |
Clears all key-value pairs stored in the current
PlainLongArray object. |
PlainLongArray |
clone() |
Obtains a cloned
PlainLongArray object. |
boolean |
contains(int key) |
Checks whether the current
PlainLongArray object contains the specified key. |
Optional<Long> |
get(int key) |
Queries the value associated with a specified
key. |
long |
get(int key,
long defaultValue) |
Queries the value associated with the specified
key with the defaultValue parameter passed. |
int |
indexOfKey(int key) |
Queries the index for a specified
key. |
int |
indexOfValue(long value) |
Queries the index for a specified
value. |
boolean |
isEmpty() |
Checks whether the current
PlainLongArray object is empty. |
int |
keyAt(int index) |
Queries the key at a specified index.
|
int |
locate(int key) |
Searches for the index of a specified key.
|
void |
put(int key,
long value) |
Adds a key-value pair to
PlainLongArray. |
Optional<Long> |
remove(int key) |
Removes the key-value pair based on a specified key.
|
Optional<Long> |
removeAt(int index) |
Removes the key-value pair at a specified index.
|
int |
size() |
Obtains the total number of values stored in the current
PlainLongArray. |
String |
toString() |
Obtains the string representation of the
PlainLongArray object. |
long |
valueAt(int index) |
Queries the value at a specified index.
|
public static final int INVALID_INDEX
public PlainLongArray()
PlainLongArray instance.public PlainLongArray(int capacity)
PlainLongArray instance with a custom initial capacity.
This constructor allows you to customize the initial capacity of the PlainLongArray 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(int 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 void clear()
PlainLongArray object.public void append(int key,
long value)
PlainLongArray.
If the key to be added is greater than any existing key in the array, this method is more efficient than
the put(int,long) method.
If the specified key is not the largest one in the array, the put(int,long) 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.public PlainLongArray clone()
PlainLongArray object.public Optional<Long> 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 long get(int key,
long 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 int indexOfKey(int key)
key.key - Indicates the key to query.INVALID_INDEX if the queried key does not exist.public int indexOfValue(long value)
value.value - Indicates the value to query.INVALID_INDEX if the queried value does not exist.public int keyAt(int index)
index - Indicates the index to query.IndexOutOfBoundsException - Throws this exception if the specified index is beyond the valid range.public long valueAt(int index)
index - Indicates the index to query.IndexOutOfBoundsException - Throws this exception if the specified index is beyond the valid range.public void put(int key,
long value)
PlainLongArray.
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.public int size()
PlainLongArray.public boolean isEmpty()
PlainLongArray object is empty.true if no value is contained; returns false otherwise.public boolean contains(int key)
PlainLongArray object contains the specified key.key - Indicates the key to check.true if the specified key exists; returns false otherwise.