Remove method in HashMap java does not work
I have removed the map with the method "remove(key)" but just the value
was deleted. When I call the keySet() method. The key still there!!. The
below program, I want to find two groups (2 map in the HashMap) have
shortest distance. Then merge them, that mean I need to remove these two
groups and put the merged group to maps.
/* I have already initiate the HashMap groups*/
ArrayList<String> listGroupNames ;
while (currentNumberOfGroup > 1000){
System.out.println("Groups" + currentNumberOfGroup);
listGroupNames = new ArrayList<String>(groups.keySet());
/* Name of two groups will be merged*/
String candidate1 = "";
String candidate2 = "";
double maxSimilarity = 0;
/* finding two groups which have shortest distance*/
for(String event1 : listGroupNames){
for(String event2 : listGroupNames){
if(!event1.equalsIgnoreCase(event2)){
Group group1 = groups.get(event1);
Group otherGrp = groups.get(event2);
double similarity = group1.getMaxSimilarityWith(otherGrp,
simpledEvent);
if(similarity > maxSimilarity){
maxSimilarity = similarity;
candidate1 = event1;
candidate2 = event2;
}
}
}
}
/* 2 groups have shortest distance were found already*/
/* Merge 2 groups then replace these two groups by merged group*/
Group groupCandidate1 = groups.get(candidate1) ;
Group groupCandidate2 = groups.get(candidate2) ;
Group mergerdGroup = groupCandidate1.mergeToOtherGroup(groupCandidate2);
/*remove 2 found groups*/
groups.remove(candidate2);
groups.remove(candidate1);
groups.put(mergerdGroup.name, mergerdGroup);
listGroupNames.clear();
currentNumberOfGroup --;
}
No comments:
Post a Comment