Discuss / Java / 练习:去除重复消息

练习:去除重复消息

Topic source

jasmine

#1 Created at ... [Delete] [Delete and Lock User]
static List<Msg> process(List<Msg> received) {
   Set<Msg> set = new HashSet<>(received);   return new ArrayList<>(set);}


class Msg {
    public final int sequence;    public final String text;    public Msg(int sequence, String text) {
        this.sequence = sequence;        this.text = text;    }

    @Override    public boolean equals(Object o) {
        if(o instanceof Msg) {
            Msg m = (Msg) o;            return this.sequence == m.sequence;        }
        return false;    }

    @Override    public int hashCode() {
        return Objects.hash(this.sequence);    }
}

alienation

#2 Created at ... [Delete] [Delete and Lock User]

为什么

return new ArrayList<>(set);

这一步顺序为什么不会乱?不是hashset不保证顺序么??arraylist也没说会内部再排序啊

alienation

#3 Created at ... [Delete] [Delete and Lock User]

实测了一些,顺序乱了。。


  • 1

Reply