title: Using multiple filters on Map and process each filter differently using Java 8
body: <p>I have a list of Subscription. Subscription is the class with properties Frequency and the amount.My goal is to get TOTALACCRUEDMONTHLYAMOUNT, so I approached it in two steps 1) By grouping the Subscriptions by Frequency, and 2)convert the amounts of Frequency types Quarterly, Semi-annually and Annually to Monthly. </p>
  
  <p>For Example: </p>
  
  <pre><code>Subscriptions [{
   subscription {
   frequency: Annually,
   amount    : 1200
   },
   subscription {
   frequency: Annually,
   amount    : 1200
   },
   subscription {
   frequency: Quarterly,
   amount    : 400
   },
   subscription {
   frequency: Quarterly,
   amount    : 400
   },
   subscription {
   frequency: Monthly,
   amount    : 100
   },
   subscription {
   frequency: Semi Annually,
   amount    : 600
   },
   subscription {
   frequency: Semi Annually,
   amount    : 600
   }
   }]
  </code></pre>
  
  <p>I was able to group by using the below logic</p>
  
  <pre><code>Map&lt;String,BigDecimal&gt; amountByFrequency = subscriptions.stream()
      .collect(Collectors.groupingBy(
                 subscription::getFrequency,
                     Collectors.mapping(
                     Subscription::getAmount,
                         Collectors.reducing(
                           BigDecimal.ZERO,
                               (total,elm) -&gt;(total.add(elm))))));                                                                                          
  </code></pre>
  
  <p>so that I could get the Map as below now</p>
  
  <pre><code>{[Monthly, 100],[Quarterly, 800],[Annually, 2400 ],[Semi Annually, 1200]}
  </code></pre>
  
  <p>However, still converting the amounts to monthly amounts is not done yet, so I achieved that using the below switch statement.</p>
  
  <pre><code>for(Map.Entry&lt;String, BigDecimal&gt; entry : amountByFrequency.entrySet()) {
  
          switch(entry.getKey()) {
          case "Monthly":
              sum = sum.add(entry.getValue());
              break;
          case "Quaterly":
              sum = sum.add(entry.getValue().divide(new BigDecimal("4"),2,RoundingMode.HALF_UP));
              break;
          case "Semiannually":
              sum = sum.add(entry.getValue().divide(new BigDecimal("6"),2,RoundingMode.HALF_UP));
              break;
          case "Yearly":
          case "Annually":
              sum = sum.add(entry.getValue().divide(new BigDecimal("12"),2,RoundingMode.HALF_UP));
              break;
          }
      }
  </code></pre>
  
  <p>Is there a way to replace the above code with Stream API??
  The whole main class is below.</p>
  
  <pre><code>class Main {
    public static void main(String[] args) {
  
      List&lt;Subscription&gt; subscriptions = getSubscriptionsList();
      BigDecimal sum = BigDecimal.ZERO;
  
     //Group by frequency.
      Map&lt;String, BigDecimal&gt; amountByFrequency = subscriptions.stream()
              .collect(Collectors.groupingBy(Subscription::getFrequency,     Collectors.mapping(Subscription::getAmount,
                      Collectors.reducing(BigDecimal.ZERO, (total, elm) -&gt;   (total.add(elm))))));
  
    //convert amount to monthly
         for (Map.Entry&lt;String, BigDecimal&gt; entry :       amountByFrequency.entrySet()) {
  
          switch (entry.getKey()) {
          case "Monthly":
              sum = sum.add(entry.getValue());
              break;
          case "Quaterly": // divide by 4, to convert to Monthly
              sum = sum.add(entry.getValue().divide(new BigDecimal("4"), 2,    RoundingMode.HALF_UP));
              break;
          case "Semiannually": // divide by 6, to convert to Monthly
              sum = sum.add(entry.getValue().divide(new BigDecimal("6"), 2, RoundingMode.HALF_UP));
              break;
          case "Yearly":
          case "Annually":  // divide by 12, to convert to Monthly
              sum = sum.add(entry.getValue().divide(new BigDecimal("12"), 2, RoundingMode.HALF_UP));
              break;
          }
      }
  
      System.out.println("java 8--" + sum); //  accrued total subscription     amount
  
  }
  
  static class Subscription {
      private String frequency;
  
      private BigDecimal amount;
  
      public String getFrequency() {
          return frequency;
      }
  
      public void setFrequency(String frequency) {
          this.frequency = frequency;
      }
  
      public BigDecimal getAmount() {
          return amount;
      }
  
      public void setAmount(BigDecimal amount) {
          this.amount = amount;
      }
  
  }
  
  
  //build dummy subscription list.
  public static List&lt;Subscription&gt; getSubscriptionsList() {
  
      List&lt;Subscription&gt; SubscriptionsList = new ArrayList&lt;&gt;();
  
      Subscription SubscriptionMonthly1 = new Subscription();
      SubscriptionMonthly1.setFrequency("Monthly");
      SubscriptionMonthly1.setAmount(new BigDecimal(1000.00));
  
      Subscription SubscriptionMonthly2 = new Subscription();
      SubscriptionMonthly2.setFrequency("Monthly");
      SubscriptionMonthly2.setAmount(new BigDecimal(1000.00));
  
      Subscription SubscriptionQuaterly1 = new Subscription();
      SubscriptionQuaterly1.setFrequency("Quaterly");
      SubscriptionQuaterly1.setAmount(new BigDecimal(1000.00));
  
      Subscription SubscriptionQuaterly2 = new Subscription();
      SubscriptionQuaterly2.setFrequency("Quaterly");
      SubscriptionQuaterly2.setAmount(new BigDecimal(1000.00));
  
      Subscription SubscriptionSemiannually1 = new Subscription();
      SubscriptionSemiannually1.setFrequency("Semiannually");
      SubscriptionSemiannually1.setAmount(new BigDecimal(1000.00));
  
      Subscription SubscriptionSemiannually2 = new Subscription();
      SubscriptionSemiannually2.setFrequency("Semiannually");
      SubscriptionSemiannually2.setAmount(new BigDecimal(1000.00));
  
      Subscription SubscriptionYearly1 = new Subscription();
      SubscriptionYearly1.setFrequency("Yearly");
      SubscriptionYearly1.setAmount(new BigDecimal(1000.00));
  
      Subscription SubscriptionYearly2 = new Subscription();
      SubscriptionYearly2.setFrequency("Yearly");
      SubscriptionYearly2.setAmount(new BigDecimal(1000.00));
  
      Subscription SubscriptionAnnually1 = new Subscription();
      SubscriptionAnnually1.setFrequency("Annually");
      SubscriptionAnnually1.setAmount(new BigDecimal(1000.00));
  
      Subscription SubscriptionAnnually2 = new Subscription();
      SubscriptionAnnually2.setFrequency("Annually");
      SubscriptionAnnually2.setAmount(new BigDecimal(1000.00));
  
      SubscriptionsList.add(SubscriptionMonthly1);
      SubscriptionsList.add(SubscriptionMonthly2);
      SubscriptionsList.add(SubscriptionQuaterly1);
      SubscriptionsList.add(SubscriptionQuaterly2);
      SubscriptionsList.add(SubscriptionSemiannually1);
      SubscriptionsList.add(SubscriptionSemiannually2);
      SubscriptionsList.add(SubscriptionYearly1);
      SubscriptionsList.add(SubscriptionYearly2);
      SubscriptionsList.add(SubscriptionAnnually1);
      SubscriptionsList.add(SubscriptionAnnually2);
  
      return SubscriptionsList;
  
  }
  </code></pre>
  
  <p>}</p>