1

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?

asked Feb 7, 2017 at 10:37
5
  • Use the containsValue() method? Commented Feb 7, 2017 at 10:37
  • @Kayaman Does contains check for entire row? Commented Feb 7, 2017 at 10:38
  • You don't have rows. You have a Map of Lists. If you call containsValue(myList), it will check if myList is already contained within the values. Commented Feb 7, 2017 at 10:41
  • Consider looking at google.github.io/guava/releases/19.0/api/docs/com/google/common/… - it might simplify your list-in-map processing considerably and provide you with some helpful utility methods. Commented Feb 7, 2017 at 11:39
  • @ArturBiesiadowski Thanks for the suggestion. We do have a restriction in using third party libraries and due to this we wouldn't be able to external JAR files. Commented Feb 7, 2017 at 11:40

2 Answers 2

3

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
answered Feb 7, 2017 at 10:46
Sign up to request clarification or add additional context in comments.

4 Comments

You need to check for both the values separately either in list or in a set. but set gives better performance if u just want to check whether it exists or not and dont care about the order
Oh yes I got your point, for A1 key, the combination of Orange and Apple exists or not correct?
yes. you need to run a loop to check whether orange as well as apple exists or not for A1
Thanks and much appreciated.
2

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

answered Feb 7, 2017 at 10:45

1 Comment

Thanks and much appreciated.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.