title: Java multiply value from a List and Map
body: <p>I have a code that will output the data from a List and Map. How do I multiply the corresponding number with each other?</p>
  
  <p>The output from List</p>
  
  <pre><code>{prepare=0.25, saturday=0.5, republic=0.25, approach=0.25, ...}
  </code></pre>
  
  <p>The output from Map</p>
  
  <pre><code>[0, 1, 0, 0,...]
  </code></pre>
  
  <p>I need to make them multiply with corresponding number and sum them up. The <strong>total will be my desired output</strong> like this:</p>
  
  <pre><code>(0.25*0)+(0.5*1)+(0.25*0)+(0.25*0).... = total 
  </code></pre>
  
  <p>This is my example code:</p>
  
  <pre><code>import java.util.*;
  import java.util.stream.Collectors;
  
  class WordOcc {
    public static void main(String[] args) {
      String someText = "hurricane gilbert head dominican coast saturday. hurricane gilbert sweep dominican republic sunday civil  prepare high wind heavy rain high sea. storm approach southeast sustain wind  mph   mph. there alarm civil defense director   a television alert shortly midnight saturday.";
  
      List&lt;List&lt;String&gt;&gt; sort = new ArrayList&lt;&gt;();
      Map&lt;String, ArrayList&lt;Integer&gt;&gt; res = new HashMap&lt;&gt;();
  
      for (String sentence : someText.split("[.?!]\\s*"))
      {
          sort.add(Arrays.asList(sentence.split("[ ,;:]+"))); //put each sentences in list
      }
  
      // put all word in a hashmap with 0 count initialized
      int sentenceCount = sort.size();
  
      for (List&lt;String&gt; sentence: sort) {
          sentence.stream().forEach(x -&gt; res.put(x, new ArrayList&lt;Integer&gt;(Collections.nCopies(sentenceCount, 0))));
      }
  
      int index = 0;
      // count the occurrences of each word for each sentence.
      for (List&lt;String&gt; sentence: sort) {
          for (String q : sentence) {
              res.get(q).set(index, res.get(q).get(index) + 1); 
          }
          index++;
      }
  
  
      Map&lt;String,Float&gt;word0List = getFrequency(res);
  
      System.out.println(word0List + "\n"); //print the list value      
  
      //get first sentence count value from each word
      List&lt;Integer&gt; sentence0List = getSentence(0, res);
  
      System.out.println(sentence0List + "\n"); //print the map value
    }
  
    static List&lt;Integer&gt; getSentence(int sentence, Map&lt;String, ArrayList&lt;Integer&gt;&gt; map) {
        return map.entrySet().stream().map(e -&gt; e.getValue().get(sentence)).collect(Collectors.toList());
     }
  
   static Map&lt;String, Float&gt; getFrequency(Map&lt;String, ArrayList&lt;Integer&gt;&gt; stringMap) {
    Map&lt;String, Float&gt; res = new HashMap&lt;&gt;();
   stringMap.entrySet().stream().forEach(e -&gt; res.put(e.getKey(), e.getValue().stream().mapToInt(Integer::intValue).sum() / (float)e.getValue().size()));
          return res;
        }
  
  }
  </code></pre>
  
  <p>Appreciate any help on this. Thanks!</p>