user75ponic user75ponic - 1 year ago 64
Java Question

Collections duplicates

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("Orange");
valSetOne.add("Apple");
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?

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download