Walker
Walker

Reputation: 341

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

Upvotes: 1

Views: 2542

Answers (2)

romfret
romfret

Reputation: 391

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.

By the way, you should not name your Map SuccessorsArray but successorsArray.

Upvotes: 0

Xavier Delamotte
Xavier Delamotte

Reputation: 3599

I've just tested with 3.2.1 version and

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]]

so the value is indeed not removed.

Upvotes: 1

Related Questions