Java Set 不存储具有相同元素的类似列表,但它存储数组的重复项

问题描述 投票:0回答:1

java中set如何比较对象?

我正在阅读数组和列表的 equals 方法的 java 文档,从文档中看它看起来很相似。有没有更好的区分方法。

这篇文章只是建议使用列表,但我想了解我如何知道其他数据结构? https://stackoverflow.com/questions/7488098/storing-arrays-in-set-and-avoiding-duplicates

例如:

   int[] arr1 = new int[] {1, 2};
   int[] arr2 = new int[] {1, 2};
   if (arr1 == arr2) {
      // doesn't get printed
      System.out.println("hello");
   }
   Set<int[]> set1 = new HashSet<>();
   set1.add(arr1);
   set1.add(arr2);
   System.out.println(set1.size());
   // Prints 2
    Set<List<Integer>> set = new HashSet<>();
    List<Integer> list1 = Arrays.asList(1, 2, 3, 4);
    List<Integer> list2 = Arrays.asList(1, 2, 3, 4);
    set.add(list1);
    set.add(list2);
    if (list1 == list2) {
    // doesn't get printed
      System.out.println("hello");
    }
    System.out.println(set);
    System.out.println(set.size());
    // Prints
    // [[1, 2, 3, 4]]
    // 1
java arrays list set
1个回答
0
投票

“...java中set如何比较对象?...”

Set接口有不同的实现。

在您的特定示例中,您使用的是 HashSet,它利用 Map,或更准确地说,HashMap
其中,在输入时,又会生成所提供的 key哈希码值。
意思是,没有两个 key 可能具有相同的 hash-code

这是源代码,用于 HashMap#put
GitHub – HashMap.java – put(K, V)

/**
 * Associates the specified value with the specified key in this map.
 * If the map previously contained a mapping for the key, the old
 * value is replaced.
 *
 * @param key key with which the specified value is to be associated
 * @param value value to be associated with the specified key
 * @return the previous value associated with {@code key}, or
 *         {@code null} if there was no mapping for {@code key}.
 *         (A {@code null} return can also indicate that the map
 *         previously associated {@code null} with {@code key}.)
 */
public V put(K key, V value) {
    return putVal(hash(key), key, value, false, true);
}

而且,这是 HashMap#putVal
GitHub – HashMap.java – putVal(int, K, V, 布尔值, 布尔值).

它似乎是典型的Set抽象;仅允许唯一值。
这是相关的 Wikipedia 文章。
维基百科 – 集合(抽象数据类型)

/**
 * Implements Map.put and related methods.
 *
 * @param hash hash for key
 * @param key the key
 * @param value the value to put
 * @param onlyIfAbsent if true, don't change existing value
 * @param evict if false, the table is in creation mode.
 * @return previous value, or null if none
 */
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
               boolean evict) {
    Node<K,V>[] tab; Node<K,V> p; int n, i;
    if ((tab = table) == null || (n = tab.length) == 0)
        n = (tab = resize()).length;
    if ((p = tab[i = (n - 1) & hash]) == null)
        tab[i] = newNode(hash, key, value, null);
    else {
        Node<K,V> e; K k;
        if (p.hash == hash &&
            ((k = p.key) == key || (key != null && key.equals(k))))
            e = p;
        else if (p instanceof TreeNode)
            e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
        else {
            for (int binCount = 0; ; ++binCount) {
                if ((e = p.next) == null) {
                    p.next = newNode(hash, key, value, null);
                    if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                        treeifyBin(tab, hash);
                    break;
                }
                if (e.hash == hash &&
                    ((k = e.key) == key || (key != null && key.equals(k))))
                    break;
                p = e;
            }
        }
        if (e != null) { // existing mapping for key
            V oldValue = e.value;
            if (!onlyIfAbsent || oldValue == null)
                e.value = value;
            afterNodeAccess(e);
            return oldValue;
        }
    }
    ++modCount;
    if (++size > threshold)
        resize();
    afterNodeInsertion(evict);
    return null;
}

“...这篇文章只是建议使用列表,但我想了解我如何知道其他数据结构?...”

这是一篇维基百科文章,列出了不同类型的数据结构
维基百科 – 数据结构列表

© www.soinside.com 2019 - 2024. All rights reserved.