I want to get weekdays for common time sets from the following hashmap I have the following HashMap :

  HashMap&lt;String, String&gt; days = new HashMap&lt;String, String&gt;(){
        days.put("MON", "9-5");
        days.put("TUE", "9-4");
        days.put("WED", "9-5");
        days.put("THU", "9-5");
        days.put("FR", "9-5");
        days.put("SAT", "Closed");
        days.put("SUN", "Closed");
};


I want to display following output :


  MON-WED-THU-FRI : 9-5,
  TUE: 9-4,
  SAT-SUN : Closed


NOTE:The value of hashmap("9-5") may change depending upon the inputs from the admin panel.  

I have done the below code but it's too lengthy:

    private void days() {

         String timing1 = null;
         String timing2 = null;
         String timing3 = null;
         String timing4 = null;
         String timing5 = null;
         String timing6 = null;
         String timing7 = null;

         ArrayList&lt;String&gt; time1 =new ArrayList&lt;&gt;();
         ArrayList&lt;String&gt; time2 =new ArrayList&lt;&gt;();
         ArrayList&lt;String&gt; time3 =new ArrayList&lt;&gt;();
         ArrayList&lt;String&gt; time4 =new ArrayList&lt;&gt;();
         ArrayList&lt;String&gt; time5 =new ArrayList&lt;&gt;();
         ArrayList&lt;String&gt; time6 =new ArrayList&lt;&gt;();
         ArrayList&lt;String&gt; time7 =new ArrayList&lt;&gt;();

         //iterating over the hash map and taking value ie time storing in respected variable

        for (Map.Entry&lt;String, String&gt; entry : days.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            System.out.println("key" + key + "Val" + value.toString());


            if (timing1 ==null || timing1.equals(value.toString())){
                timing1 = value.toString();
                continue;
            }

            if (timing2 ==null || timing2.equals(value.toString())){
                timing2 = value.toString();
                continue;
            }

            if (timing3 ==null || timing3.equals(value.toString())){
                timing3 = value.toString();
                continue;
            }

            if (timing4 ==null || timing4.equals(value.toString())){
                timing4 = value.toString();
                continue;
            }

            if (timing5 ==null || timing5.equals(value.toString())){
                timing5 = value.toString();
                continue;
            }

            if (timing6 ==null || timing6.equals(value.toString())){
                timing6 = value.toString();
                continue;
            }

            if (timing7 ==null || timing7.equals(value.toString())){
                timing7 = value.toString();

            }


        }


         //based on value from hash-map appending the key ie. days name(eg. MON) to the respected array-list

        for (Map.Entry&lt;String, String&gt; entry : days.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();


            if (timing1!=null &amp;&amp; timing1.equals(value.toString())){

                time1.add(key);
                continue;
            }

            if (timing2!=null &amp;&amp; timing2.equals(value.toString())){

                time2.add(key);
                continue;
            }

            if (timing3!=null &amp;&amp; timing3.equals(value.toString())){

                time3.add(key);
                continue;
            }

            if (timing4!=null &amp;&amp; timing4.equals(value.toString())){

                time4.add(key);
                continue;
            }

            if (timing5!=null &amp;&amp; timing5.equals(value.toString())){

                time5.add(key);
                continue;
            }

            if (timing6!=null &amp;&amp; timing6.equals(value.toString())){

                time6.add(key);
                continue;
            }

            if (timing7!=null &amp;&amp; timing7.equals(value.toString())){

                time7.add(key);
               // continue;
            }

        }
The thing you need to do is commonly called "grouping", i.e. you iterate over a given dataset, take one property (the time in your case) and collect the entries with the same property in a useful structure.

Fortunately, java (8+) already can do this for you by streaming through the map entries and grouping them all automagically:

    Map&lt;String, List&lt;Map.Entry&lt;String, String&gt;&gt;&gt; grouped = days.entrySet().stream()
        .collect(Collectors.groupingBy(Map.Entry::getValue));


OK, the data-structure looks a little wierd here, because your list consists of map-entries, but this was the main work. If you print this result, you already get (format added manually):

{
     9-4=[TUE=9-4],
     9-5=[MON=9-5, WED=9-5, THU=9-5, FRI=9-5],
     Closed=[SAT=Closed, SUN=Closed]
}


... which is exactly what you need from a data perspective. Using a further iteration over this map which uses the time as a key and the old map's entries as values to format the respective output should be quite trivial.

BTW: to keep the order of the days, you should use a LinkedHashMap instead of a HashMap.