我想从下面的hashmap获取常用时间集的工作日

时间:2018-02-06 08:32:27

标签: java android arraylist hashmap

我有一个以下的hashmap:

HashMap<String, String> days = new HashMap<String, String>(){
    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");
};

我想显示以下输出:

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

注意:hashmap(&#34; 9-5&#34;)的值可能会根据管理面板的输入而改变

我已经完成了以下代码,但它太冗长了:

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<String> time1 =new ArrayList<>();
    ArrayList<String> time2 =new ArrayList<>();
    ArrayList<String> time3 =new ArrayList<>();
    ArrayList<String> time4 =new ArrayList<>();
    ArrayList<String> time5 =new ArrayList<>();
    ArrayList<String> time6 =new ArrayList<>();
    ArrayList<String> time7 =new ArrayList<>();

迭代哈希映射并获取值,即存储在受尊重变量中的时间

for (Map.Entry<String, String> 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();
    }
}

基于哈希映射的值附加密钥即。天名称(例如MON)到受尊重的数组列表

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

    if (timing1!=null && timing1.equals(value.toString())){
        time1.add(key);
        continue;
    }

    if (timing2!=null && timing2.equals(value.toString())){
        time2.add(key);
        continue;
    }

    if (timing3!=null && timing3.equals(value.toString())){
        time3.add(key);
        continue;
    }

    if (timing4!=null && timing4.equals(value.toString())){
        time4.add(key);
        continue;
    }

    if (timing5!=null && timing5.equals(value.toString())){
        time5.add(key);
        continue;
    }

    if (timing6!=null && timing6.equals(value.toString())){
        time6.add(key);
        continue;
    }

    if (timing7!=null && timing7.equals(value.toString())){
        time7.add(key);
        // continue;
    }
}

2 个答案:

答案 0 :(得分:1)

请查看以下是否有帮助。但是,这些值不按此输出排序。

打印:9-4 TUE

9-5 THU WED FR MON

关闭SAT SUN

public void formatMap(HashMap<String,String> hMap){
    HashMap<String, String> formattedMap = new HashMap<String, String>();
    Set<String> lKeys = (Set)hMap.keySet();

    for(String sKey : lKeys){

        String sValue = hMap.get(sKey);
        if(formattedMap.get(sValue) == null){
            formattedMap.put(sValue, sKey);
        }else{
            String temp = formattedMap.get(sValue);
            temp += " "+sKey;
            formattedMap.put(sValue, temp);
        }

    }

    lKeys = (Set)formattedMap.keySet();
    for(String s : lKeys){
        System.out.println(s +" "+formattedMap.get(s));
    }

}

答案 1 :(得分:1)

使用某些对象会更容易:

首先是一个存储时间表的类

package schedule;

public class DaySchedule {

    String weekDay;
    int minHour, maxHour;
    boolean open;

    public DaySchedule(String weekDay, int minHour, int maxHour, boolean open) {
        this.weekDay = weekDay;
        this.minHour = minHour;
        this.maxHour = maxHour;
        this.open = open;
    }

    public String getWeekDay() {
        return weekDay;
    }


    /**
     * Returns a unique key based on min and max hour
     * -1 is the key for closed status
     * @return int value
     */
    public String getKey() {
        if (open)
            return String.valueOf(minHour)+"-"+String.valueOf(maxHour);
        else
            return "Closed";
    }



}

然后是一个实用程序类来构建最终结果。

package schedule;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ScheduleBuilder {

    HashMap<String, StringBuilder> builderHashMap = new HashMap<>();

    public void addSchedule(DaySchedule daySchedule) {
        String key = daySchedule.getKey();

        if (builderHashMap.containsKey(daySchedule.getKey())) {
            StringBuilder stringBuilder = builderHashMap.get(key);
            // If there is a string builder, there is already something in it, so add a -
            stringBuilder.append('-');
            stringBuilder.append(daySchedule.getWeekDay());
        } else {
            builderHashMap.put(key, new StringBuilder(daySchedule.getWeekDay()));
        }

    }


    public String toString(){

        StringBuilder result = new StringBuilder();

        Iterator iterator = builderHashMap.entrySet().iterator();
           while (iterator.hasNext()) {

               if(result.length()>0){
                   result.append(',');
               }

               Map.Entry pair = (Map.Entry)iterator.next();
               result.append(pair.getValue());
               result.append(":");
               result.append(pair.getKey());
               iterator.remove(); // avoids a ConcurrentModificationException
           }
      return result.toString();
    }
}

要测试......

package schedule;

import java.util.ArrayList;
import java.util.Iterator;

public class Test {

    public static void main(String[] args) {


        ArrayList<DaySchedule> dayScheduleArrayList = new ArrayList<DaySchedule>(7);

        dayScheduleArrayList.add(new DaySchedule("MON", 9, 5, true));
        dayScheduleArrayList.add(new DaySchedule("TUE", 9, 4, true));
        dayScheduleArrayList.add(new DaySchedule("WED", 9, 5, true));
        dayScheduleArrayList.add(new DaySchedule("THU", 9, 5, true));
        dayScheduleArrayList.add(new DaySchedule("FR", 9, 5, true));
        dayScheduleArrayList.add(new DaySchedule("SAT", 0, 0, false));
        dayScheduleArrayList.add(new DaySchedule("SUN", 0, 0, false));

        ScheduleBuilder scheduleBuilder = new ScheduleBuilder();
        for (Iterator<DaySchedule> iterator = dayScheduleArrayList.iterator(); iterator.hasNext(); ) {
            DaySchedule daySchedule = iterator.next();
            scheduleBuilder.addSchedule(daySchedule);
        }

        System.out.println("scheduleBuilder = " + scheduleBuilder);

    }
}