title: Same Code is producing different outputs for Java 7 & 8
body: <p>On Giving Input as <strong>91912323</strong> it gives output as <strong>33221199</strong> for Java 8 while in Java 7 the output is <strong>11223399</strong>.</p>
  
  <p>The problem statement is also provided for your reference.</p>
  
  <p>Any help would be appreciated.</p>
  
  <blockquote>
    <p>A string consists of digits from 1-9 will be passed as input. The
    program must print the digits sorted based on the number of
    occurrence. If one or more digits occur the same number of times, the
    smallest digit must be printed first.</p>
    
    <p>Input Format: The first line will contain the N digits from 1-9</p>
    
    <p>Boundary Conditions: 3 &lt;= N &lt;= 30</p>
    
    <p>Output Format: The digits sorted based on the number of occurrence.</p>
    
    <p>Example Input/Output 1:</p>
    
    <p>Input: 4443338993</p>
    
    <p>Output: 3333444998</p>
    
    <p>Explanation: 3 occurs the most number of times (four times). Hence it
    is printed first. 4 occurs thrice and hence printed after the 3s. 9
    occurs twice and hence printed after the 4s. 8 occurs only once and
    hence printed after 9.</p>
    
    <p>Example Input/Output 2:</p>
    
    <p>Input: 95559998228</p>
    
    <p>Output: 99995552288</p>
    
    <p>Explanation: Here 2 and 8 occurs twice. Hence 2 being the smaller
    digit is printed before 8.</p>
  </blockquote>
  
  <pre><code>package E001;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.util.Comparator;
  import java.util.HashMap;
  import java.util.Map;
  import java.util.TreeMap;
  
  /**
   *
   * @author Anagh
   */
  
  public class CharOccurrences {
  
      public static void main(String[] args) throws IOException
      {
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          String input = br.readLine();
          char[] arr = input.toCharArray();
          HashMap&lt;String, Integer&gt; map = new HashMap&lt;&gt;();
          for(int i = 0; i &lt; arr.length; i++)
          {
              if(!map.containsKey(String.valueOf(arr[i])))
              {
                  map.put(String.valueOf(arr[i]), 1);
              }
              else
              {
                  map.put(String.valueOf(arr[i]), map.get(String.valueOf(arr[i]))+1);
              }
          }
          TreeMap&lt;String, Integer&gt; output = sortByValue(map);
          printMap(output);
  
      }
      public static TreeMap&lt;String, Integer&gt; sortByValue (HashMap&lt;String, Integer&gt; map) 
      {
      ValueComparator vc =  new ValueComparator(map);
      TreeMap&lt;String,Integer&gt; sortedMap = new TreeMap&lt;&gt;(vc);
      sortedMap.putAll(map);
      return sortedMap;
      }
  
      private static void printMap(TreeMap&lt;String, Integer&gt; map) {
          String key;
          int value;
          for (Map.Entry&lt;String, Integer&gt; entry : map.entrySet()) 
          {
              key = entry.getKey();
              value = entry.getValue();
              for(int j = 0; j &lt; value; j++)
              {
                  System.out.print(key);
              }
      }        
      }
  
  }
  class ValueComparator implements Comparator&lt;String&gt; {
  
      Map&lt;String, Integer&gt; map;
  
      public ValueComparator(Map&lt;String, Integer&gt; base) {
          this.map = base;
      } 
      @Override
      public int compare(String a, String b) {
          if (map.get(a) &gt; map.get(b)) {
              return -1;
          } else {
              return 1;
          } // returning 0 would merge keys 
      }
  }
  </code></pre>