Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将重复元组列表转换为唯一元组列表?_Java_Arrays_Unique - Fatal编程技术网

Java 如何将重复元组列表转换为唯一元组列表?

Java 如何将重复元组列表转换为唯一元组列表?,java,arrays,unique,Java,Arrays,Unique,我正在编写一段代码,其中包含一个包含重复元组的列表,我希望在不使用HASHSET或SET的情况下从该列表中删除重复元组。我只允许使用数组、ArrayList和迭代器 下面的代码将溢出/无限循环。我理解为什么它会无限循环,因为我添加了元素,并且大小也增加了 元组类: public class Tuple { private int key; private String value; public Tuple(int keyP, String valueP) {

我正在编写一段代码,其中包含一个包含重复元组的列表,我希望在不使用HASHSET或SET的情况下从该列表中删除重复元组。我只允许使用数组、ArrayList和迭代器

下面的代码将溢出/无限循环。我理解为什么它会无限循环,因为我添加了元素,并且大小也增加了

元组类:

public class Tuple {
    private int key;
    private String value;

    public Tuple(int keyP, String valueP) {
        key = keyP;
        value = valueP;
    }

    public int getKey() {
        return key;
    }  

    public String getValue() {
        return value;
    }

    public boolean equals(Tuple t) {
        if (this.key == t.getKey() && this.value.equals(t.getValue())) {
            return true;
        }
        else return false;
    }
}

ArrayList < Tuple > union= new ArrayList<Tuple>();
ArrayList < Tuple > unique= new ArrayList<Tuple>();

Union: [Tuple < 15, a >, Tuple < 17, a >, Tuple < 15, a >, Tuple<79, b>] 

unique.add(union.get(0));

for (int k = 1; k < union.size(); k++) {
    Tuple test= union.get(k);
    for (int j= 0; j < unique.size();j++) {
        if (!test.equals(unique.get(j))) {
            System.out.println(union.get(k));
            unique.add(union.get(k));
        }
    }
}
公共类元组{
私钥;
私有字符串值;
公共元组(int-keyP,String-valueP){
key=keyP;
value=valueP;
}
public int getKey(){
返回键;
}  
公共字符串getValue(){
返回值;
}
公共布尔等于(元组t){
if(this.key==t.getKey()&&this.value.equals(t.getValue())){
返回true;
}
否则返回false;
}
}
ArrayListunion=newArrayList();
ArrayListunique=新的ArrayList();
联合:[元组<15,a>,元组<17,a>,元组<15,a>,元组]
unique.add(union.get(0));
for(int k=1;k
应该是独一无二的

[Tuple< 15, a >, Tuple< 17, a >, Tuple< 79, b >] 
[元组<15,a>,元组<17,a>,元组<79,b>]

我完全被困了一天多。请帮帮我

如果没有数据,我们可以承认编译器对元组的空列表执行操作:

-> union.stream().distinct()
|  Expression value is: java.util.stream.DistinctOps$1@4493d195
|    assigned to temporary variable $58 of type Stream<Tuple>
->union.stream().distinct()
|表达式值为:java.util.stream.DistinctOps$1@4493d195
|分配给Stream类型的临时变量$58
覆盖元组的键、值变量上的
equals()
hashCode()
方法。然后您可以直接使用列表的
contains()
方法,而不是每次都通过
unique
List循环

文件:Tuple.java

public class Tuple {
    private int key;
    private String value;

    public Tuple(int keyP, String valueP) {
        key = keyP;
        value = valueP;
    }

    public int getKey() {
        return key;
    }

    public String getValue() {
        return value;
    }

    @Override
    public String toString() {
        return "Tuple [key=" + key + ", value=" + value + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + key;
        result = prime * result + ((value == null) ? 0 : value.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Tuple other = (Tuple) obj;
        if (key != other.key)
            return false;
        if (value == null) {
            if (other.value != null)
                return false;
        } else if (!value.equals(other.value))
            return false;
        return true;
    }
}
文件:TupleMain.java

public class TupleMain {
    public static void main(String[] args) {
        List<Tuple> union = Arrays.asList(new Tuple(15, "a"), new Tuple(17, "a"), new Tuple(15, "a"),
                new Tuple(79, "b"));
        List<Tuple> unique = new ArrayList<Tuple>();

        unique.add(union.get(0));
        for (int k = 1; k < union.size(); k++) {
            Tuple test = union.get(k);
            if (!unique.contains(test)) {
                unique.add(test);
            }
        }

        System.out.println(unique);

        // The Java 8 way
        union.stream().distinct().forEach(System.out::println);
    }
}
公共类TupleMain{
公共静态void main(字符串[]args){
List union=Arrays.asList(新元组(15,“a”)、新元组(17,“a”)、新元组(15,“a”),
新元组(79,“b”);
List unique=new ArrayList();
unique.add(union.get(0));
for(int k=1;k
可能有更好的选择,但使用您的需求,您可以在循环中添加一个标志

    ArrayList<Tuple> union= new ArrayList<Tuple>();
    ArrayList<Tuple> unique= new ArrayList<Tuple>();

    union.add(new Tuple(15, "a"));
    union.add(new Tuple(17, "a"));
    union.add(new Tuple(15, "a"));
    union.add(new Tuple(79, "b"));

    unique.add(union.get(0));

    for(int k=1;k<union.size();k++) {

        Tuple test=union.get(k);
        Boolean exists = false;
        for(int j=0;j<unique.size();j++) {

            if(test.equals(unique.get(j))) {
                exists = true; 
                break;
            }
        }

        if(!exists) {
            System.out.println(union.get(k));
            unique.add(union.get(k)); 
        }

    } 
ArrayList union=new ArrayList();
ArrayList unique=新的ArrayList();
union.add(新元组(15,“a”);
union.add(新元组(17,“a”);
union.add(新元组(15,“a”);
union.add(新元组(79,“b”);
unique.add(union.get(0));

对于(intk=1;kMuch更好)行
并集:[Tuple<15,a>,Tuple<17,a>,Tuple<15,a>,Tuple<79,b>]并集:[Tuple<15,a>,Tuple<17,a>,Tuple<15,a>,Tuple<79,b>]
不编译。我真的找到了答案,所以我只想发布答案。顺便说一句:在生成一个在SE上发布我的问题的过程中,我通常会自己解决90%的问题。这是对想法的排序,找到核心,并与想象中的观众交谈,这就是mcves的不明显的好处。t更明显的一点是,100个读者(每个读者设置一个而不是一个)并不经济。提示:在重写方法时,应该使用@Override注释,例如在equals方法上。这将导致编译器错误,告诉您签名应该是
equals(Object)
,而不是
equals(Tupel)
。永远不要覆盖像这样的“标准”方法!