I am adding the following to the HashMap collection after each row in the UI form
Declaration
Map<String, List<String>> map = new HashMap<String, List<String>>();
List<String> valSetOne = new ArrayList<String>();
Code snippet
valSetOne.add("BMW");
valSetOne.add("Audi");
map.put("A1", valSetOne);
When I am in the second row in UI, I would like to check the above combination exist in the HashMap collection.
How can I check for the combination of values in each row?
2 Answers 2
If you need performance with respect to checking if a value exists and dont care about the order in each row, you can use Set interface and use a HashSet instead of List
Map<String, Set<String>> map = new HashMap<String, Set<String>>();
Set<String> valSetOne = new HashSet<String>();
To check, use
Set<String> set= map.get("A1");//returns null if key does not exists(you also use containsKey() to check if key exists)
set.contains("value")
If you dont need performance, u can just use
List<String> valSetOne = new ArrayList<String>();
List<String> list = map.get("A1");//returns null if key does not exists(you also use containsKey() to check if key exists )
list .contains("value") to check if it exists
4 Comments
Depends on what you'd like to check for existence, there are two Map methods:
containsKey and contiansValue.
need to check is key A1 there? -> use containsKey
need to check is there a list with Orange and Apple use -> containsValue
containsValue()method?MapofLists. If you callcontainsValue(myList), it will check ifmyListis already contained within the values.