我试图从JAVA中的MultiMap(org.apache.commons.collections.MultiMap)中删除单个值

时间:2015-05-27 14:25:27

标签: java collections graph hashmap

我正在尝试从Java中删除MultiMap(org.apache.commons.collections.MultiMap)中的单个值。问题是它包含两个值。当我删除一个,另一个也被删除。

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);

2 个答案:

答案 0 :(得分:1)

我刚刚使用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"));
}

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

因此确实没有删除该值。

答案 1 :(得分:0)

请试试这个

MultiValueMap SuccessorsArray = new MultiValueMap(); 

而不是

MultiMap SuccessorsArray = new MultiValueMap();

我测试了它,它的工作原理3.2.1 commons-collection

顺便说一句,您不应将地图SuccessorsArray命名为successorsArray