hashCode和equals应使用相同的字段,这些字段应该是有效的不可变的(即在添加到散列集合后不会更改)
这可以包括您喜欢的日期或任何字段.
顺便说一下,我更喜欢使用long而不是Date因为我可以使它变得不可变而且速度稍慢.
如果您要使用时间戳作为id,您还可以通过向上推毫秒来确保它是唯一的(如果您可以存储这样的时间戳,则可以确保它是唯一的)
private static final AtomicLong TIME_STAMP = new AtomicLong();
// can have up to 1000 ids per second.
public static long getUniqueMillis() {
    long now = System.currentTimeMillis();
    while (true) {
        long last = TIME_STAMP.get();
        if (now <= last)
            now = last + 1;
        if (TIME_STAMP.compareAndSet(last, now))
            return now;
    }
}
要么
private static final AtomicLong TIME_STAMP = new AtomicLong();
// can have up to 1000000 ids per second.
public static long getUniqueMicros() {
    long now = System.currentTimeMillis() * 1000;
    while (true) {
        long last = TIME_STAMP.get();
        if (now <= last)
            now = last + 1;
        if (TIME_STAMP.compareAndSet(last, now))
            return now;
    }
}