简体   繁体   English

我正在尝试从JAVA中的MultiMap(org.apache.commons.collections.MultiMap)中删除一个值

[英]I am trying to remove a single value from a MultiMap(org.apache.commons.collections.MultiMap) in JAVA

I am trying to remove a single value from a MultiMap (org.apache.commons.collections.MultiMap) in Java. 我正在尝试从Java中的MultiMap(org.apache.commons.collections.MultiMap)中删除一个值。 The issue is it contains two values. 问题是它包含两个值。 When I remove one, the other gets removed as well. 当我删除一个时,另一个也会被删除。

class MappedValue
{
  public MappedValue(String id , boolean touched) {
    identifier = id;
    this.touched=touched;
  }

  private String  identifier;  
  private boolean touched; 
}   

MultiMap SuccessorsArray = new MultiValueMap();   

MappedValue mv = new MappedValue("1", false);
MappedValue mv2 = new MappedValue("2", true);

SuccessorsArray.put("key1", mv );         
SuccessorsArray.put("key1", mv2 );      
//Below is the problem as both values in the get removed instead of 1(mv).
SuccessorsArray.remove("key1", mv);

I've just tested with 3.2.1 version and 我刚刚测试了3.2.1版本,

public static void main(String[] args) {
    class MappedValue {
        public MappedValue(String id, boolean touched) {
            identifier = id;
            this.touched = touched;
        }
        private String identifier;
        private boolean touched;
        @Override
        public String toString() {
            return "MappedValue [identifier=" + identifier + ", touched=" + touched + "]";
        }
    }

    MultiMap multiMap = new MultiValueMap();
    MappedValue mv = new MappedValue("1", false);
    MappedValue mv2 = new MappedValue("2", true);
    multiMap.put("key1", mv);
    multiMap.put("key1", mv2);
    //Below is the problem as both values in the get removed instead of 1(mv).
    multiMap.remove("key1", mv);
    System.out.println(multiMap.get("key1"));
}

returns [MappedValue [identifier=2, touched=true]] 返回[MappedValue [identifier=2, touched=true]]

so the value is indeed not removed. 因此确实不会删除该值。

Please try this 请尝试这个

MultiValueMap SuccessorsArray = new MultiValueMap(); 

instead of 代替

MultiMap SuccessorsArray = new MultiValueMap();

I tested it, it works, with version 3.2.1 of commons-collection . 我测试了它,并与commons-collection 3.2.1版本一起使用。

By the way, you should not name your Map SuccessorsArray but successorsArray . 顺便说一句,您不应该为Map SuccessorsArray命名,而要为successorsArray命名。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM