简体   繁体   中英

Check if Java ArrayList of HashMap<String,String> contains a key

I have this type ArrayList<HashMap<String,String>> . I want to check if a key exist in that array list . I can do this through a for loop but is there any alternative way to do this ?

Along the line of @Bhushan's answer, create a new class that encapsulates the ArrayList of maps, and a Map of the key values. Eg

public class EncapsulatedLookup {
    private ArrayList<HashMap<String,String>> data;
    private Map<String, Integer> keyToArrayListPosition lookup; 
}

Add methods to ensure whenever you add/remove from data , you also update the lookup

While building those individual HashMaps, if you could build a super hashmap, then there is no need for for loop. But don't know if it is possible for you. Just look-up in that super-hashmap.

The most easiest way that comes into my mind and maight work is to extend the HashMap class and implement .equals() method. Anyway, this method also internally uses interation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM