Skip to content

Instantly share code, notes, and snippets.

@caseycrites
Created August 24, 2012 17:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caseycrites/3453143 to your computer and use it in GitHub Desktop.
Save caseycrites/3453143 to your computer and use it in GitHub Desktop.
package com.urbanairship.util;
import android.util.SparseArray;
public class SparseStringArray extends SparseArray<String> {
public SparseStringArray() {
this(10);
}
public SparseStringArray(int initialCapacity) {
super(initialCapacity);
}
@Override
public int indexOfValue(String value) {
int count = this.size();
for (int i=0; i<count; i++) {
String currentValue = this.valueAt(i);
if ((currentValue == null && value == null) ||
(currentValue != null && currentValue.equals(value))) {
return i;
}
}
return -1;
}
}
@kevinsawicki
Copy link

Here's a faster version using a reduced number of checks:

@Override
public int indexOfValue(final String value) {
    final int count = size();
    if (value != null) {
        for (int i = 0; i < count; i++)
            if (value.equals(valueAt(i)))
                return i;
    } else {
        for (int i = 0; i < count; i++)
            if (valueAt(i) == null)
                return i;
    }
    return -1;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment