title: How to group and count elements in a sublist using java streams
body: <p>I want to group and count elements in a sub list using java streams.</p>
  
  <p>For Example, I have an answer of type AnswerWithOneCorrectOption which looks like:</p>
  
  <pre><code>class AnswerWithOneCorrectOption {
       Long choiceId;
  }
  </code></pre>
  
  <p>This answer type has only one correct option and is stored in "AnswerWithOneCorrectOption.id". I am streaming through a list of AnswerWithOneCorrectOption's, grouping based on id and counting using:</p>
  
  <pre><code>private Map&lt;Long, Long&gt; countChoicesAndGroup(List&lt;AnswerWithOneCorrectOption&gt; answers){
  
  Map&lt;Long, Long&gt; map = answers.parallelStream()
               .collect(Collectors.groupingBy(AnswerWithOneCorrectOption::getChoiceId, 
   Collectors.counting())); 
  
   return map;
  }
  </code></pre>
  
  <p>Suppose I have another answer type that can have multiple correct options. I am saving those options in <code>List&lt;Long&gt; choiceIds</code>. </p>
  
  <pre><code>class AnswerWithMultipleCorrectOptions {
       List&lt;Long&gt; choiceIds;
  }
  </code></pre>
  
  <p>How can I group by choiceId's in <code>List&lt;Long&gt; choiceIds</code> and count?</p>