public class

LongSparseArray<E>

extends java.lang.Object

implements java.lang.Cloneable

 java.lang.Object

↳androidx.collection.LongSparseArray<E>

Gradle dependencies

compile group: 'androidx.collection', name: 'collection', version: '1.2.0'

  • groupId: androidx.collection
  • artifactId: collection
  • version: 1.2.0

Artifact androidx.collection:collection:1.2.0 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.collection:collection com.android.support:collections

Androidx class mapping:

androidx.collection.LongSparseArray android.support.v4.util.LongSparseArray

Overview

SparseArray mapping longs to Objects. Unlike a normal array of Objects, there can be gaps in the indices. It is intended to be more memory efficient than using a HashMap to map Longs to Objects, both because it avoids auto-boxing keys and its data structure doesn't rely on an extra entry object for each mapping.

Note that this container keeps its mappings in an array data structure, using a binary search to find keys. The implementation is not intended to be appropriate for data structures that may contain large numbers of items. It is generally slower than a traditional HashMap, since lookups require a binary search and adds and removes require inserting and deleting entries in the array. For containers holding up to hundreds of items, the performance difference is not significant, less than 50%.

To help with performance, the container includes an optimization when removing keys: instead of compacting its array immediately, it leaves the removed entry marked as deleted. The entry can then be re-used for the same key, or compacted later in a single garbage collection step of all removed entries. This garbage collection will need to be performed at any time the array needs to be grown or the the map size or entry values are retrieved.

It is possible to iterate over the items in this container using LongSparseArray.keyAt(int) and LongSparseArray.valueAt(int). Iterating over the keys using keyAt(int) with ascending values of the index will return the keys in ascending order, or the values corresponding to the keys in ascending order in the case of valueAt(int).

Summary

Constructors
publicLongSparseArray()

Creates a new LongSparseArray containing no mappings.

publicLongSparseArray(int initialCapacity)

Creates a new LongSparseArray containing no mappings that will not require any additional memory allocation to store the specified number of mappings.

Methods
public voidappend(long key, java.lang.Object value)

Puts a key/value pair into the array, optimizing for the case where the key is greater than all existing keys in the array.

public voidclear()

Removes all key-value mappings from this LongSparseArray.

public LongSparseArray<java.lang.Object>clone()

public booleancontainsKey(long key)

Returns true if the specified key is mapped.

public booleancontainsValue(java.lang.Object value)

Returns true if the specified value is mapped from any key.

public voiddelete(long key)

public java.lang.Objectget(long key)

Gets the Object mapped from the specified key, or null if no such mapping has been made.

public java.lang.Objectget(long key, java.lang.Object valueIfKeyNotFound)

Gets the Object mapped from the specified key, or the specified Object if no such mapping has been made.

public intindexOfKey(long key)

Returns the index for which LongSparseArray.keyAt(int) would return the specified key, or a negative number if the specified key is not mapped.

public intindexOfValue(java.lang.Object value)

Returns an index for which LongSparseArray.valueAt(int) would return the specified key, or a negative number if no keys map to the specified value.

public booleanisEmpty()

Return true if size() is 0.

public longkeyAt(int index)

Given an index in the range 0...size()-1, returns the key from the indexth key-value mapping that this LongSparseArray stores.

public voidput(long key, java.lang.Object value)

Adds a mapping from the specified key to the specified value, replacing the previous mapping from the specified key if there was one.

public voidputAll(LongSparseArray<java.lang.Object> other)

Copies all of the mappings from the other to this map.

public java.lang.ObjectputIfAbsent(long key, java.lang.Object value)

Add a new value to the array map only if the key does not already have a value or it is mapped to null.

public voidremove(long key)

Removes the mapping from the specified key, if there was any.

public booleanremove(long key, java.lang.Object value)

Remove an existing key from the array map only if it is currently mapped to value.

public voidremoveAt(int index)

Removes the mapping at the specified index.

public java.lang.Objectreplace(long key, java.lang.Object value)

Replace the mapping for key only if it is already mapped to a value.

public booleanreplace(long key, java.lang.Object oldValue, java.lang.Object newValue)

Replace the mapping for key only if it is already mapped to a value.

public voidsetValueAt(int index, java.lang.Object value)

Given an index in the range 0...size()-1, sets a new value for the indexth key-value mapping that this LongSparseArray stores.

public intsize()

Returns the number of key-value mappings that this LongSparseArray currently stores.

public java.lang.StringtoString()

public java.lang.ObjectvalueAt(int index)

Given an index in the range 0...size()-1, returns the value from the indexth key-value mapping that this LongSparseArray stores.

