英文:
How to remove an element from sorted ConcurrentNavigableMap with custom Comparator
问题
问题出在移除元素的时候。我尝试在未排序的ConcurrentNavigableMap
上进行,元素被成功移除了。但是在排序的情况下却无法移除。有人知道问题出在哪吗?
private ConcurrentNavigableMap<String, Post> registeredPost = new ConcurrentSkipListMap<String, Post>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int i1 = Integer.parseInt(o1);
int i2 = Integer.parseInt(o2);
if (i1 >= i2) {
return -1;
} else {
return 1;
}
};
});
public int deletePost(String postId) {
for (Iterator<Entry<String, Post>> it = registeredPost.entrySet().iterator(); it.hasNext();) {
Entry<String, Post> e = it.next();
String temp = e.getKey();
if (temp.compareTo(postId) == 0) {
registeredPost.remove(e.getKey());
}
return 0;
}
return 1;
}
英文:
The problem is when it's time to remove an element. I tried on unsorted ConcurrentNavigableMap
and the element was successfully removed. But on the sorted one cannot be removed. Does anyone have any idea what the problem?
private ConcurrentNavigableMap<String, Post> registeredPost=new ConcurrentSkipListMap<String,Post>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int i1=Integer.parseInt(o1);
int i2=Integer.parseInt(o2);
if(i1 >= i2){
return -1;
}else{
return 1;
}
};
});
public int deletePost(String postId) {
for(Iterator<Entry<String, Post>> it= registeredPost.entrySet().iterator();it.hasNext();) {
Entry<String,Post> e=it.next();
String temp=e.getKey();
if(temp.compareTo(postId)==0) {
registeredPost.remove(e.getKey());
}
return 0;
}
return 1;
}
答案1
得分: 3
一些问题:
键的相等性由比较器确定,您的比较器未考虑相等的元素。如果 i1 == i2
,您必须返回 0
- 或者使用 Comparator
的辅助方法:Comparator.comparingInt(Integer::parseInt)
。如果您未指定比较器,将使用 String.compareTo
方法,它会正确处理这一点。
您应该使用 it.remove()
方法删除元素 - 否则将会出现 ConcurrentModificationException
。
如已指出,返回语句的位置不正确。
英文:
Some issues:
Equality of keys is determined by the comparator, your Comparator doesn't take equal elements into account. You must return 0
if i1 == i2
- or use the Comparator
helper methods: Comparator.comparingInt(Integer::parseInt)
. If you do not specify a comparator, the String.compareTo
method is used, which handles it correctly.
You should remove Elements with it.remove()
method - otherwise you will get a ConcurrentModificationException
.
The return statement is in the wrong place, as it has been pointed out.
答案2
得分: 0
似乎您在错误的位置添加了return 0
,以至于它只会迭代一次然后停止。
public int deletePost(String postId) {
for (Iterator<Entry<String, Post>> it = registeredPost.entrySet().iterator(); it.hasNext();) {
Entry<String, Post> e = it.next();
String temp = e.getKey();
if (temp.compareTo(postId) == 0) {
registeredPost.remove(e.getKey());
return 0;//在这里添加了返回语句?
}
}
return 1;
}
英文:
It seems you have add return 0
in the wrong place so that it will only iterate once and stop
public int deletePost(String postId) {
for(Iterator<Entry<String, Post>> it= registeredPost.entrySet().iterator();it.hasNext();) {
Entry<String,Post> e=it.next();
String temp=e.getKey();
if(temp.compareTo(postId)==0) {
registeredPost.remove(e.getKey());
return 0;//add return here?
}
}
return 1;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论