aperture
aperture

Reputation: 2895

Java/Android - Search ArrayList<HashMap<String, String>> for matching key -> value

I have an ArrayList of HashMaps, and I have the key and value of one HashMap that I'd like to use to match the value of another HashMap found at a different position..

For example:

1- [<key1, val1>, <key2, "some value">, <key3, val3>]

2- [<key1, val1>, <key2, val2>, <key3, val3>]

3- [<key1, "some value">, <key2, val2>, <key3, val3>]

And I know that Array.get(1).get(key2) == "some value"

How can I then use this to match the key1 at Array.position(2), and return the value of key3?

Or in other words, how can I find the position of an ArrayList given the value of one of it's containing key/value HashMaps?

Something like Array.where("key1" == "some value").

Thanks

Upvotes: 0

Views: 3803

Answers (1)

Khoa Le
Khoa Le

Reputation: 362

You have to do it brute-force way.

for(HashMap hm:HashMapArrayList) 
if(hm.get(key1).equal(yourDesireValue) 
{
//hm is the one you need to find out. Do something
}

Upvotes: 3

Related Questions