from java.lang.Objectequals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

Constructors

public LongSparseArray()

Creates a new LongSparseArray containing no mappings.

public LongSparseArray(int initialCapacity)

Creates a new LongSparseArray containing no mappings that will not require any additional memory allocation to store the specified number of mappings. If you supply an initial capacity of 0, the sparse array will be initialized with a light-weight representation not requiring any additional array allocations.

Methods

public LongSparseArray<java.lang.Object> clone()

public java.lang.Object get(long key)

Gets the Object mapped from the specified key, or null if no such mapping has been made.

public java.lang.Object get(long key, java.lang.Object valueIfKeyNotFound)

Gets the Object mapped from the specified key, or the specified Object if no such mapping has been made.

public void delete(long key)

Deprecated: Alias for LongSparseArray.remove(long).

public void remove(long key)

Removes the mapping from the specified key, if there was any.

public boolean remove(long key, java.lang.Object value)

Remove an existing key from the array map only if it is currently mapped to value.

Parameters:

key: The key of the mapping to remove.
value: The value expected to be mapped to the key.

Returns:

Returns true if the mapping was removed.

public void removeAt(int index)

Removes the mapping at the specified index.

public java.lang.Object replace(long key, java.lang.Object value)

Replace the mapping for key only if it is already mapped to a value.

Parameters:

key: The key of the mapping to replace.
value: The value to store for the given key.

Returns:

Returns the previous mapped value or null.

public boolean replace(long key, java.lang.Object oldValue, java.lang.Object newValue)

Replace the mapping for key only if it is already mapped to a value.

Parameters:

key: The key of the mapping to replace.
oldValue: The value expected to be mapped to the key.
newValue: The value to store for the given key.

Returns:

Returns true if the value was replaced.

public void put(long key, java.lang.Object value)

Adds a mapping from the specified key to the specified value, replacing the previous mapping from the specified key if there was one.

public void putAll(LongSparseArray<java.lang.Object> other)

Copies all of the mappings from the other to this map. The effect of this call is equivalent to that of calling LongSparseArray.put(long, E) on this map once for each mapping from key to value in other.

public java.lang.Object putIfAbsent(long key, java.lang.Object value)

Add a new value to the array map only if the key does not already have a value or it is mapped to null.

Parameters:

key: The key under which to store the value.
value: The value to store for the given key.

Returns:

Returns the value that was stored for the given key, or null if there was no such key.

public int size()

Returns the number of key-value mappings that this LongSparseArray currently stores.

public boolean isEmpty()

Return true if size() is 0.

Returns:

true if size() is 0.

public long keyAt(int index)

Given an index in the range 0...size()-1, returns the key from the indexth key-value mapping that this LongSparseArray stores.

The keys corresponding to indices in ascending order are guaranteed to be in ascending order, e.g., keyAt(0) will return the smallest key and keyAt(size()-1) will return the largest key.

public java.lang.Object valueAt(int index)

Given an index in the range 0...size()-1, returns the value from the indexth key-value mapping that this LongSparseArray stores.

The values corresponding to indices in ascending order are guaranteed to be associated with keys in ascending order, e.g., valueAt(0) will return the value associated with the smallest key and valueAt(size()-1) will return the value associated with the largest key.

public void setValueAt(int index, java.lang.Object value)

Given an index in the range 0...size()-1, sets a new value for the indexth key-value mapping that this LongSparseArray stores.

public int indexOfKey(long key)

Returns the index for which LongSparseArray.keyAt(int) would return the specified key, or a negative number if the specified key is not mapped.

public int indexOfValue(java.lang.Object value)

Returns an index for which LongSparseArray.valueAt(int) would return the specified key, or a negative number if no keys map to the specified value. Beware that this is a linear search, unlike lookups by key, and that multiple keys can map to the same value and this will find only one of them.

public boolean containsKey(long key)

Returns true if the specified key is mapped.

public boolean containsValue(java.lang.Object value)

Returns true if the specified value is mapped from any key.

public void clear()

Removes all key-value mappings from this LongSparseArray.

public void append(long key, java.lang.Object value)

Puts a key/value pair into the array, optimizing for the case where the key is greater than all existing keys in the array.

public java.lang.String toString()

This implementation composes a string by iterating over its mappings. If this map contains itself as a value, the string "(this Map)" will appear in its place.

Source

