如何在Java中维护唯一列表? [英] How to maintain a Unique List in Java?

查看:189
本文介绍了如何在Java中维护唯一列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Java中创建唯一/不同对象列表(无重复项)?

How to create a list of unique/distinct objects (no duplicates) in Java?

现在我正在使用 HashMap< String,Integer> 来执行此操作,因为密钥被覆盖,因此在最后我们可以获得 HashMap.getKeySet()这将是唯一的。但我确信应该有更好的方法来实现这一点,因为价值部分浪费在这里。

Right now I am using HashMap<String, Integer> to do this as the key is overwritten and hence at the end we can get HashMap.getKeySet() which would be unique. But I am sure there should be a better way to do this as the value part is wasted here.

推荐答案

你可以使用设置实施:

来自JAVADoc的一些信息:

Some info from the JAVADoc:


包含无重复元素的集合。更正式地说,集合不包含元素对e1和e2,使得e1.equals(e2)和至多一个null元素。正如其名称所暗示的,此接口对数学集抽象进行建模。

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

注意:如果将可变对象用作集合,则必须非常小心元素。如果在对象是集合中的元素的同时以影响等于比较的方式更改对象的值,则不指定集合的​​行为。这种禁止的特殊情况是,不允许集合将自身包含为元素。

Note: Great care must be exercised if mutable objects are used as set elements. The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the set. A special case of this prohibition is that it is not permissible for a set to contain itself as an element.`

这些是实现:

此类为基本操作(添加,删除,包含和大小)提供恒定的时间性能,假设哈希函数在桶之间正确地分散元素。迭代此集合需要的时间与HashSet实例的大小(元素数量)加上后备HashMap实例的容量(桶数)之和成比例。因此,如果迭代性能很重要,则不要将初始容量设置得太高(或负载因子太低)非常重要。

This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets. Iterating over this set requires time proportional to the sum of the HashSet instance's size (the number of elements) plus the "capacity" of the backing HashMap instance (the number of buckets). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.

迭代 HashSet 生成元素的顺序未定义。

When iterating a HashSet the order of the yielded elements is undefined.

LinkedHashSet

Set接口的哈希表和链表实现,可预测的迭代顺序。此实现与HashSet的不同之处在于它维护了一个贯穿其所有条目的双向链表。此链接列表定义迭代排序,即元素插入集合(插入顺序)的顺序。请注意,如果将元素重新插入到集合中,则不会影响插入顺序。 (如果s.contains(e)在调用之前立即返回true,则调用s.add(e)时,将元素e重新插入集合中。)

Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set. (An element e is reinserted into a set s if s.add(e) is invoked when s.contains(e) would return true immediately prior to the invocation.)

所以,上面代码的输出......

So, the output of the code above...

 Set<Integer> linkedHashSet = new LinkedHashSet<>();
 linkedHashSet.add(3);
 linkedHashSet.add(1);
 linkedHashSet.add(2);

 for (int i : linkedHashSet) {
     System.out.println(i);
 }

...必然是

3
1
2


  • TreeSet

    此实现为基本操作(添加,删除和包含)提供有保证的log(n)时间成本。默认情况下,迭代返回的元素按自然顺序排序,所以上面的代码......

    This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains). By default he elements returned on iteration are sorted by their "natural ordering", so the code above...

     Set<Integer> treeSet = new TreeSet<>();
     treeSet.add(3);
     treeSet.add(1);
     treeSet.add(2);
    
     for (int i : treeSet) {
         System.out.println(i);
     }
    

    ...将输出:

    1
    2
    3
    

    (您还可以将 Comparator 实例传递给 TreeSet 构造函数,使其对元素进行排序一个不同的顺序。)

    (You can also pass a Comparator instance to a TreeSet constructor, making it sort the elements in a different order.)

    注意,由一个集合维护的排序(无论是否提供显式比较器)必须与equals一致才能正确实现设置界面。 (请参阅Comparable或Comparator以获得与equals一致的精确定义。)这是因为Set接口是根据equals操作定义的,但TreeSet实例使用compareTo(或compare)方法执行所有元素比较,因此从该集合的角度来看,通过这种方法被认为相等的元素是相等的。集合的行为即使其排序与equals不一致也是明确定义的;它只是没有遵守Set接口的一般合约。

    Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.

    这篇关于如何在Java中维护唯一列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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