diff options
-rw-r--r-- | api/current.txt | 1 | ||||
-rw-r--r-- | api/system-current.txt | 1 | ||||
-rw-r--r-- | api/test-current.txt | 1 | ||||
-rw-r--r-- | core/java/android/util/LongSparseArray.java | 33 |
4 files changed, 34 insertions, 2 deletions
diff --git a/api/current.txt b/api/current.txt index 956559e6aa69..384434a832cc 100644 --- a/api/current.txt +++ b/api/current.txt @@ -40466,6 +40466,7 @@ package android.util { method public E get(long, E); method public int indexOfKey(long); method public int indexOfValue(E); + method public int indexOfValueByValue(E); method public long keyAt(int); method public void put(long, E); method public void remove(long); diff --git a/api/system-current.txt b/api/system-current.txt index 292f533cfd25..5c6c122a68a0 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -43632,6 +43632,7 @@ package android.util { method public E get(long, E); method public int indexOfKey(long); method public int indexOfValue(E); + method public int indexOfValueByValue(E); method public long keyAt(int); method public void put(long, E); method public void remove(long); diff --git a/api/test-current.txt b/api/test-current.txt index 592ec67170a1..1b17bd0a4f29 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -40546,6 +40546,7 @@ package android.util { method public E get(long, E); method public int indexOfKey(long); method public int indexOfValue(E); + method public int indexOfValueByValue(E); method public long keyAt(int); method public void put(long, E); method public void remove(long); diff --git a/core/java/android/util/LongSparseArray.java b/core/java/android/util/LongSparseArray.java index 6b45ff484e67..58ad2fd0c586 100644 --- a/core/java/android/util/LongSparseArray.java +++ b/core/java/android/util/LongSparseArray.java @@ -299,10 +299,39 @@ public class LongSparseArray<E> implements Cloneable { gc(); } - for (int i = 0; i < mSize; i++) - if (mValues[i] == value) + for (int i = 0; i < mSize; i++) { + if (mValues[i] == value) { return i; + } + } + return -1; + } + + /** + * Returns an index for which {@link #valueAt} would return the + * specified key, or a negative number if no keys map to the + * specified value. + * <p>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. + * <p>Note also that this method uses {@code equals} unlike {@code indexOfValue}. + */ + public int indexOfValueByValue(E value) { + if (mGarbage) { + gc(); + } + for (int i = 0; i < mSize; i++) { + if (value == null) { + if (mValues[i] == null) { + return i; + } + } else { + if (value.equals(mValues[i])) { + return i; + } + } + } return -1; } |