LinkedHashSet .equals() vs LinkedList .equals() 元素相同但顺序不同 [英] LinkedHashSet .equals() vs LinkedList .equals() with same elements but different order

查看:22
本文介绍了LinkedHashSet .equals() vs LinkedList .equals() 元素相同但顺序不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下 SSCCE:

Consider the following SSCCE:

public static void main(String[] args) {
    LinkedHashSet<String> set1 = new LinkedHashSet<>();
    set1.add("Bob");
    set1.add("Tom");
    set1.add("Sam");
    LinkedHashSet<String> set2 = new LinkedHashSet<>();
    set2.add("Sam");
    set2.add("Bob");
    set2.add("Tom");

    System.out.println(set1);
    System.out.println(set2);
    System.out.println(set1.equals(set2));
}

打印:

[Bob, Tom, Sam]
[Sam, Bob, Tom]
true

但是,如果您将 LinkedHashSet 更改为 LinkedList:

Yet if you change LinkedHashSet to LinkedList:

public static void main(String[] args) {
    LinkedList<String> set1 = new LinkedList<>();
    set1.add("Bob");
    set1.add("Tom");
    set1.add("Sam");
    LinkedList<String> set2 = new LinkedList<>();
    set2.add("Sam");
    set2.add("Bob");
    set2.add("Tom");

    System.out.println(set1);
    System.out.println(set2);
    System.out.println(set1.equals(set2));
}

它产生:

[Bob, Tom, Sam]
[Sam, Bob, Tom]
false

我的问题是澄清问题之一.有人可以帮助理解这一点吗?为什么 LinkedHashSet 会被认为是相等的,而相同的 LinkedList 不会?我假设 ListSet 的定义起作用,但我不确定.

My question is one of clarification. Can someone help make sense of this? Why would a LinkedHashSet be considered equals whereas the same LinkedList would not? I'm assuming the definition of List and Set plays a role, but I'm not sure.

基本上,我是说,如果您认为 Set 是相同的,您是否也会认为 List 是相同的?反之亦然(假设没有重复的元素)?

Basically, I'm saying if you consider the Sets to be the same, wouldn't you consider the Lists to be the same too? And vice-versa (assuming no duplicate elements)?

推荐答案

LinkedHashSet 做出的保证是关于迭代顺序的.然而,它仍然是一个 Set 并且一个集合本身并不关心顺序.另一方面,List 可以.一个元素在第三个位置的 List 与另一个在第一个位置具有相同元素的 List 不同.

The guarantee that LinkedHashSet makes is about iteration order. However, it's still a Set and a set doesn't care about order in itself. A List on the other hand, does. A List with an element in 3rd position is not the same as another List with the same element in the 1st position.

Set 用于 equals(Object) 方法

Set javadoc for the equals(Object) method

如果指定的对象也是一个集合则返回真,这两个集合有相同的大小,并且指定集合的​​每个成员都包含在这个集合(或者等价地,这个集合的每个成员都包含在指定的集合).这个定义保证了equals方法可以在 set 接口的不同实现中正常工作.

Returns true if the specified object is also a set, the two sets have the same size, and every member of the specified set is contained in this set (or equivalently, every member of this set is contained in the specified set). This definition ensures that the equals method works properly across different implementations of the set interface.

LinkedHashSet javadoc 声明

The LinkedHashSet javadoc states

Set接口的哈希表和链表实现,带有可预测的迭代顺序.

Hash table and linked list implementation of the Set interface, with predictable iteration order.

一个 LinkedHashSet 是一个 Set.它具有相同的规则,即.适用于集合 ADT 的那些.

A LinkedHashSet is a Set. It has the same rules, ie. those that apply to the set ADT.

这篇关于LinkedHashSet .equals() vs LinkedList .equals() 元素相同但顺序不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