Java Map Lambda Exception
<p>I have a List of Maps with certain keys that map to String values.</p>

<p>Something like <code>List&lt;Map&lt;String,String&gt;&gt; aMapList;</code></p>

<p><strong>Objective</strong> : Stream over this List of maps and collect values of a single key in all Maps.</p>

<p>How I'm doing this -></p>

<pre><code>key = "somekey";
aMapList.stream().map(a -&gt; a.get(key)).collect(Collectors.averagingInt());
</code></pre>

<p><strong>The Problem:</strong> 
I get exceptions <strong>due to</strong> a.get(key) if there is no such key! because averaging this will give a null. How do I check or make lambda ignore any such maps and move on.</p>

<p>I do know that I can add a filter on <code>a -&gt; a.contains(key)</code> and then proceed. 
Edit : I can also add more filters or simple check multiple conditions on one filter.
<strong>Possible Solution:</strong></p>

<pre><code>    aMapList.stream().filter(a -&gt; a.contains(key)).
         map(a -&gt; a.get(key)).collect(Collectors.averagingInt());
</code></pre>

<p>Can this be made prettier?  <strong>Instead of halting the operation, simply skip over them?</strong>
Is there some more generic way to skip over exceptions or nulls. 
For eg. We can expand the lambda and put a try-catch block, but I still need to return something, what if I wish to do an equivalent of "continue".</p>

<p>Eg.</p>

<pre><code>(a -&gt; {return a.get(key) }).
</code></pre>

<p>Can be expanded to --></p>

<pre><code>(a -&gt; {try{return a.get(key)} 
catch(Exception e){return null} }).
</code></pre>

<p>The above still returns a null, instead of just skipping over.</p>

<p>I'm selecting the best answer for giving two options, But I do not find any of them prettier. Chaining filters seems to be the solution to this.</p>