-2

大家好,我一直在尝试解决此问题,但无法解决,问题是删除在 hashmap 中不起作用。

请看下面的代码。

 package com.org.common;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@SuppressWarnings("unused")
class Workindays {
    public static int findNoOfDays(int year, int month, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day);
        int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        return days;
    }
    public static Map<String, String> getHolydaysMap(int year, int month, int day) {
        //connect with database and check whether the date is holyday query is = SELECT * FROM holiday_calendar h WHERE date >='2008-10-01' AND date <='2008-10-30' AND type='Fixed';
        Map<String, String> holydaysMap = new LinkedHashMap<String,String>(); 

        holydaysMap.put("20","17-04-2012");
        holydaysMap.put("10","25-04-2012");
        return holydaysMap;
    }
    public static Map<String, String> getWorkingDaysMap(int year, int month, int day){
        int totalworkingdays=0,noofdays=0;
        String nameofday = "";
        ConcurrentHashMap<String,String> workingDaysMap = new ConcurrentHashMap<String,String>();
        Map<String,String> holyDayMap = new LinkedHashMap<String,String>();
        noofdays = findNoOfDays(year,month,day);
        holyDayMap = getHolydaysMap(year,month,day);

        for (int i = 1; i <= noofdays; i++) {
            Date date = (new GregorianCalendar(year,month - 1, i)).getTime(); // year,month,day
            SimpleDateFormat f = new SimpleDateFormat("EEEE");
            nameofday = f.format(date);
            String formatedDate = i+"-"+month+"-"+year;
            if(!(nameofday.equals("Saturday") || nameofday.equals("Sunday"))){
                workingDaysMap.put(String.valueOf(i),formatedDate);
                totalworkingdays++;
            }
        }
        workingDaysMap.put("totalworkingdays", String.valueOf(totalworkingdays));

        System.out.println("removeHolyday : "+removeHoliday(workingDaysMap,holyDayMap));
        return workingDaysMap;
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static Map removeHoliday(ConcurrentHashMap daysMap, Map holydayMap) {
        Iterator<Map.Entry> holyDayiterator = holydayMap.entrySet().iterator();
        while (holyDayiterator.hasNext()) {
            Map.Entry holyDayEntry = holyDayiterator.next();

            Iterator<Map.Entry> daysiterator = daysMap.entrySet().iterator();
            while (daysiterator.hasNext()) {
                Map.Entry daysEntry = daysiterator.next();
                 if(daysEntry.getKey().equals(holyDayEntry.getKey()))
                      daysMap.remove(holyDayEntry.getKey());
            }
        }
        return daysMap;
    }
    @SuppressWarnings({ "rawtypes" })
    public static void main(String[] args) {
        String delimiter = null, dateValues[] = null, startDate = "01-04-2012";
        int year = 0,month=0,day=0,totalworkingdays = 0;
        Map workingDaysMap = new LinkedHashMap<String,String>();

        startDate = "01-04-2012";
        delimiter = "-";
        dateValues = startDate.split(delimiter);

        year = Integer.parseInt(dateValues[2]);
        month = Integer.parseInt(dateValues[1]);
        day = Integer.parseInt(dateValues[0]);

        workingDaysMap = getWorkingDaysMap(year, month, day);
        //System.out.println("workingdays map : "+workingDaysMap);

    }
}

这是我用来删除值的方法

removeHolyday

我刚刚给出了示例值,以通过以下方法进行测试。

getHolydaysMap

请帮忙。

编辑:感谢所有花费宝贵时间回答,赞成和反对的人。我已经更改了代码,现在工作正常,

更新的工作代码。

问候安东尼

4

5 回答 5

2
  if (Integer.parseInt(daysEntry.getKey().toString()) ==   Integer.parseInt(holyDayEntry.getKey().toString()))
                daysMap.remove( Integer.parseInt(holyDayEntry.getKey().toString()));
            break;

如果条件失败,它将爆发而不是循环地图的其余部分

于 2012-05-22T11:26:50.347 回答
2

您没有说明您遇到的具体错误,但有一些一般性的评论。

您的 break 语句位于“if”之外,因此代码将通过,然后在第一个键后退出 while 循环。换句话说,您实际上并没有遍历所有键。

其次,您不需要迭代地图并测试匹配键,无论如何调用 remove 都会为您完成此操作。

第三(作为兴趣点)在迭代地图时从地图中删除项目将导致下一次循环时迭代器出现异常。在这种特殊情况下没有突出显示,因为 break 语句可以避免您遇到错误,但值得注意的是未来。

于 2012-05-22T11:27:17.793 回答
1

在您的 removeHoliday 方法中,您需要括号;您需要以下内容:

if (Integer.parseInt(daysEntry.getKey().toString()) == Integer.parseInt(holyDayEntry.getKey().toString())) {
    daysMap.remove( Integer.parseInt(holyDayEntry.getKey().toString()));
    break;
}

代替

if (Integer.parseInt(daysEntry.getKey().toString()) ==     Integer.parseInt(holyDayEntry.getKey().toString()))
    daysMap.remove( Integer.parseInt(holyDayEntry.getKey().toString()));
break;
于 2012-05-22T11:28:14.680 回答
1

考虑这样重写removeHolydays

代替

  public static Map removeHolyday(Map daysMap, Map holydayMap) {
    Iterator<Map.Entry> holyDayiterator = holydayMap.entrySet().iterator();
    while (holyDayiterator.hasNext()) {
        Map.Entry holyDayEntry = holyDayiterator.next();
        Iterator<Map.Entry> daysiterator = daysMap.entrySet().iterator();

        while (daysiterator.hasNext()) {
            Map.Entry daysEntry = daysiterator.next();
            if (Integer.parseInt(daysEntry.getKey().toString()) == Integer.parseInt(holyDayEntry.getKey().toString()))
                daysMap.remove( Integer.parseInt(holyDayEntry.getKey().toString()));
            break;
        }
    }
    return daysMap;
}

它应该是:

public static Map<Integer, String> remove(Map<Integer, String> daysMap, Map<Integer, String> holydayMap){
     for(Integer i : holydayMap.keySet())
         daysMap.remove(i);
     return daysMap;
}

break正如指出的那样,这将消除错误位置的错误ejb_guy

于 2012-05-22T11:29:03.310 回答
0

您可以使用 ConcurrentHashMap 来执行此操作,

公共静态地图 removeHolyday(ConcurrentHashMap daysMap,Map HolydayMap){

编辑:使用下面的代码来做,

public static Map removeHoliday(ConcurrentHashMap daysMap, Map holydayMap) {
        Iterator<Map.Entry> holyDayiterator = holydayMap.entrySet().iterator();
        while (holyDayiterator.hasNext()) {
            Map.Entry holyDayEntry = holyDayiterator.next();
            if(daysMap.containsKey(holyDayEntry.getKey())){
                daysMap.remove(holyDayEntry.getKey());
            }
        }
        return daysMap;
    }
于 2012-05-22T11:55:04.317 回答