/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.collection;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * SparseArray mapping longs to Objects.  Unlike a normal array of Objects,
 * there can be gaps in the indices.  It is intended to be more memory efficient
 * than using a HashMap to map Longs to Objects, both because it avoids
 * auto-boxing keys and its data structure doesn't rely on an extra entry object
 * for each mapping.
 *
 * <p>Note that this container keeps its mappings in an array data structure,
 * using a binary search to find keys.  The implementation is not intended to be appropriate for
 * data structures
 * that may contain large numbers of items.  It is generally slower than a traditional
 * HashMap, since lookups require a binary search and adds and removes require inserting
 * and deleting entries in the array.  For containers holding up to hundreds of items,
 * the performance difference is not significant, less than 50%.</p>
 *
 * <p>To help with performance, the container includes an optimization when removing
 * keys: instead of compacting its array immediately, it leaves the removed entry marked
 * as deleted.  The entry can then be re-used for the same key, or compacted later in
 * a single garbage collection step of all removed entries.  This garbage collection will
 * need to be performed at any time the array needs to be grown or the the map size or
 * entry values are retrieved.</p>
 *
 * <p>It is possible to iterate over the items in this container using
 * {@link #keyAt(int)} and {@link #valueAt(int)}. Iterating over the keys using
 * <code>keyAt(int)</code> with ascending values of the index will return the
 * keys in ascending order, or the values corresponding to the keys in ascending
 * order in the case of <code>valueAt(int)</code>.</p>
 */
public class LongSparseArray<E> implements Cloneable {
    private static final Object DELETED = new Object();
    private boolean mGarbage = false;

    private long[] mKeys;
    private Object[] mValues;
    private int mSize;

    /**
     * Creates a new LongSparseArray containing no mappings.
     */
    public LongSparseArray() {
        this(10);
    }

    /**
     * Creates a new LongSparseArray containing no mappings that will not
     * require any additional memory allocation to store the specified
     * number of mappings.  If you supply an initial capacity of 0, the
     * sparse array will be initialized with a light-weight representation
     * not requiring any additional array allocations.
     */
    public LongSparseArray(int initialCapacity) {
        if (initialCapacity == 0) {
            mKeys = ContainerHelpers.EMPTY_LONGS;
            mValues = ContainerHelpers.EMPTY_OBJECTS;
        } else {
            initialCapacity = ContainerHelpers.idealLongArraySize(initialCapacity);
            mKeys = new long[initialCapacity];
            mValues = new Object[initialCapacity];
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public LongSparseArray<E> clone() {
        LongSparseArray<E> clone;
        try {
            clone = (LongSparseArray<E>) super.clone();
            clone.mKeys = mKeys.clone();
            clone.mValues = mValues.clone();
        } catch (CloneNotSupportedException e) {
            throw new AssertionError(e); // Cannot happen as we implement Cloneable.
        }
        return clone;
    }

    /**
     * Gets the Object mapped from the specified key, or <code>null</code>
     * if no such mapping has been made.
     */
    @Nullable
    @SuppressWarnings("NullAway") // See inline comment.
    public E get(long key) {
        // We pass null as the default to a function which isn't explicitly annotated as nullable.
        // Not marking the function as nullable should allow us to eventually propagate the generic
        // parameter's nullability to the caller. If we were to mark it as nullable now, we would
        // also be forced to mark the return type of that method as nullable which harms the case
        // where you are passing in a non-null default value.
        return get(key, null);
    }

    /**
     * Gets the Object mapped from the specified key, or the specified Object
     * if no such mapping has been made.
     */
    @SuppressWarnings("unchecked")
    public E get(long key, E valueIfKeyNotFound) {
        int i = ContainerHelpers.binarySearch(mKeys, mSize, key);

        if (i < 0 || mValues[i] == DELETED) {
            return valueIfKeyNotFound;
        } else {
            return (E) mValues[i];
        }
    }

    /**
     * @deprecated Alias for {@link #remove(long)}.
     */
    @Deprecated
    public void delete(long key) {
        remove(key);
    }

    /**
     * Removes the mapping from the specified key, if there was any.
     */
    public void remove(long key) {
        int i = ContainerHelpers.binarySearch(mKeys, mSize, key);

        if (i >= 0) {
            if (mValues[i] != DELETED) {
                mValues[i] = DELETED;
                mGarbage = true;
            }
        }
    }

    /**
     * Remove an existing key from the array map only if it is currently mapped to {@code value}.
     * @param key The key of the mapping to remove.
     * @param value The value expected to be mapped to the key.
     * @return Returns true if the mapping was removed.
     */
    public boolean remove(long key, Object value) {
        int index = indexOfKey(key);
        if (index >= 0) {
            E mapValue = valueAt(index);
            if (value == mapValue || (value != null && value.equals(mapValue))) {
                removeAt(index);
                return true;
            }
        }
        return false;
    }

    /**
     * Removes the mapping at the specified index.
     */
    public void removeAt(int index) {
        if (mValues[index] != DELETED) {
            mValues[index] = DELETED;
            mGarbage = true;
        }
    }

    /**
     * Replace the mapping for {@code key} only if it is already mapped to a value.
     * @param key The key of the mapping to replace.
     * @param value The value to store for the given key.
     * @return Returns the previous mapped value or null.
     */
    @Nullable
    @SuppressWarnings("unchecked")
    public E replace(long key, E value) {
        int index = indexOfKey(key);
        if (index >= 0) {
            E oldValue = (E) mValues[index];
            mValues[index] = value;
            return oldValue;
        }
        return null;
    }

    /**
     * Replace the mapping for {@code key} only if it is already mapped to a value.
     *
     * @param key The key of the mapping to replace.
     * @param oldValue The value expected to be mapped to the key.
     * @param newValue The value to store for the given key.
     * @return Returns true if the value was replaced.
     */
    public boolean replace(long key, E oldValue, E newValue) {
        int index = indexOfKey(key);
        if (index >= 0) {
            Object mapValue = mValues[index];
            if (mapValue == oldValue || (oldValue != null && oldValue.equals(mapValue))) {
                mValues[index] = newValue;
                return true;
            }
        }
        return false;
    }

    private void gc() {
        // Log.e("SparseArray", "gc start with " + mSize);

        int n = mSize;
        int o = 0;
        long[] keys = mKeys;
        Object[] values = mValues;

        for (int i = 0; i < n; i++) {
            Object val = values[i];

            if (val != DELETED) {
                if (i != o) {
                    keys[o] = keys[i];
                    values[o] = val;
                    values[i] = null;
                }

                o++;
            }
        }

        mGarbage = false;
        mSize = o;

        // Log.e("SparseArray", "gc end with " + mSize);
    }

    /**
     * Adds a mapping from the specified key to the specified value,
     * replacing the previous mapping from the specified key if there
     * was one.
     */
    public void put(long key, E value) {
        int i = ContainerHelpers.binarySearch(mKeys, mSize, key);

        if (i >= 0) {
            mValues[i] = value;
        } else {
            i = ~i;

            if (i < mSize && mValues[i] == DELETED) {
                mKeys[i] = key;
                mValues[i] = value;
                return;
            }

            if (mGarbage && mSize >= mKeys.length) {
                gc();

                // Search again because indices may have changed.
                i = ~ContainerHelpers.binarySearch(mKeys, mSize, key);
            }

            if (mSize >= mKeys.length) {
                int n = ContainerHelpers.idealLongArraySize(mSize + 1);

                long[] nkeys = new long[n];
                Object[] nvalues = new Object[n];

                // Log.e("SparseArray", "grow " + mKeys.length + " to " + n);
                System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length);
                System.arraycopy(mValues, 0, nvalues, 0, mValues.length);

                mKeys = nkeys;
                mValues = nvalues;
            }

            if (mSize - i != 0) {
                // Log.e("SparseArray", "move " + (mSize - i));
                System.arraycopy(mKeys, i, mKeys, i + 1, mSize - i);
                System.arraycopy(mValues, i, mValues, i + 1, mSize - i);
            }

            mKeys[i] = key;
            mValues[i] = value;
            mSize++;
        }
    }

    /**
     * Copies all of the mappings from the {@code other} to this map. The effect of this call is
     * equivalent to that of calling {@link #put(long, Object)} on this map once for each mapping
     * from key to value in {@code other}.
     */
    public void putAll(@NonNull LongSparseArray<? extends E> other) {
        for (int i = 0, size = other.size(); i < size; i++) {
            put(other.keyAt(i), other.valueAt(i));
        }
    }

    /**
     * Add a new value to the array map only if the key does not already have a value or it is
     * mapped to {@code null}.
     * @param key The key under which to store the value.
     * @param value The value to store for the given key.
     * @return Returns the value that was stored for the given key, or null if there
     * was no such key.
     */
    @Nullable
    public E putIfAbsent(long key, E value) {
        E mapValue = get(key);
        if (mapValue == null) {
            put(key, value);
        }
        return mapValue;
    }

    /**
     * Returns the number of key-value mappings that this LongSparseArray
     * currently stores.
     */
    public int size() {
        if (mGarbage) {
            gc();
        }

        return mSize;
    }

    /**
     * Return true if size() is 0.
     * @return true if size() is 0.
     */
    public boolean isEmpty() {
        return size() == 0;
    }

    /**
     * Given an index in the range <code>0...size()-1</code>, returns
     * the key from the <code>index</code>th key-value mapping that this
     * LongSparseArray stores.
     *
     * <p>The keys corresponding to indices in ascending order are guaranteed to
     * be in ascending order, e.g., <code>keyAt(0)</code> will return the
     * smallest key and <code>keyAt(size()-1)</code> will return the largest
     * key.</p>
     */
    public long keyAt(int index) {
        if (mGarbage) {
            gc();
        }

        return mKeys[index];
    }

    /**
     * Given an index in the range <code>0...size()-1</code>, returns
     * the value from the <code>index</code>th key-value mapping that this
     * LongSparseArray stores.
     *
     * <p>The values corresponding to indices in ascending order are guaranteed
     * to be associated with keys in ascending order, e.g.,
     * <code>valueAt(0)</code> will return the value associated with the
     * smallest key and <code>valueAt(size()-1)</code> will return the value
     * associated with the largest key.</p>
     */
    @SuppressWarnings("unchecked")
    public E valueAt(int index) {
        if (mGarbage) {
            gc();
        }

        return (E) mValues[index];
    }

    /**
     * Given an index in the range <code>0...size()-1</code>, sets a new
     * value for the <code>index</code>th key-value mapping that this
     * LongSparseArray stores.
     */
    public void setValueAt(int index, E value) {
        if (mGarbage) {
            gc();
        }

        mValues[index] = value;
    }

    /**
     * Returns the index for which {@link #keyAt} would return the
     * specified key, or a negative number if the specified
     * key is not mapped.
     */
    public int indexOfKey(long key) {
        if (mGarbage) {
            gc();
        }

        return ContainerHelpers.binarySearch(mKeys, mSize, key);
    }

    /**
     * Returns an index for which {@link #valueAt} would return the
     * specified key, or a negative number if no keys map to the
     * specified value.
     * Beware that this is a linear search, unlike lookups by key,
     * and that multiple keys can map to the same value and this will
     * find only one of them.
     */
    public int indexOfValue(E value) {
        if (mGarbage) {
            gc();
        }

        for (int i = 0; i < mSize; i++)
            if (mValues[i] == value)
                return i;

        return -1;
    }

    /** Returns true if the specified key is mapped. */
    public boolean containsKey(long key) {
        return indexOfKey(key) >= 0;
    }

    /** Returns true if the specified value is mapped from any key. */
    public boolean containsValue(E value) {
        return indexOfValue(value) >= 0;
    }

    /**
     * Removes all key-value mappings from this LongSparseArray.
     */
    public void clear() {
        int n = mSize;
        Object[] values = mValues;

        for (int i = 0; i < n; i++) {
            values[i] = null;
        }

        mSize = 0;
        mGarbage = false;
    }

    /**
     * Puts a key/value pair into the array, optimizing for the case where
     * the key is greater than all existing keys in the array.
     */
    public void append(long key, E value) {
        if (mSize != 0 && key <= mKeys[mSize - 1]) {
            put(key, value);
            return;
        }

        if (mGarbage && mSize >= mKeys.length) {
            gc();
        }

        int pos = mSize;
        if (pos >= mKeys.length) {
            int n = ContainerHelpers.idealLongArraySize(pos + 1);

            long[] nkeys = new long[n];
            Object[] nvalues = new Object[n];

            // Log.e("SparseArray", "grow " + mKeys.length + " to " + n);
            System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length);
            System.arraycopy(mValues, 0, nvalues, 0, mValues.length);

            mKeys = nkeys;
            mValues = nvalues;
        }

        mKeys[pos] = key;
        mValues[pos] = value;
        mSize = pos + 1;
    }

    /**
     * {@inheritDoc}
     *
     * <p>This implementation composes a string by iterating over its mappings. If
     * this map contains itself as a value, the string "(this Map)"
     * will appear in its place.
     */
    @Override
    public String toString() {
        if (size() <= 0) {
            return "{}";
        }

        StringBuilder buffer = new StringBuilder(mSize * 28);
        buffer.append('{');
        for (int i=0; i<mSize; i++) {
            if (i > 0) {
                buffer.append(", ");
            }
            long key = keyAt(i);
            buffer.append(key);
            buffer.append('=');
            Object value = valueAt(i);
            if (value != this) {
                buffer.append(value);
            } else {
                buffer.append("(this Map)");
            }
        }
        buffer.append('}');
        return buffer.toString();
    }
}