【问题标题】:Java Collection removeAll not removing a thingJava Collection removeAll 没有删除任何东西
【发布时间】:2014-01-28 00:05:37
【问题描述】:

所以我有一个旧列表、一个新列表和一个唯一列表。我从每个列表(旧/新)中读取数据,并从我的类文件中创建一堆对象。然后我将 newList 添加到唯一用户,然后删除旧列表以确定唯一用户。

public class User {

    private String fName;
    private String mInitial;
    private String lName;
    private String age;
    private String city;
    private String state;

        ... // set and get methods

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((age == null) ? 0 : age.hashCode());
    result = prime * result + ((city == null) ? 0 : city.hashCode());
    result = prime * result + ((fName == null) ? 0 : fName.hashCode());
    result = prime * result + ((lName == null) ? 0 : lName.hashCode());
    result = prime * result
            + ((mInitial == null) ? 0 : mInitial.hashCode());
    result = prime * result + ((state == null) ? 0 : state.hashCode());
    return result;
    }

@Override
public boolean equals(Object o) {
    if(o == null) return false;
    if (getClass() != o.getClass()) return false;

    User other = (User) o;
    if(this.fName != other.fName) return false;
    if(! this.mInitial.equals(other.mInitial)) return false;
    if(! this.lName.equals(other.lName))   return false;
    if(! this.age.equals(other.age)) return false;
    if(! this.city.equals(other.city)) return false;
    if(! this.state.equals(other.state)) return false; 

    return true;
    }

}

主要

    try {

    // List creation (new, old, unique)
    List<User> listNew = new ArrayList<User>();
    List<User> listOld = new ArrayList<User>();
    Collection<User> listUnique = new HashSet<User>();

    // Read the files in with while loop, 
    //  ... 
    // Put them in their respective list
    //  ...

    listUnique.addAll(listNew);
    System.out.println("Junk... " + listUnique.size());
    listUnique.removeAll(listOld);

    // Checking the sizes of lists to confirm stuff is working or not
    System.out.println(
                "New: \t" + listNew.size() + "\n" + 
                "Old: \t" + listOld.size() + "\n" + 
                "Unique: " + listUnique.size() + "\n"
                );


    } 
    catch { ... }

输出

Junk... 20010
New:    20010
Old:    20040
Unique: 20010

所以基本上它是将内容添加到列表中,但 removeAll 不起作用。这可能是我的用户类文件中的 hashCode() 的问题吗?我只是无法弄清楚为什么它不起作用。 (注意:我在类文件中自动生成了我的 hashCode,不确定这是不是一个坏主意)

感谢您的帮助!

【问题讨论】:

  • if(this.fName != other.fName) return false; 让我印象深刻。
  • 为什么你会选择与!=进行6个字符串比较中的1个?
  • 鉴于我对创建自己的 equals/hashCode 方法不是很熟悉,所以我使用了本指南:tutorials.jenkov.com/java-collections/hashcode-equals.html .. 所以您认为我应该将其更改为与其他方法相同吗?

标签: java list object set


【解决方案1】:

正如Takendark 指出的那样。这可能是因为在字符串名称的情况下您正在检查引用而不是值。如果名称的来源不同(它们有不同的引用),即使它们具有相同的值,它们也可能被视为不平等。

【讨论】:

  • 是否可以检查参考值?怎么可能直接去源头进行比较?
  • 不需要知道参考价值。您需要做的是使用 equals 方法而不是 == 运算符。或在您的特定情况下使用 (!object1.equals(object2)) 而不是 object1!=object2。
  • equals 方法的字符串比较值,而 as != 比较引用。
猜你喜欢
  • 2011-06-01
  • 2015-02-11
  • 2014-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-16
  • 2021-10-29
相关资源
最近更新 更多