简体   繁体   English

如何使用自定义比较器从排序的 ConcurrentNavigableMap 中删除元素

[英]How to remove an element from sorted ConcurrentNavigableMap with custom Comparator

The problem is when it's time to remove an element.问题是何时需要删除元素。 I tried on unsorted ConcurrentNavigableMap and the element was successfully removed.我尝试了未排序的ConcurrentNavigableMap并成功删除了该元素。 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;
}

Some issues:一些问题:

Equality of keys is determined by the comparator, your Comparator doesn't take equal elements into account.键的相等性由比较器决定,您的 Comparator 不考虑相等的元素。 You must return 0 if i1 == i2 - or use the Comparator helper methods: Comparator.comparingInt(Integer::parseInt) .如果i1 == i2您必须返回0 - 或使用Comparator辅助方法: Comparator.comparingInt(Integer::parseInt) If you do not specify a comparator, the String.compareTo method is used, which handles it correctly.如果未指定比较器,则使用String.compareTo方法,该方法可以正确处理它。

You should remove Elements with it.remove() method - otherwise you will get a ConcurrentModificationException .您应该使用it.remove()方法删除 Elements - 否则您将收到ConcurrentModificationException

The return statement is in the wrong place, as it has been pointed out.正如已经指出的那样,return 语句在错误的地方。

It seems you have add return 0 in the wrong place so that it will only iterate once and stop似乎您在错误的地方添加了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;//add return here?
            }
        }
 return 1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将Java ConcurrentNavigableMap与比较器而不是TreeMap一起使用? - How can I use a Java ConcurrentNavigableMap with comparator instead of a TreeMap? 如何在 java 8 Stream 中定义自定义排序比较器以比较键和值 - How to define custom sorted comparator in java 8 Stream to compare on the key and the value 如何应用比较器从按人口排序的列表中获取前 5 个城市? - How to apply comparator to get the TOP 5 cities from a List sorted by population? 如何通过某个比较器对 Java TreeMap 进行排序? - How to make a Java TreeMap be sorted by some comparator? 从列表和比较器中删除Java - remove from list and comparator java 我实现了一个自定义比较器来对我的ArrayList进行排序,但是listview仍然没有排序 - i implemented a custom comparator to sort my ArrayList, but listview is still not sorted 如果使用自定义Comparator创建,则为SortedMap生成的流的流特征可能不会被分类 - Stream characteristics for the streams generated for SortedMap may not be SORTED if created with custom Comparator 如何在java中编写自定义比较器? - How to write custom comparator in java? 为什么不使用自定义比较器从 TreeSet 中删除更大的项目集? - Why doesn't removing from a TreeSet with a custom comparator remove a larger set of items? 使用比较器对流排序的方法 - Stream sorted method with comparator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM