E - Indicates the type of the value to be stored.Cloneablepublic class PlainArray<E> extends Object implements Cloneable
int 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 PlainArray class:
null object.| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
INVALID_INDEX |
Indicates an invalid index used by index-related operations.
|
| 构造器 | 说明 |
|---|---|
PlainArray() |
A constructor used to create a
PlainArray instance with an initial capacity. |
PlainArray(int capacity) |
A constructor used to create a
PlainArray instance with a custom initial capacity. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
append(int key,
E value) |
Appends a key-value pair to
PlainArray. |
void |
clear() |
Clears the current
PlainArray object. |
PlainArray<E> |
clone() |
Obtains a clone of the current
PlainArray object. |
boolean |
contains(int key) |
Checks whether the current
PlainArray object contains the specified key. |
Optional<E> |
get(int key) |
Queries the value associated with a specified
key. |
E |
get(int key,
E 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(E value) |
Queries the index for a specified
value. |
boolean |
isEmpty() |
Checks whether the current
PlainArray 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,
E value) |
Adds a key-value pair to
PlainArray. |
Optional<E> |
remove(int key) |
Removes the key-value pair based on a specified key.
|
Optional<E> |
removeAt(int index) |
Removes the key-value pair at a specified index.
|
int |
removeBatchAt(int index,
int batchSize) |
Removes a size of batch key-value pairs from 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
PlainArray. |
String |
toString() |
Obtains the string representation of the
PlainArray object. |
E |
valueAt(int index) |
Queries the value at a specified index.
|
public static final int INVALID_INDEX
public PlainArray()
PlainArray instance with an initial capacity.public PlainArray(int capacity)
PlainArray instance with a custom initial capacity.
This constructor allows you to customize the initial capacity of the PlainArray 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 PlainArray<E> clone()
PlainArray object.
Note that values stored in the array are shallowly copied.
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 put(int key,
E value)
PlainArray.
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(int key,
E value)
PlainArray.
If the key to be added is greater than any existing key in the array, this method is more efficient than
the put(int,java.lang.Object) method.
If the specified key is not the largest one in the array, the put(int,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 void clear()
PlainArray object.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 Optional<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 int removeBatchAt(int index,
int batchSize)
index - Indicates the index of the key-value pair to remove.batchSize - Indicates the size of batch to remove.IndexOutOfBoundsException - Throws this exception if the specified index is beyond the valid range.IllegalArgumentException - Throws this exception if the specified batchSize is smaller than 1.public E get(int 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 E valueAt(int index)
index - Indicates the index for the value to query.IndexOutOfBoundsException - Throws this exception if the specified index is beyond the valid range.public int indexOfKey(int 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 int size()
PlainArray.public boolean isEmpty()
PlainArray object is empty.true if no value is contained; returns false otherwise.public boolean contains(int key)
PlainArray object contains the specified key.key - Indicates the key to check.true if the specified key exists; returns false otherwise.public String toString()
PlainArray object.
The returned string representation is JSON-compliant, and the values in JSON name/value pairs are returned by
toString() method of every elements in the PlainArray object