title: How to extract the a value from a JSON object by matching the key in a Map using Java 8?
body: <p>I'm using Java 8 and jackson to try and get an int value from a json object.  Here is some similar code I used to verify the structure.</p>
  
  <pre><code>HashMap&lt;String, Object&gt; myMap = new HashMap&lt;String, Object&gt;();  
  myMap
          .entrySet()
          .stream()
          .forEach(x -&gt; System.out.println(x.getKey() + " =&gt; " + x.getValue()));
  </code></pre>
  
  <p>and the result:</p>
  
  <p>myKey1 => {"number":1}</p>
  
  <p>myKey2 => {"number":1}</p>
  
  <p>myKey3 => {"number":2}</p>
  
  <p>What I'm trying to do is use the key, like myKey1 to find the json value i.e. {"number":1} and pull out the actual number, which for that key is 1.  </p>
  
  <p>However, I don't all know the values of the key's, just the key I want to match up.  So I have to do this dynamically.  I know the structure of the values and that is always the same, except the number can be different.  </p>
  
  <p>I think because I don't know the keys and their order, plus I'm using entrySet, that is forcing me into using Optional, which is making this more difficult.</p>
  
  <p>Here is my code where I'm actually using the key to pull the json value:</p>
  
  <pre><code>Optional&lt;Object&gt; object = myMap.entrySet().stream()
          .filter(e -&gt; e.getKey().equals(myKey))
          .map(Map.Entry::getValue)
          .findFirst();
  </code></pre>
  
  <p>However, the stream pulls back </p>
  
  <blockquote>
    <p>Optional[{"number":1}]</p>
  </blockquote>
  
  <p>and I can't seem to get the number out so I can return it from a method.  I don't actually
  have a Java class for the object, so I assume that is why it's returning Optional as I was getting a compile error without it, but I'm not sure.  </p>
  
  <p>Any ideas as to the right way to do this?</p>