title: How to write a custom Comparator for TreeMap in Java?
body: <p>I want to store  key-value pairs in TreeMap and sort the entries based on the value of Key as per following logic:</p>
  
  <p>Sort by the length of the key. If the length of two keys is same then sort them alphabetically. Example, for the following key-value pairs.</p>
  
  <pre><code>IBARAKI MitoCity
  TOCHIGI UtunomiyaCity
  GUNMA MaehashiCity
  SAITAMA SaitamaCity
  CHIBA ChibaCity
  TOKYO Sinjyuku
  KANAGAWA YokohamaCity
  </code></pre>
  
  <p>The expected output is like this.</p>
  
  <pre><code>CHIBA : ChibaCity
  GUNMA : MaehashiCity
  TOKYO : Sinjyuku
  IBARAKI : MitoCity
  SAITAMA : SaitamaCity
  TOCHIGI : UtunomiyaCity
  KANAGAWA : YokohamaCity
  </code></pre>