自定义类的equals函数

仅作笔记使用

public class VectorQueue {

private final Vector queue;

private class VectorItem {
private final Object item;
private final int quantity;

public VectorItem(Object item, Integer quantity) {
this.item = item;
this.quantity = quantity;
}

@Override
public boolean equals(Object o) {
if (o instanceof VectorItem) {
return this.item.equals(((VectorItem) o).item);
}
return false;
}

@Override
public int hashCode() {
return this.item.hashCode();
}
}

public VectorQueue() {
this.queue = new Vector(10);
}

public void removeItem(Object item) {
queue.removeElement(new VectorItem(item, null));
}

public boolean isQueued(Object item) {
return queue.contains(new VectorItem(item, null));
}
}

你可能感兴趣的:(Java)