title: How to verify HashMap containsValue with partial String
body: <p>I was working on a program to validate that a map contains some set of values or not. <code>Map</code> API is having one method called as <code>map.containsValue(string)</code>. But, this method verify complete String as value. </p>
  
  <pre><code>import java.util.HashMap;
  import java.util.Map;
  
  public class TestMap {
  
      /**
       * @param args
       */
      public static void main(String[] args) {
          // TODO Auto-generated method stub
  
          Map&lt;Integer, String&gt; map = new HashMap&lt;Integer, String&gt;();
  
          map.put(6, "P_T");
          map.put(7, "P_Q_T");
          map.put(8, "T");
          map.put(9, "A");
          map.put(10, "B");
          map.put(11, "P_A");
          map.put(1, "P_1");
          map.put(2, "Q");
          map.put(3, "P_Q");
          map.put(4, "Q_T");
          map.put(5, "T");
  
          System.out.println("Map is = "+map);
  
          System.out.println("Result is = " + map.containsValue("P"));
      }
  }
  </code></pre>
  
  <p>Above program will return output as :</p>
  
  <pre><code>Map is = {1=P_1, 2=Q, 3=P_Q, 4=Q_T, 5=T, 6=P_T, 7=P_Q_T, 8=T, 9=A, 10=B, 11=P_A}
  Result is = false
  </code></pre>
  
  <p>My requirement is that <code>Result</code> should be true as, Map contains keys 1, 3, 6, 7, 11, which contains <code>char P</code> as value.</p>
  
  <p>I have a solution, that i could use loop to verify each value and then find <code>indexOf</code> each value for <code>P</code> char. </p>
  
  <pre><code>import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.Set;
  
  public class TestMap {
  
      /**
       * @param args
       */
      public static void main(String[] args) {
          // TODO Auto-generated method stub
  
          Map&lt;Integer, String&gt; map = new HashMap&lt;Integer, String&gt;();
  
          map.put(6, "P_T");
          map.put(7, "P_Q_T");
          map.put(8, "T");
          map.put(9, "A");
          map.put(10, "B");
          map.put(11, "P_A");
          map.put(1, "P_1");
          map.put(2, "Q");
          map.put(3, "P_Q");
          map.put(4, "Q_T");
          map.put(5, "T");
  
          System.out.println("Map is = " + map);
  
          System.out.println("Result is = " + map.containsValue("P"));
  
          System.out.println("Result from loop is = " + verifyMap(map));
  
      }
  
      private static boolean verifyMap(Map&lt;Integer, String&gt; map) {
          // TODO Auto-generated method stub
          Set&lt;Integer&gt; set = map.keySet();
          Iterator&lt;Integer&gt; itr = set.iterator();
          while (itr.hasNext()) {
              Integer integer = (Integer) itr.next();
              String str = map.get(integer);
              if (str.indexOf("P") &gt;= 0)
                  return true;
          }
          return false;
      }
  }
  </code></pre>
  
  <p>I have evaluated <code>string</code> with <code>indexOf</code> method instead of <code>contains</code>, which is marginally faster . 
  <a href="https://i.stack.imgur.com/1lm7V.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1lm7V.png" alt="String.class file from JAVA7"></a></p>
  
  <p><a href="https://stackoverflow.com/questions/498686/is-string-contains-faster-than-string-indexof">See this question</a></p>
  
  <p>This return the desired result as:</p>
  
  <pre><code>Result from loop is = true
  </code></pre>
  
  <p>But, I just want to know, is there any other way to verify same?</p>