如何使用自定义比较器从已排序的ConcurrentNavigableMap中移除一个元素。

huangapple go评论89阅读模式
英文:

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&lt;String, Post&gt; registeredPost=new ConcurrentSkipListMap&lt;String,Post&gt;(new Comparator&lt;String&gt;() {
			@Override
			public int compare(String o1, String o2) {
				int i1=Integer.parseInt(o1);
				int i2=Integer.parseInt(o2);
				if(i1 &gt;= i2){
					return -1;
				}else{
					return 1;
				}
			};
		});

public int deletePost(String postId) {
for(Iterator&lt;Entry&lt;String, Post&gt;&gt; it= registeredPost.entrySet().iterator();it.hasNext();) {
			Entry&lt;String,Post&gt; 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&lt;Entry&lt;String, Post&gt;&gt; it= registeredPost.entrySet().iterator();it.hasNext();) {
            Entry&lt;String,Post&gt; e=it.next();
            String temp=e.getKey();
            
            if(temp.compareTo(postId)==0) {
                registeredPost.remove(e.getKey());
                return 0;//add return here?
            }
        }
 return 1;
}

huangapple
  • 本文由 发表于 2020年10月3日 23:07:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64185668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定