java


Delete entry from HashMap [duplicate]


This question already has an answer here:
Iterate through a HashMap [duplicate]
7 answers
I have a HashMap:
public static Map<String, Set<String>> adjMap = new HashMap<String, Set<String>>();
adjMap.put(title, new HashSet<String>());
adjMap.get(title).add(cutTitle(graphLink));
Now I want do delete all entries from the values (HashSet), which does not contains as a key.
Here is my code so far:
for(String s: adjMap.keySet()){
for(Set<String> s1: adjMap.values()){
for(String s2: s1){
if(!s.contains(s2)){
s1.remove(s2);
}
}
}
}
But I get an exception:
Exception in thread "main" java.util.ConcurrentModificationException
Iterate your Map
Iterator it = adjMap.entrySet().iterator();
while (it.hasNext())
{
Entry item = it.next();
map.remove(item.getKey());
}
I think for-each loops are not allowed to alter the iterated object. To remove entries you should use an iterator.
Compare to map-Iteration for an example.
You can use ConcurrentHashMap instead, or create a copy of your HashMap and make any changes to the copy.

Related Links

How to convert a String-represented ByteBuffer into a byte array in Java
how to check if my cpanel server has tomcat installed on it
jnetpcap library unable to build on ubuntu 12.04
How to convert an instance of Project to MavenProject?
com.hp.hpl.jena.rdf.model not exist when try to compile with javac
Can't load resource from jar
My app start automatically after I cancel it
Where the Local object Will be Stored of String in java
Android Progressive Stream Audio File received in HTTP response Progressive
How to set Vaadin 6 'Select' box select value
how to find client system mac address in web application
I get java code 13 error when i try to run eclipse
where to use ArrayList and where to use simple array specially for integer
hibernate console configuration cant find jdbc driver in database connection
Java API for calculating percent [closed]
How could I apply custom LifecycleStrategySupport to camel context

Categories

HOME
google-chrome-extension
cloud
testng
kde
react-virtualized
include
dot
webstorm
bookshelf.js
filtering
retrofit
onelogin
adfs
row
numeral.js
correlation
foselasticabundle
django-admin
google-static-maps
viewport
plunker
custom-wordpress-pages
css-animations
excel-2007
devops
autosys
android-widget
fish
spark-jobserver
intel-pin
nhapi
fluentvalidation
minitab
strncpy
libssl
google-cloud-nl
frame
directx-10
xquery-3.0
hybridauth
overlap
espeak
code-search-engine
webix-treetable
unobtrusive-validation
ansible-playbook
eclipse-scout
angular2-meteor
flickr-api
dotcover
smartcontracts
hendrix
alphabet
brightcove
directory-structure
pg-dump
boost-preprocessor
spring-android
paypal-express
autorest
hill-climbing
essence
fadeout
photobucket
fouc
pundit
emailrelay
map-projections
ibaction
dia
oxwall
tt-news
java.util.concurrent
nsbutton
wordpress-theme-customize
websocket4net
marmalade
kgdb
message-driven-bean
intentservice
apc
tws
gridcontrol
sharp-repository
tinn-r
typoscript2
simba
sublist
libc++
pydot
appender
actionview
cxxtest
rescale
firefox4
webkit.net
simpletest
misv

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App