嵌套在同一组上的循环,从所述集中删除元素

时间:2015-02-11 22:53:26

标签: java set

我正在编写程序,并且在从一组集中删除集合时遇到了问题。

问题

首先,我将概述所需的功能。

给定一组存储整数值的集合,如下所示:

[[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]] 

以及表单中的约束列表:

((1, 2), (9, 10), (2, 9))

对于每个约束,我迭代集合并根据约束合并和删除集合。 例如(1,2)表示1和2应该在同一个集合中,因此合并集合。

在迭代约束之后,其余的集应该是:

[[1,2,9,10], [3], [4], [5], [6], [7], [8]]

当集合中只有一个元素时,我实现,合并和删除集合的方法没问题(至少那是我已经识别的模式)。 约束按顺序迭代,在应用最终约束之前,集合的状态如下:

[[1,2], [3], [4], [5], [6], [7], [8], [9,10]]

在应用最终约束(2,9)时,这意味着应合并包含2和9的集合,代码将生成以下输出:

[[1,2,9,10], [3], [4], [5], [6], [7], [8], [9,10]]

实施

以下示例程序演示了这一点:

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class SetTest {

public static void main(String[] args) {

    Set<Set<Integer>> originalSet = new HashSet<Set<Integer>>();
    Set<Integer> tempSet = new HashSet<Integer>();

    //Populate original set with sets of integers 1-10
    for (int i = 1; i <= 10; i++) {
        tempSet.add(i);
        originalSet.add(new HashSet<Integer>(tempSet));
        tempSet.clear();
    }

    System.out.println("Original Sets: ");
    for (Set<Integer> set : originalSet) {
        System.out.println(set.toString());
    }

    //Generate constraints
    List<Pair> pairSet = new LinkedList<Pair>();
    pairSet.add(new Pair(1, 2));
    pairSet.add(new Pair(9, 10));
    pairSet.add(new Pair(2, 9));

    System.out.println("Set of Pairs: ");
    for (Pair p : pairSet) {
        System.out.println(p.toString());
    }

    System.out.println("Merge and Remove Process");
    //For each pair
    for (Pair p : pairSet) {
        System.out.println("Iteration::Merge sets containing " + p.toString());
        boolean merged = false;
        //Iterate over each set in the original set
        for (Set s : originalSet) {
            //If the set contains the first element of the pair and no merge has been performed
            if (s.contains(p.getFirst()) && (merged == false)) {
                //Find the set containing the second element of the pair 
                for (Set t : originalSet) {
                    if (t.contains(p.getSecond())) {
                        //Merge t with s
                        s.addAll(t);
                        originalSet.remove(t);
                        break;
                    }
                }
                //s.add(constraint.getB());
                merged = true;
                break;
            }

            //If the set contains the second element of the pair and no merge has been performed
            if (s.contains(p.getSecond()) && (merged == false)) {
                //Find the set containing the first element of the pair 
                for (Set t : originalSet) {
                    if (t.contains(p.getFirst())) {
                        //Merge t with s
                        s.addAll(t);
                        originalSet.remove(t);
                        break;
                    }
                }
                //s.add(constraint.getB());
                merged = true;
                break;
            }
        }

        //Output the set after update
        for (Set<Integer> set : originalSet) {
            System.out.println(set.toString());
        }
    }

}

public static class Pair<F, S> {

    private F first; //first member of pair
    private S second; //second member of pair

    public Pair(F first, S second) {
        this.first = first;
        this.second = second;
    }

    public void setFirst(F first) {
        this.first = first;
    }

    public void setSecond(S second) {
        this.second = second;
    }

    public F getFirst() {
        return first;
    }

    public S getSecond() {
        return second;
    }

    @Override
    public String toString() {
        return "(" + getFirst() + ", " + getSecond() + ")";
    }
}
}

在for循环中,删除集合的功能似乎不起作用,我不知道为什么。

for (Pair p : pairSet) {
        for (Set s : originalSet) {
                for (Set t : originalSet) {
                   originalSet.remove(t);
                }
        }
}

虽然合并似乎工作正常。

程序的输出如下:

