How to check if TreeMap> contains a value? in Java

前端 未结 3 388
刺人心
刺人心 2021-01-28 14:39

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

/*I have TreeMap> map with these element         


        
相关标签:
3条回答
  • 2021-01-28 15:07

    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");
    
    0 讨论(0)
  • 2021-01-28 15:19
    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;
    } 
    
    0 讨论(0)
  • 2021-01-28 15:33

    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")
                    {
    
                    }
                }
    
    0 讨论(0)
提交回复
热议问题