title: Grouping of FizzBuzz numbers
body: <p>I am trying to code FizzBuzz problem in Java8. It is working fine and I am getting desired output. For the number divisible by "3", it should return "Fizz", for the number divisible by "5", it should return "Buzz" and for the number divisible by both, it should return "FizzBuzz".</p>
  
  <p>If I pass the value as "15" it returns:</p>
  
  <pre><code>["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
  </code></pre>
  
  <p>Now, I am stuck in one thing. I want to get an output as follows, if I pass the value as "15":</p>
  
  <pre><code>{"Fizz": [3, 6, 9, 12],"Buzz": [5, 10],"FizzBuzz": [15]}
  </code></pre>
  
  <p>I want to group the numbers by Fizz, Buzz, and FizzBuzz.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public class FizzBuzzService {
  
  private Map&lt;Rule, String&gt; ruleContainers = new HashMap();
  private Set&lt;Rule&gt; rules  = new HashSet();
  
  public FizzBuzzService(){
      addRule(i -&gt; i % 15 == 0, "FizzBuzz");
      addRule(i -&gt; i % 3 == 0, "Fizz");
      addRule(i -&gt; i % 5 == 0, "Buzz");
  }
  
  public void addRule(Rule rule, String res) {
      rules.add(rule);
      ruleContainers.put(rule, res);
  }
  
  public String getValue(int i) {
      for (Rule rule : rules) {
          if (rule.apply(i)) {
              return ruleContainers.get(rule);
          }
      }
      return String.valueOf(i);
  }
  
  //then the origin code should be as follows:
  public List&lt;String&gt; fizzBuzz(int n) {
       List&lt;String&gt; res = new ArrayList();
       for(int i = 1; i &lt;= n; i++){
           res.add(getValue(i));
       }
       return res;
   }
  
  interface Rule{
      boolean apply(int i);
  }
  }
  </code></pre>
  
  <p>I would really appreciate if someone can guide me. Thanks</p>