Hi, here is my problem. I have the next task

//Write a program that removes from a given
//sequence all the numbers that present in it odd
//number of times. Example:
//{4, 2, 2, 5, 2, 3, 2, 3, 1, 5, 2} {5, 3, 3, 5}

The problem comes in the last for where iam trying to remove the element.

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public class Zadacha_7 {

	/**
	 * @param args
	 */
	
		//Write a program that removes from a given
		//sequence all the numbers that present in it odd
		//number of times. Example:
		//{4, 2, 2, 5, 2, 3, 2, 3, 1, 5, 2}  {5, 3, 3, 5}
		
	public static void main(String[] args) {
		int[] array = new int[] {-1,0,3,3,3,3,3, 4, 4, 2, 3, 3, 4, 3, 2};
		HashMap<Integer, Integer> numbers = new HashMap<Integer, Integer>();
		
		for ( int i = 0; i<array.length; i++)
		{
		
			if (numbers.containsKey(array[i])) {
				int temp = numbers.get(array[i]);
				temp++;
				numbers.put(array[i], temp);
			}
			else {
				int value =1;
				numbers.put(array[i], value);
			}
		
		}
		System.out.println ("Those are the numbers + the number of occuraces " + numbers);

		for (int i : numbers.keySet()){
			
			if (numbers.get(i) != null) {
				
				if (numbers.get(i) %2 == 0){
					numbers.remove(i);
					
				}
                                 i--;
			}	
		}
	
		System.out.println (numbers);
	}

}

Recommended Answers

All 8 Replies

The problem comes in the last lines for where iam trying to remove the element. It doesn`t do it for some reason.

This exception occures when one thread is trying to iterate throught the collection and other trying to modify the values in the same collection. Try copying the values to other collection and then modify them or make the methods which modify the values and method which iterates through the collection as synchronized. Hope its helpful

Can u give me an example. I cant understand what do u mean.

#
import java.util.Iterator;
#
import java.util.ArrayList;
#
 
#
public class RemoveElementThroughIteratorExample {
#
 
#
public static void main(String[] args) {
#
 
#
//create an ArrayList object
#
ArrayList aList = new ArrayList();

 
//populate ArrayList object

aList.add("1");

aList.add("2");

aList.add("3");

aList.add("4");

aList.add("5");

 
/////////////////////////

change aList as Hashmap
////////////////
 

System.out.println("ArrayList before removal : ");

for(int i=0; i< aList.size(); i++)

System.out.println(aList.get(i));

 /////////
copy hashmap list to Iterator
////////

//get an Iterator

Iterator itr = aList.iterator();

 

//remove 2 from ArrayList using Iterator's remove method.

String strElement = "";

while(itr.hasNext()){

 

/*
#
  Iterator's next method returns an Object so we need to cast it into
#
  appropriate class before using it.
#
  */

strElement = (String)itr.next();


/////////////
here you do your condition for remove elements
//////////
if(strElement.equals("2"))

{

/*

  Remove an element using remove() method of Iterator

  Remove method removes an element from underlying collection and

  it may throw a UnsupportedOperationException if the remove

  operation is not supported.
#
  */

itr.remove();

break;

}

 

 

 

}

 

System.out.println("ArrayList after removal : ");

for(int i=0; i< aList.size(); i++)

System.out.println(aList.get(i));

 

}

}

 

/*

after you get it correctly...

mark it as resolved...

How to I copy the HashMap to Iterator?
Sorry but i didnt reach Iterators yet, and i couldnt find how to do it.

check what i did, iam not getting the concurent exception any more but it still doesnt remove the elements.

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public class Zadacha_7 {

	/**
	 * @param args
	 */
	
		//Write a program that removes from a given
		//sequence all the numbers that present in it odd
		//number of times. Example:
		//{4, 2, 2, 5, 2, 3, 2, 3, 1, 5, 2}  {5, 3, 3, 5}
		
	public static void main(String[] args) {
		int[] array = new int[] {-1,0,3,3,3,3,3, 4, 4, 2, 3, 3, 4, 3, 2};
		HashMap<Integer, Integer> numbers = new HashMap<Integer, Integer>();
		
		for ( int i = 0; i<array.length; i++)
		{
		
			if (numbers.containsKey(array[i])) {
				int temp = numbers.get(array[i]);
				temp++;
				numbers.put(array[i], temp);
			}
			else {
				int value =1;
				numbers.put(array[i], value);
			}
		
		}
		System.out.println ("Those are the numbers + the number of occuraces " + numbers);

		for (int i : numbers.keySet()){
			
			Iterator<Integer> a = numbers.keySet().iterator();
			Integer key = a.next();
			
			System.out.println (key+"is going to be removed");
							
				if (key %2 != 0){
					a.remove();
				}
		}
	
		System.out.println (numbers);
	}

}

Finnaly i got it write with a lot of help :)

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.HashSet;
public class Zadacha_7 {

	/**
	 * @param args
	 */
	
		//Write a program that removes from a given
		//sequence all the numbers that present in it odd
		//number of times. Example:
		//{4, 2, 2, 5, 2, 3, 2, 3, 1, 5, 2}  {5, 3, 3, 5}
		
	public static void main(String[] args) {
		int[] array = new int[] {-1,0,3,3,3,3,3, 4, 4, 2, 3, 3, 4, 3, 2};
		HashMap<Integer, Integer> numbers = new HashMap<Integer, Integer>();
		
		for ( int i = 0; i<array.length; i++)
		{
		
			if (numbers.containsKey(array[i])) {
				int temp = numbers.get(array[i]);
				temp++;
				numbers.put(array[i], temp);
			}
			else {
				int value =1;
				numbers.put(array[i], value);
			}
		
		}
		System.out.println ("Those are the numbers + the number of occuraces " + numbers);

		for (Iterator<Map.Entry<Integer, Integer>> i= numbers.entrySet().iterator(); i.hasNext(); ) {
			  Map.Entry<Integer, Integer> entry= i.next();
			  
		   if ((entry.getValue()+2) % 2 != 0) {
			
			      i.remove();
			}
	
		}
		
		System.out.println ("Result - numbers with even occurances" + numbers);
		
	}
}

Thanks everyone

While that code works, it's nowhere near as readable as the original version. You can fix the concurrent exception by simply iterating on a copy of the hashmap, and keep the clarity of the first version. Ie:

HashMap<Integer, Integer> temp = new HashMap<Integer, Integer>(numbers);
for (int i : temp.keySet()) {
	if (numbers.get(i) % 2 != 0) {
		numbers.remove(i);
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.