Java removeAll似乎影响了它的论点

Java removeAll似乎影响了它的论点,java,collections,removeall,Java,Collections,Removeall,我写了一个通用的分区类,分区是一个集合到不相交子集的划分,称为部分。在内部,这是一个映射和一个映射,其中整数是零件的标签。例如,partition.getLabelT t给出了t所在部分的标签,而partition.moveT t,Integer label在内部将t移动到由label标记的分区,它会更新两个映射 但我将对象集合移动到新零件的方法不起作用。似乎Set.removeAll正在影响其参数。我认为这个问题有点像ConcurrentModificationException,但我无法解决

我写了一个通用的分区类,分区是一个集合到不相交子集的划分,称为部分。在内部,这是一个映射和一个映射,其中整数是零件的标签。例如,partition.getLabelT t给出了t所在部分的标签,而partition.moveT t,Integer label在内部将t移动到由label标记的分区,它会更新两个映射

但我将对象集合移动到新零件的方法不起作用。似乎Set.removeAll正在影响其参数。我认为这个问题有点像ConcurrentModificationException,但我无法解决它。抱歉,代码太长了,但我已经标记了问题的位置,底部的输出应该清楚地表明问题所在-最后分区处于非法状态

    import java.util.*;

    public class Partition<T> {
  private Map<T,Integer> objToLabel = new HashMap<T,Integer>();
     private Map<Integer,Set<T>> labelToObjs = 
       new HashMap<Integer,Set<T>>();
     private List<Integer> unusedLabels;
     private int size;  // = number of elements

     public Partition(Collection<T> objects) {
      size = objects.size();
      unusedLabels = new ArrayList<Integer>();
      for (int i = 1; i < size; i++)
       unusedLabels.add(i);
      // Put all the objects in part 0. 
      Set<T> part = new HashSet<T>(objects);
      for (T t : objects)
       objToLabel.put(t,0);
      labelToObjs.put(0,part);
     }

     public Integer getLabel(T t) {
      return objToLabel.get(t);
     }
     public Set<T> getPart(Integer label) {
      return labelToObjs.get(label);
     }
     public Set<T> getPart(T t) {
      return getPart(getLabel(t));
     }

     public Integer newPart(T t) { 
      // Move t to a new part. 
      Integer newLabel = unusedLabels.remove(0); 
      labelToObjs.put(newLabel,new HashSet<T>()); 
      move(t, newLabel); 
      return newLabel; 
     } 
     public Integer newPart(Collection<T> things) {
      // Move things to a new part. (This assumes that 
      // they are all in the same part to start with.)
      Integer newLabel = unusedLabels.remove(0);
      labelToObjs.put(newLabel,new HashSet<T>());
      moveAll(things, newLabel); 
      return newLabel; 
     }
     public void move(T t, Integer label) {
      // Move t to the part "label". 
      Integer oldLabel = getLabel(t);
      getPart(oldLabel).remove(t);
      if (getPart(oldLabel).isEmpty())  // if the old part is 
       labelToObjs.remove(oldLabel);  // empty, remove it
      getPart(label).add(t);
      objToLabel.put(t,label);
     }
     public void moveAll(Collection<T> things, Integer label) {
      // Move all the things from their current part to label. 
      // (This assumes all the things are in the same part.)
      if (things.size()==0)  return;

      T arbitraryThing = new ArrayList<T>(things).get(0);
      Set<T> oldPart = getPart(arbitraryThing);

   // THIS IS WHERE IT SEEMS TO GO WRONG //////////////////////////
      System.out.println(" oldPart = " + oldPart);
      System.out.println(" things = " + things);
      System.out.println("Now doing oldPart.removeAll(things) ...");
      oldPart.removeAll(things);
      System.out.println(" oldPart = " + oldPart);
      System.out.println(" things = " + things);

      if (oldPart.isEmpty())
       labelToObjs.remove(objToLabel.get(arbitraryThing));

      for (T t : things)
       objToLabel.put(t,label);
      getPart(label).addAll(things);
     }

     public String toString() {
      StringBuilder result = new StringBuilder();
      result.append("\nPARTITION OF " + size + " ELEMENTS INTO " + 
        labelToObjs.size() + " PART");
      result.append((labelToObjs.size()==1 ? "" : "S") + "\n");
      for (Map.Entry<Integer,Set<T>> mapEntry : 
        labelToObjs.entrySet()) {
       result.append("PART " + mapEntry.getKey() + ": ");
       result.append(mapEntry.getValue() + "\n");
      }
      return result.toString();
     }

     public static void main(String[] args) {
      List<String> strings = 
        Arrays.asList("zero one two three".split(" "));

      Partition<String> p = new Partition<String>(strings);
      p.newPart(strings.get(3));  // move "three" to a new part
      System.out.println(p);

      System.out.println("Now moving all of three's part to the " + 
        "same part as zero.\n");
      Collection<String> oldPart = p.getPart(strings.get(3));
      //oldPart = Arrays.asList(new String[]{"three"});  // works fine!
      p.moveAll(oldPart, p.getLabel(strings.get(0)));
      System.out.println(p);
     }
    }

/* OUTPUT
PARTITION OF 4 ELEMENTS INTO 2 PARTS
PART 0: [two, one, zero]
PART 1: [three]

Now moving all of three's part to the same part as zero.

 oldPart = [three]
 things = [three]
Now doing oldPart.removeAll(things) ...
 oldPart = []
 things = []

PARTITION OF 4 ELEMENTS INTO 1 PART
PART 0: [two, one, zero]
*/

使用我的调试器,我在removeAll之前放置了一个断点,我可以看到,因为我怀疑oldPart和things是同一个集合,所以删除所有元素会清除该集合。

您的代码非常混乱,但据我所知,oldPart和things实际上是同一个对象。Set.removeAll当然不会影响其参数,除非它与在以下对象上调用的对象相同:

public boolean removeAll(Collection<?> c) {
    boolean modified = false;

    if (size() > c.size()) {
        for (Iterator<?> i = c.iterator(); i.hasNext(); )
            modified |= remove(i.next());
    } else {
        for (Iterator<?> i = iterator(); i.hasNext(); ) {
            if (c.contains(i.next())) {
                i.remove();
                modified = true;
            }
        }
    }
    return modified;
}
这一行创建但立即丢弃一个对象的副本。因此,我建议将其替换为:

ArrayList<T> thingsToRemove = new ArrayList<T>(things)
T arbitraryThing = thingsToRemove.get(0);

在方法的其余部分,将所有对thingsToRemove的引用替换为thingsToRemove。

谢谢。他们是同一个物体,这就可以解释了。有人能建议我需要做什么来修复它吗?我宁愿不要复制东西。也很抱歉,代码很混乱。我应该多加评论,还是仅仅是构思不好或写得不好??我无法解决如何修复缩进。@Edd不要说抱歉,这是您的代码,您可以随意编写最让我困惑的是,每件事都是一个部分、标签或事物,所有太笼统的词都很少透露出它们的用途。我会用可能的解决方案更新我的答案。谢谢。令人惊叹的我想我用这些词是因为它是一个抽象的数学对象。不过,有一个词可能比label更好。谢谢。如果有人能很容易地看到我需要做什么来修复这个类,我将非常感激。我也会自己解决的。
ArrayList<T> thingsToRemove = new ArrayList<T>(things)
T arbitraryThing = thingsToRemove.get(0);