Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7965359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:08:02+00:00 2026-06-04T06:08:02+00:00

I am trying to remove value from the map which is iterated when i

  • 0

I am trying to remove value from the map which is iterated when i try this i get the following exception .

Exception in thread "main" java.util.ConcurrentModificationException

My code is below.

public static Map removeHolyday(Map daysMap,Map holydayMap){
        Iterator<Map.Entry> workingDays = daysMap.entrySet().iterator();
          while (workingDays.hasNext()) {
              Map.Entry workingDaysEntry = workingDays.next();
              System.out.println("Key = " + workingDaysEntry.getKey() + ", Value = " + workingDaysEntry.getValue());
              Iterator<Map.Entry> holydays = daysMap.entrySet().iterator();
              while (holydays.hasNext()) {
                  Map.Entry holydayEntry = holydays.next();
                  if(workingDaysEntry.getKey().toString().equals(holydayEntry.getKey().toString())){
                      daysMap.remove(workingDaysEntry.getKey().toString());
                  }
              }
          }
        return daysMap;
    }

Please help me to solve this.

EDIT :

this is the code i use but the value is not gettig deleted from map;

package sample;

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;

class Workindays {
    public static int findNoOfDays(int year, int month, int day) {
        Calendar calendar = Calendar.getInstance();
        System.out.println("month : " + month);
        calendar.set(year, month - 1, day);
        int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        System.out.println("days :"+days);
        return days;
    }
    public static Map 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 holydaysMap = new ConcurrentHashMap(); 
        holydaysMap.put("17","17-04-2012");
        holydaysMap.put("25","25-04-2012");
        return holydaysMap;
    }
    public static Map getWorkingDaysMap(int year, int month, int day){
        int totalworkingdays=0,noofdays=0;
        String nameofday = "";
        Map workingDaysMap = new HashMap();
        Map holyDayMap = new ConcurrentHashMap();
        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(i,formatedDate);
                totalworkingdays++;
            }
        }
        workingDaysMap.put("totalworkingdays", totalworkingdays);
        System.out.println("removeHolyday : "+removeHolyday(workingDaysMap,holyDayMap));
        return workingDaysMap;
    }

    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(daysEntry.getKey().equals(holyDayEntry.getKey()))
                      daysMap.remove(holyDayEntry.getKey());
              }
          }
System.out.println(daysMap);
        return daysMap;
    }

    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();

        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);

    }
}

working code :

package sample;

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

public class Main {
    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(daysEntry.getKey().equals(holyDayEntry.getKey()))
                      daysMap.remove(holyDayEntry.getKey());
              }
          }
System.out.println(daysMap);
        return daysMap;
    }
    public static void main(String[] args) {

        Map holydaysMap = new ConcurrentHashMap(); 
        holydaysMap.put("17", "17-04-2012");
        holydaysMap.put("25", "25-04-2012");

    Map holydayMap = new HashMap(); 
    holydayMap.put("17", "17-04-2012");
    holydayMap.put("25", "25-04-2012");
    holydayMap.put("3", "03-04-2012");
    holydayMap.put("4", "04-04-2012");

    removeHolyday(holydayMap, holydaysMap);

}
}

Regards
Antony

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:08:03+00:00Added an answer on June 4, 2026 at 6:08 am

    In this case, it’s actually pretty easy – just change this line:

    daysMap.remove(workingDaysEntry.getKey().toString());
    

    to:

    workingDays.remove();
    

    While you’re iterating over a collection, you can only make changes to it via the iterator’s remove() method, basically. Note that some iterators don’t support removal – hopefully the implementation of map you’re using does…

    EDIT: I suspect you’ve got another bug, actually. This line:

    Iterator<Map.Entry> holydays = daysMap.entrySet().iterator();
    

    should probably be:

    Iterator<Map.Entry> holydays = holydayMap.entrySet().iterator();
    

    At the moment you’re not even using holydayMap. You should also break after the call to remove() – you can’t remove the same entry twice.

    EDIT: I think I’ve found the problem now, and you’d have found it yourself if you were using generics. The holyDayMap keys are all strings:

    Map holydaysMap = new ConcurrentHashMap(); 
    holydaysMap.put("17","17-04-2012");
    holydaysMap.put("25","25-04-2012");
    

    … but the working day map keys are integers:

        for (int i = 1; i <= noofdays; i++) {
            ...
            workingDaysMap.put(i,formatedDate);
        }
    

    Now “17” isn’t the same as 17, so no entries will match. If you declared your maps with their key/value types, you’d spot this earlier.

    Note that your “working” code sample doesn’t have this problem – it uses strings everywhere.

    (You should really consider whether strings are the right values to use to start with – consider using Joda Time and LocalDate for date representations…)

    EDIT: Here’s a short but complete program which shows remove() working:

    import java.util.*;
    
    public class Test {
        public static void main(String[] args) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("foo1", "a");
            map.put("foo2", "b");
            map.put("bar1", "c");
            map.put("bar2", "d");
            map.put("foo3", "e");
            System.out.println("Before: " + map);
    
            Iterator<String> iterator = map.keySet().iterator();
            while (iterator.hasNext()) {
                if (iterator.next().startsWith("foo")) {
                    iterator.remove();
                }
            }
            System.out.println("After: " + map);
        }
    }
    

    Output:

    Before: {foo3=e, foo2=b, foo1=a, bar1=c, bar2=d}
    After: {bar1=c, bar2=d}
    

    So you need to work out why your code doesn’t behave that way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to remove all but the first child component from a Java Container.
I am trying to remove the attribute xmlns=http://webdev2003.test.com from the following xml using xsl/xslt,
I was trying to remove a key value pair from the dictionary with CFDictionaryRemoveValue.
I am trying to remove two key value pairs from an array, I am
I'm trying to delete the highest value from a group. Given this data: group_table
i am trying to get the complete parameter map from the request object and
I am trying to remove certain values from an array containing input fields in
I'm trying to read a value from a list in a remote SharePoint site
I'm trying to remove a color from a picture imported (JPG) in Flash CS4
I'm trying to remove a class attribute from an element and add the class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.