Results 1 to 13 of 13
Thread: HashMap problem
- 05-16-2013, 02:22 AM #1
Member
- Join Date
- May 2013
- Posts
- 43
- Rep Power
- 0
HashMap problem
Hello everyone,
I made this code:
Java Code:public void removerInvasor_LIVE() { int count = 0; List<MovingImage> temp = new ArrayList<MovingImage>(); for (MovingImage invaders : Map_Inv.keySet()) { if (Map_Inv.get(invaders).ja_morreu()) { Map_Inv.remove(invaders); temp.add(invaders); index.getDisplayer().remove(temp.get(count)); } } count++; index.getDisplayer().refresh(); }
So, I made another code with an Iterator, like this:
Java Code:public void removerInvasor_LIVE_teste5(){ Iterator<MovingImage> it = Map_Inv.keySet().iterator(); while (it.hasNext()){ it.next(); if (Map_Inv.get(it).ja_morreu()) it.remove(); } }
Can I solve this?
- 05-16-2013, 02:31 AM #2
Senior Member
- Join Date
- Nov 2012
- Posts
- 257
- Rep Power
- 9
Re: HashMap problem
java.util.ConcurrentModificationException i think is caused when you try to modify something thats in use, in this case you try to remove something whilst its still being used:
i order to stop this, create a local variable of the same Type and assign it to the current one being iterated through:
e.g:
MovingImage temp = invaders;
remove(temp);
- 05-16-2013, 02:59 AM #3
Member
- Join Date
- May 2013
- Posts
- 43
- Rep Power
- 0
Re: HashMap problem
Like this
Java Code:public void removerInvasor_LIVE() { int count = 0; List<MovingImage> temp = new ArrayList<MovingImage>(); for (MovingImage invaders : Map_Inv.keySet()) { MovingImage temp = invaders; if (Map_Inv.get(invaders).ja_morreu()) { Map_Inv.remove(temp); temp.add(invaders); index.getDisplayer().remove(temp)); } } count++; index.getDisplayer().refresh(); }
- 05-16-2013, 03:10 AM #4
Senior Member
- Join Date
- Nov 2012
- Posts
- 257
- Rep Power
- 9
Re: HashMap problem
try it :) i had that exception and that's how i solved it, it wouldn't allow me to remove an element whilst evaluating it
- 05-16-2013, 10:57 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: HashMap problem
Java Code:public void removerInvasor_LIVE_teste5(){ Iterator<MovingImage> it = Map_Inv.keySet().iterator(); while (it.hasNext()){ it.next(); if (Map_Inv.get(it).ja_morreu()) it.remove(); } }
The problem lies not in using the Iterator, but in these lines:
Java Code:it.next(); if (Map_Inv.get(it).ja_morreu())
As it is, you are calling get while passing in the Iterator (as you suspect).
So:
Java Code:MovingImage invaders = it.next();
Please do not ask for code as refusal often offends.
** This space for rent **
- 05-16-2013, 02:30 PM #6
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: HashMap problem
Last edited by jim829; 05-16-2013 at 03:18 PM.
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 05-16-2013, 02:48 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: HashMap problem
It's the correct way, ie using the Iterator.remove() method.
That's why it's there.
The error they were getting (the NPE) was simply down to using the iterator itself as the key to the Map, rather than the value returned by next().Please do not ask for code as refusal often offends.
** This space for rent **
- 05-16-2013, 06:56 PM #8
Member
- Join Date
- May 2013
- Posts
- 43
- Rep Power
- 0
Re: HashMap problem
Hi Tools, I made it like this:
Java Code:public void removerInvasor_LIVE_teste5(){ Iterator<MovingImage> it = Map_Inv.keySet().iterator(); while (it.hasNext()){ MovingImage invaders = it.next(); if (Map_Inv.get(invaders).ja_morreu()) it.remove(); } }
- 05-16-2013, 07:10 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: HashMap problem
Post the full exception including the stack trace, because the following works:
Java Code:public static void main(String args[]) { HashMap<String, String> map = new HashMap<>(); map.put("test", "4test"); map.put("test1", "1test"); map.put("test2", "test"); map.put("test3", "3test"); Iterator<String> it = map.keySet().iterator(); while (it.hasNext()) { String invaders = it.next(); if (map.get(invaders).equals("test")) { it.remove(); } } for (Entry<String, String> entry : map.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue()); } }
Please do not ask for code as refusal often offends.
** This space for rent **
- 05-16-2013, 07:18 PM #10
Member
- Join Date
- May 2013
- Posts
- 43
- Rep Power
- 0
Re: HashMap problem
My MAP is this:
Java Code:private Map<MovingImage, Invaders> Map_Inv = new HashMap<MovingImage, Invaders>();
Java Code:public int getLife() { return life; } public boolean ja_morreu() { return (getLife() <= 0); }
Java Code:Exception in thread "main" java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry(Unknown Source) at java.util.HashMap$KeyIterator.next(Unknown Source) at pt.iul.poo.games.Mapa.towerShots(Mapa.java:129) at pt.iul.poo.games.Mapa.game_start(Mapa.java:179) at pt.iul.poo.games.teste_main.main(teste_main.java:27) )
- 05-16-2013, 07:25 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: HashMap problem
That stack trace does not match the code in post #8.
The method in that post is called removerInvasor_LIVE_teste5 which does not appear in there.
You need to show us the towerShots() method, which is where (in your code) this is coming from.Please do not ask for code as refusal often offends.
** This space for rent **
- 05-16-2013, 08:42 PM #12
Member
- Join Date
- May 2013
- Posts
- 43
- Rep Power
- 0
Re: HashMap problem
here is:
Java Code:if (shot != null) index.getDisplayer().remove(shot); for (SimpleImage tower : InsertedTowersImages) { for (MovingImage x : Map_Inv.keySet()) { removerInvasor_LIVE_teste5(); if (Math.abs(tower.getPosition().distance(x.getPosition())) < RANGE) { Map_Inv.get(x).ReduzirVida(); SimpleShape s = new SimpleShape(new Line2D.Double(tower .getPosition().getX() + tower.getDimension().width / 2, tower .getPosition().getY() + tower.getDimension().height / 2, x .getPosition().getX() + x.getDimension().width / 2, x.getPosition().getY() + x.getDimension().height / 2), Color.RED, 50); shot = s; index.getDisplayer().add(s); } } } }
- 05-17-2013, 10:37 AM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: HashMap problem
And can you show which line in that lot the exception is being thrown from?
You need to show us the actual code, and point out where the problem is occurring in that code.
I'm guessing it's here:
Java Code:for (MovingImage x : Map_Inv.keySet())
You cannot change the underlying data being used by an iterator except through that iterator. Doing so will result in a ConcurrentModException.Please do not ask for code as refusal often offends.
** This space for rent **
Similar Threads
-
HashMap problem
By Java Junior in forum Advanced JavaReplies: 6Last Post: 03-16-2012, 07:03 PM -
HashMap problem
By vince8864 in forum New To JavaReplies: 1Last Post: 02-13-2012, 12:51 PM -
hashmap or search problem
By flushdabuffer in forum AndroidReplies: 0Last Post: 04-09-2011, 02:55 PM -
hashmap problem
By minotaurus in forum Advanced JavaReplies: 5Last Post: 03-16-2011, 12:24 PM -
Problem with HashMap
By maz09 in forum New To JavaReplies: 2Last Post: 04-14-2010, 10:40 PM
Bookmarks