大家好我一直试图修复此问题但无法修复,问题是删除无法在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
请帮忙。
编辑: 非常感谢所有那些已经花费了宝贵时间回答,upvote和downvote.i已经改变了代码的人,现在工作正常,
更新了工作代码。
此致 安东尼
答案 0 :(得分:2)
if (Integer.parseInt(daysEntry.getKey().toString()) == Integer.parseInt(holyDayEntry.getKey().toString()))
daysMap.remove( Integer.parseInt(holyDayEntry.getKey().toString()));
break;
如果条件失败,它将会突破,而不是为Map的其余部分循环
答案 1 :(得分:2)
您没有说明您遇到的具体错误,但有一些一般性意见要做。
你的break语句在“if”之外,所以代码将经过然后在第一个键之后退出while循环。换句话说,你实际上并没有迭代所有键。
其次,你不需要来迭代地图并测试匹配的密钥,调用remove会为你做这件事。
第三个(作为一个兴趣点)在迭代它时从地图中删除项将导致下一次循环时迭代器的异常。在这种特殊情况下没有突出显示,因为break语句可以帮助您避免错误,但值得注意未来。
答案 2 :(得分:1)
你需要parantheses;你需要以下内容:
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;
答案 3 :(得分: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
错误位置的错误
答案 4 :(得分:0)
您可以使用ConcurrentHashMap执行此操作,
public static Map 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;
}