java - 比较通用项目?

标签 java generics compare project

我目前陷入了代码的特定部分。在我的类(class)中,我们将创建一列火车,其中包含装有人员或 cargo 的棚车。我们使用泛型来定义棚车是否可以容纳人员或 cargo 。然后我们将个人/ cargo 装载到棚车上,如果它与棚车上已有的人具有相同的字符串“ID”,那么我们会记录错误并且不加载该人/ cargo 。这就是我遇到麻烦的地方。我一生都无法弄清楚如何比较他们的“ID”来看看它们是否相等。以下是我到目前为止的代码,

package proj5;

public class Person implements Comparable<Person> {

private String id;
private String name;
private int age;

public Person(String id, String name, int age){
    this.id = id;
    this.id = id;
    this.name = name;
    this.age = age;
}   

public Person(String id){
    this.id = id;
}

public String getId(){
    return id;
}

public int getAge(){
    return age;
}

public String getName(){
    return name;
}

public String toString(){
    String str = "        " + "ID: " + id + "  " + " Name: " + name + "  " + " Age: " + age;
    return str;
}

public int compareTo(Person p) {
    int result = this.id.compareTo(p.getId());
    return result;
}

}

package proj5;

import java.util.ArrayList;
import java.util.List;
import java.util.Collections;

public class Boxcar<T extends Comparable<T>> {

private ArrayList<T> boxcar;
private int maxItems;
private int boxcarID;

public Boxcar(){
    boxcar = new ArrayList<T>();
}

public void load(T thing){
    for(int i = 0; i < boxcar.size(); i++){
    if(boxcar.size() < maxItems && !boxcar.get(i).equals(thing)){
        boxcar.add(thing);
        System.out.println(boxcar.get(i));
    }
    else{
        boxcar.remove(thing);
    }
    }
    Collections.sort(boxcar);
}

public int getBoxcarId(){
    return boxcarID;
}

public int getMaxItems(){
    return maxItems;
}

public void setMaxItems(int i){
    maxItems = i;
}

public void unload(T thing){
    for(T item : boxcar){
        if(item.equals(thing)){
            boxcar.remove(item);
        }
    }
}

public List<T> getBoxcar(){
    return boxcar;
}

public String toString(){
    String str = "";
    for(T item : boxcar){
        str += item + "\n";
    }
    return str;
}

}

问题出在我的加载函数上。我不知道如何比较他们的ID。为了澄清起见,对象 ID 是字符串。我还有其他类(class),但我只包括了我认为必要的类(class)。如果您需要更多文件,我将很乐意提供。我已经被困在这个问题上几个小时了,非常感谢任何帮助!预先非常感谢您!

编辑:我尝试使用 Collections API 中的 contains() 方法,但为什么不起作用?听起来它会完美地工作。

最佳答案

您必须为您的 Person 类实现 equals 和 hashCode。

问题在于 boxcar.get(i).equals(thing) 正在调用通用 equals 并且它仅比较引用。

所以通用的 equals 看起来像这样。

public boolean equals(Object obj){
  if (obj == null) return false;
  if (obj == this) return true;
  if (obj instanceof Person){
    Person p = (Person) obj;
    return p.getId().equals(this.getId());
  }
  return false;
}

hashCode 可以是这样的

public int hashCode(){
  return 37*this.getId().hashCode();
}

关于java - 比较通用项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13795069/

相关文章:

java - 如何在运行时更改 Android 主题而不使用 recreate()?

Java:在运行时获取通用类参数

java - 如何仅比较二维 int 数组的值?

c - 在 C 中比较字符串的最快方法

java - 使用 JSF 提交数据表值并通过 javax.servlet.http.HttpServletRequest 访问它

Java 类内部泛型绑定(bind)掩盖了父接口(interface)泛型绑定(bind)定义

java - Box2d Edgeshape 不旋转

generics - 这种构造导致代码不像类型注释所指示的那么通用

angularjs - 一种类型定义,无需联合即可涵盖原始类型和泛型类型的属性

objective-c - 比较两个 NSArrays 并返回差异数