用于从 ArrayList#1 中删除出现在另一个 ArrayList#2 中并且在 ArrayList#1 中不唯一的行的代码

发布于 2024-12-25 18:26:07 字数 2706 浏览 3 评论 0原文

这篇文章是 的延续我之前的帖子。现在我有一个想要编译的代码。唯一的区别是,现在我使用自己的类 List 的列表,而不是 List。特别要注意 Row 中的 hashCode,因为它提供了编译错误。

    public class Row {
          private String key;
          private Integer[] values;

          public Row(String k,Integer[] v) {
              this.key = k;
              this.values = v;
          }

          public String getKey() {
              return this.key;
          }

          public Integer[] getValues() {
              return this.values;
          }

          @Override
          public boolean equals(Object obj) {
              if(this == obj)
                  return true;
              if((obj == null) || (obj.getClass() != this.getClass()))
                  return false;
              // object must be Row at this point
              Row row = (Row)obj;
                  return ((key == row.key) && (values == row.values));
          }

          @Override
          public int hashCode () { // HERE I HAVE A PROBLEM. DON'T KNOW HOW TO IMPLEMENT IT
              return this.key;
          }

    }

public class Test {

    public static void main(String[] args) {
        List<Row> allRows = new ArrayList<Row>();
        allRows.add(new Row("0",new Integer[]{1,2,3}));
        allRows.add(new Row("0",new Integer[]{1,2,2}));
        allRows.add(new Row("1",new Integer[]{1,2,3}));
        allRows.add(new Row("2",new Integer[]{1,1,1}));
        allRows.add(new Row("2",new Integer[]{1,1,1}));

List<Row> selectedRows = new ArrayList<Row>();
            selectedRows.add(new Row("0",new Integer[]{1,2,3}));
            selectedRows.add(new Row("2",new Integer[]{1,1,1}));

    System.out.println(allRows);
    System.out.println(selectedRows);
        List<Row> refreshedRows = refreshRows(allRows,selectedRows);
        System.out.println(refreshedRows);

    }

    private static List<Row> refreshRows(List<Row> allRows,List<Row> selectedRows) {
        Set<Row> set1 = new HashSet<Row>();
        Iterator<Row> it = allRows.iterator(); 

        while(it.hasNext()) {
            Row curr = it.next();
            if (!set1.add(curr) && selectedRows.contains(curr)) {
                it.remove();
            }
        }
        return allRows;
    }
}

结果,即 refreshedArray,应该等于:

key = "0", values = {1,2,3}
key = "0", values = {1,2,2};
key = "1", values = {1,2,3};
key = "2", values = {1,1,1};

This post is the continuation of my previous post. Now I have a code that I'd like to compile. The only difference is that now I'm using lists of my own class List<Row> instead of List<Integer[]>. In particular look at hashCode in Row, because it provides a compilation error.

    public class Row {
          private String key;
          private Integer[] values;

          public Row(String k,Integer[] v) {
              this.key = k;
              this.values = v;
          }

          public String getKey() {
              return this.key;
          }

          public Integer[] getValues() {
              return this.values;
          }

          @Override
          public boolean equals(Object obj) {
              if(this == obj)
                  return true;
              if((obj == null) || (obj.getClass() != this.getClass()))
                  return false;
              // object must be Row at this point
              Row row = (Row)obj;
                  return ((key == row.key) && (values == row.values));
          }

          @Override
          public int hashCode () { // HERE I HAVE A PROBLEM. DON'T KNOW HOW TO IMPLEMENT IT
              return this.key;
          }

    }

public class Test {

    public static void main(String[] args) {
        List<Row> allRows = new ArrayList<Row>();
        allRows.add(new Row("0",new Integer[]{1,2,3}));
        allRows.add(new Row("0",new Integer[]{1,2,2}));
        allRows.add(new Row("1",new Integer[]{1,2,3}));
        allRows.add(new Row("2",new Integer[]{1,1,1}));
        allRows.add(new Row("2",new Integer[]{1,1,1}));

List<Row> selectedRows = new ArrayList<Row>();
            selectedRows.add(new Row("0",new Integer[]{1,2,3}));
            selectedRows.add(new Row("2",new Integer[]{1,1,1}));

    System.out.println(allRows);
    System.out.println(selectedRows);
        List<Row> refreshedRows = refreshRows(allRows,selectedRows);
        System.out.println(refreshedRows);

    }

    private static List<Row> refreshRows(List<Row> allRows,List<Row> selectedRows) {
        Set<Row> set1 = new HashSet<Row>();
        Iterator<Row> it = allRows.iterator(); 

        while(it.hasNext()) {
            Row curr = it.next();
            if (!set1.add(curr) && selectedRows.contains(curr)) {
                it.remove();
            }
        }
        return allRows;
    }
}

The result, i.e. refreshedArray, should be equal to:

key = "0", values = {1,2,3}
key = "0", values = {1,2,2};
key = "1", values = {1,2,3};
key = "2", values = {1,1,1};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤君无依 2025-01-01 18:26:08

请尝试以下操作。尽管有一些细微的变化,大部分代码都是由 Netbeans IDE 7.0 生成的:

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Row other = (Row) obj;
    if ((this.key == null) ? (other.key != null) : !this.key.equals(other.key)) {
        return false;
    }
    if (!java.util.Arrays.deepEquals(this.values, other.values)) {
        return false;
    }
    return true;
}

@Override
public int hashCode() {
    int hash = 3;
    hash = 79 * hash + (this.key != null ? this.key.hashCode() : 0);
    hash = 79 * hash + java.util.Arrays.deepHashCode(this.values);
    return hash;
}

Try with the following. Despite minor changes, most of the code is generated by Netbeans IDE 7.0:

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Row other = (Row) obj;
    if ((this.key == null) ? (other.key != null) : !this.key.equals(other.key)) {
        return false;
    }
    if (!java.util.Arrays.deepEquals(this.values, other.values)) {
        return false;
    }
    return true;
}

@Override
public int hashCode() {
    int hash = 3;
    hash = 79 * hash + (this.key != null ? this.key.hashCode() : 0);
    hash = 79 * hash + java.util.Arrays.deepHashCode(this.values);
    return hash;
}
追风人 2025-01-01 18:26:08

查看 hashcode() 的签名。它返回一个原始整数。您正在返回 key,其类型为 String。尝试这样的事情:

@Override
public int hashCode() {
    int hash = 1;
    hash = hash * 31 + key.hashCode();
    //hash = hash * 31 + otherFields.hashCode() etc
    return hash;
}

你的 IDE 甚至可以为你生成。您可能应该阅读哈希码。你的 equals 方法看起来也错了。比较两个整数数组是否相等意味着什么?

Look at the signature for hashcode(). It returns a primitive integer. You are returning key which is of type String. Try something like this:

@Override
public int hashCode() {
    int hash = 1;
    hash = hash * 31 + key.hashCode();
    //hash = hash * 31 + otherFields.hashCode() etc
    return hash;
}

which your IDE can even generate for you. You should probably read up on hashcodes. Your equals method looks wrong too. What is meant by comparing the two Integer arrays for equality?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文