0

I'm just wondering how to check if TreeMap> contains a value in Java? For Example:

/*I have TreeMap<String,ArrayList<String>> map with these elements
 * {color = ["red","blue","green"], shape=["square", "circle"]}
 */
 System.out.println(map.containsValue("square")); //This return false
 System.out.println(map.values().contains("square")); //This return false also

I always get false when I use containsValue() or contains() method. Anybody know why and can give me suggestions, please?

Thanks Ed

zangetsKid
  • 27
  • 3
  • 12
  • 2
    The `Map#values()` returns a `Collection>`, not a `ArrayList` or so as you seemed to expect. A string `"square"` can never equal any `ArrayList`. – BalusC Aug 10 '13 at 19:52
  • 1
    Is there a reason you are using a `List` rather than a `Set`? Can you have duplicates? I only ask because the `contains` method on `List` is `O(n)` and the same method on `Set` is `O(1)`. – Boris the Spider Aug 10 '13 at 19:59
  • @ Borris the Spider: hmm..actually the values in the list will be unique, so I guess using set is better..Thx for bringing it up..would it be the same way as above though, to see if this map contains a value? Thx – zangetsKid Aug 10 '13 at 20:17
  • You would use one of the methods suggested below, basically loop over the `Map` and check whether any of the `Set`s contain your value. You should also [program to the interface](http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) so your `Map` should be `Map>`. That way you can select the best `Collection` for the job. – Boris the Spider Aug 11 '13 at 08:23

3 Answers3

2

Something like below would work

 Map<String,ArrayList<String>> map=new TreeMap<String, ArrayList<String>>();
            Collection<ArrayList<String>> values=map.values();
            for(ArrayList<String> list:values)
            {
                list.contains("text_to_search")
                {

                }
            }
Algorithmist
  • 6,657
  • 7
  • 35
  • 49
1

You're testing to see whether the map contains a String, "square"-- but the values in your map are ArrayList<String> objects.

If you know that you're looking for a shape, you can first get the "shape" list, and test to see whether it contains the specific shape "square".

  ArrayList<String> shapes = map.get("shape");
  boolean containsSquare = shapes.contains("square");
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • Thx! Now I know why.. :D – zangetsKid Aug 10 '13 at 20:20
  • You're welcome. And welcome to StackOverflow. You can *upvote* answers you find useful by clicking on the up-triangle to the left. You can *accept* the most useful answer by clicking on the checkmark to the left. – Andy Thomas Aug 10 '13 at 20:52
0
public boolean valueExists(String value){
for(Map.Entry<String,ArrayList<String>> entry : treeMap.entrySet()) {
  String key = entry.getKey();
  ArrayList<String> values = entry.getValue();
  for (String str:values){
     if (value.equals(str)){
         return true;
     }
  }

}
return false;
} 
Abdullah Shoaib
  • 2,065
  • 2
  • 18
  • 26