Original Sets: 
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[10]
Set of Pairs: 
(1, 2)
(9, 10)
(2, 9)
Merge and Remove Process
Iteration::Merge sets containing (1, 2)
[1, 2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[10]
Iteration::Merge sets containing (9, 10)
[1, 2]
[3]
[4]
[5]
[6]
[7]
[8]
[9, 10]
Iteration::Merge sets containing (2, 9)
[1, 2, 9, 10]
[3]
[4]
[5]
[6]
[7]
[8]
[9, 10]

最后一组[9,10]不应该存在。 感谢任何帮助,理解为什么删除功能无法实现所需的输出!

3 个答案:

答案 0 :(得分:2)

由于你有两个嵌套循环迭代originalSet,修改集合会从两个迭代器下面拉出地毯。

只需一个循环,您可以使用显式迭代器并调用iterator.remove()来删除当前元素来修复它。

由于这不会在这里工作,你必须在修改原始集时迭代集的副本,或者在构建新集时迭代原始集。

答案 1 :(得分:1)

for (Set t : originalSet) {
    if (t.contains(p.getFirst())) {
        //Merge t with s
        s.addAll(t);
        originalSet.remove(t);
        break;
    }
}

您无法以这种方式删除它,如果要从迭代中删除的集合中删除项目,则应使用迭代器。请参阅http://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html

答案 2 :(得分:1)

我会像这个伙伴一样使用迭代器:

package setproblem;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class SetProblem {

public static void main(String[] args) {

Set<Set<Integer>> originalSet = new HashSet<Set<Integer>>();
Set<Integer> tempSet = new HashSet<Integer>();

//Populate original set with sets of integers 1-10
for (int i = 1; i <= 10; i++) {
    tempSet.add(i);
    originalSet.add(new HashSet<Integer>(tempSet));
    tempSet.clear();
}

System.out.println("Original Sets: ");
for (Set<Integer> set : originalSet) {
    System.out.println(set.toString());
}

//Generate constraints
List<Pair> pairSet = new LinkedList<Pair>();
pairSet.add(new Pair(1, 2));
pairSet.add(new Pair(9, 10));
pairSet.add(new Pair(2, 9));

System.out.println("Set of Pairs: ");
for (Pair p : pairSet) {
    System.out.println(p.toString());
}

System.out.println("Merge and Remove Process");
//For each pair
for (Pair p : pairSet) {
    System.out.println("Iteration::" + p.toString());

    Set<Integer> firstElementSet = getAllElementsFromOriginalSetContaining((Integer)p.getFirst(), originalSet);
    System.out.println("FES::" + firstElementSet.toString());
    Set<Integer> secondElementSet = getAllElementsFromOriginalSetContaining((Integer)p.getSecond(), originalSet);
    System.out.println("SES::" + secondElementSet.toString());

    removeElements(firstElementSet, originalSet);
    removeElements(secondElementSet, originalSet);

    originalSet.removeAll(firstElementSet);
    originalSet.removeAll(secondElementSet);
    firstElementSet.addAll(secondElementSet);
    originalSet.add(firstElementSet);


    for(Set<Integer> s : originalSet){
        System.out.println(s.toString());
    }
}

}

private static Set<Integer> getAllElementsFromOriginalSetContaining(Integer element, Set<Set<Integer>> originalSet) {

    Set<Integer> tempSet = new HashSet<Integer>();

    for(Set<Integer> s : originalSet){
        if(s.contains(element)){
            tempSet.addAll(s);
        }
    }

    return new HashSet<Integer>(tempSet);
}

private static void removeElements(Set<Integer> elementSet, Set<Set<Integer>> originalSet) {

    Iterator originalSetIterator = originalSet.iterator();

    while(originalSetIterator.hasNext()) {
        Set<Integer> s = (HashSet<Integer>)originalSetIterator.next();

        if(s.equals(elementSet)){
             System.out.println("Found set! " + elementSet.toString());
              System.out.println("Found in originalSet! " + s.toString());
              originalSetIterator.remove();
        }
    }        
}

public static class Pair<F, S> {

private F first; //first member of pair
private S second; //second member of pair

public Pair(F first, S second) {
    this.first = first;
    this.second = second;
}

public void setFirst(F first) {
    this.first = first;
}

public void setSecond(S second) {
    this.second = second;
}

public F getFirst() {
    return first;
}

public S getSecond() {
    return second;
}

@Override
public String toString() {
    return "(" + getFirst() + ", " + getSecond() + ")";
}
}
}