我正在尝试从 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);
最佳答案
我刚刚测试了 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]]
所以该值确实没有被删除。
关于java - 我正在尝试从 JAVA 中的 MultiMap(org.apache.commons.collections.MultiMap) 中删除单个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30485387/