我已使用该方法删除了地图,"remove(key)"
但仅删除了该值。当我调用该keySet()
方法时。钥匙还在!!下面的程序,我想找到两个组(HashMap 中的 2 个映射)具有最短距离。然后合并它们,这意味着我需要删除这两个组并将合并的组放到地图上。
/* 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 --;
}