Set interface of the set framework, Set framework

Source: Internet
Author: User
Tags addall

Set interface of the set framework, Set framework


A collection that does not contain repeated elements. To be more precise,Set does not contain element pairs that meet e1.equals (e2)E1 and e2, andContains up to one null element..

In all constructor methods and add, equals, and hashCode protocols, other provisions are added to the Set interface, which exceeds the content inherited from the Collection interface.

Set cannot contain duplicate elements,All its methods are inherited from the Collection interface.And add restrictions to some methods. Set also enhances the availability of the equals and hasCode methods, allowing meaningful comparison of instances of different implementation types

The methods defined by the Set interface are the same as those defined by the Collection interface. They are not listed here. They are only described in the batch operations of the Set interface. Assuming that s1 and s2 are both Set types, some examples are given below.

S1.ContainsAll(S2)-If s2 is a subset of s1, true is returned; otherwise, false is returned.

S1.AddAll(S2)-store the Union of s1 and s2 to s1

S1.RetainAll(S2)-store the intersection of s1 and s2 to s1

S1.RemoveAll(S2)-remove elements in s1 that overlap with s2

 

The Java platform provides three general implementation classes: HashSet, TreeSet, and javashashset.

HashSetUsing a hash table as a container is the best implementation class, but it does not guarantee the iteration sequence, and the element sequence is not fixed.

TreeSetThe red/black tree is used as a container and sorted by element values. Its efficiency is much lower than that of HashSet.

LinkedHashSetThe hash table is also implemented. The difference is that the hash table runs a linked list and elements are sorted in the order of insertion. The element sequence of the LinkedHashSet is much clearer than that of the chaotic HashSet, but the efficiency is much lower than that of the HashSet.

HashSet

This class implements the Set interface, which is composed of a hash table (Actually a HashMap instance. ItThe set iteration sequence is not guaranteed.In particular, it does not guarantee that the order remains unchanged. This class allows the use of null elements.

This class provides stable performance for basic operations, including add, remove, contains, and size, assuming that the hash function correctly distributes these elements in the bucket. The time required for iteration on this set is the "capacity" and proportion of the size (number of elements) of the HashSet instance and the number of underlying HashMap instances (number of buckets. Therefore, if iteration performance is important, do not set the initial capacity too high (or set the loading factor too low ).

In addition to the generic implementation classes of the Set, HashSet has two special constructor functions:

HashSet (intinitialCapacity)

Construct a new empty set. Its underlying HashMap instance has the specified initial capacity and default loading Factor (0.75 ).

HashSet (intinitialCapacity, float loadFactor)

Construct a new empty set. Its underlying HashMap instance has the specified initial capacity and the specified loading factor.

The default value of initial capacity is 16 and the default value of loading factor is 0.75.

Note that HashSet does not support batch operation methods defined in the Collection interface, such as addAll (), containsAll (), removeAll (), retainAll ()

TreeSet

TreeMap-basedNavigableSet Interface. Sort the elements in the natural order of the elements, or sort the elements based on the Comparator provided when the set is created, depending on the construction method used.

Note: to correctly implement the Set interface, the set maintenance sequence (whether or not an explicit comparator is provided) must be consistent with equals. This is because the Set interface is defined according to the equals operation, but the TreeSet instance uses its compareTo (or compare) method to compare all elements. Therefore, from the perspective of set, in this method, two equal elements are considered equal. Even if the set sequence is inconsistent with equals, its behavior is well defined. It only violates the conventional protocol of the Set interface.

This implementation class also has two special constructor methods:

TreeSet (Comparator <? Super E> comparator)

Construct a new empty TreeSet, Which is sorted by the specified comparator.

TreeSet (SortedSet <E> s)

Construct a new TreeSet with the same ing relationship and same sorting as the specified ordered set.

TreeSet Extension Method (that is, the method not defined in the Set Interface ):

E ceiling (E e)

Returns the smallest element in this set that is equal to or greater than the given element. If such an element does not exist, null is returned.

Comparator <? Super E> comparator ()

Returns the comparator for sorting the elements in this set. If this set uses the natural sequence of its elements, null is returned.

NavigableSet <E> descendingSet ()

Returns the reverse view of the elements contained in this set.

E first ()

Returns the first (lowest) element of the current set.

E floor (E e)

Returns the largest element smaller than or equal to the given element in this set. If such an element does not exist, null is returned.

SortedSet <E> headSet (E toElement)

Returns some views of this set, whose elements are strictly less than the toElement.

NavigableSet <E> headSet (E toElement, boolean random SIVE)

Return some views of this set. The element is smaller than (or equal to, if the fully sive value is true) toElement.

E higher (E e)

Returns the minimum element strictly greater than the given element in this set. If such an element does not exist, null is returned.

E last ()

Returns the last (highest) element of the current set.

E lower (E e)

Returns the largest element in the set that is strictly less than the given element. If such an element does not exist, null is returned.

E pollFirst ()

Gets and removes the first (lowest) element. If this set is null, null is returned.

E pollLast ()

Obtain and remove the last (highest) element. If this set is null, null is returned.

NavigableSet <E> subSet (E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)

Return some views of this set, whose element ranges from fromElement to toElement.

SortedSet <E> subSet (E fromElement, EtoElement)

Return some views of this set, whose elements are from fromElement (included) to toElement (not included ).

SortedSet <E> tailSet (E fromElement)

Return some views of this set. The element is greater than or equal to fromElement.

NavigableSet <E> tailSet (E fromElement, boolean aggressive)

Returns some of the views of this set. The element is greater than (or equal to, if implicit is true) fromElement.

Note that TreeSet implements the SortedSet interface. In the subclass of this interface, view operations (subSet, tailSet, and other methods) are very different from List operations. The List View operation will throw an exception after the source List is modified. For example, we intercept an element of the List using the subList method as the operation object, once the source List is inserted or deleted, an exception will be thrown in the subList obtained using subList. This is not the case for the subclass of SortedSet, because the use of subSet to intercept the starting point of SortedSet is not a fixed two elements, but the absolute position of the underlying object. Modifications to the source set will be synchronized to the child set, and vice versa. In addition, the Set view operation is the same as the List operation.Semi-open, That isContains low-level segments, not high-level segmentsFor example, there is a Set containing the letter a-g, subSet ('A', 'D ') the result is a set of 'A', 'B', and 'c '.

LinkedHashSet

Implement the hash table and link list of the Set interface with predictable iteration sequence. This implementation differs from HashSet in that the latterMaintains a list of dual links running on all entries. The Link List defines the iteration order, that isIterate by inserting elements into the set (insertion order). Note that the insertion sequence is not affected by the re-inserted elements in the set. (If you call s. add (e) Immediately after s. contains (e) returns true, Element e is re-inserted into set s .)

This implementation prevents the customer from undesignated and generally disordered sorting tasks provided by HashSet, without increasing the associated costs with TreeSet. You can use it to generate a set copy that is identical to the original sequence and has nothing to do with the implementation of the original set:

     void foo(Set s) {         Set copy = new LinkedHashSet(s);         ...     }

If the module obtains a set through input, copies the set, and returns the result that determines the sequence of the copy, this technique is particularly useful in this case. (The order in which the content is returned is usually the same as that in which it appears .)

Extension constructor:

LinkedHashSet (intinitialCapacity)

Construct a new empty link hash set with the specified initial capacity and default load factor (0.75.

LinkedHashSet (intinitialCapacity, float loadFactor)

Construct a new empty link hash set with the specified initial capacity and loading factor.

 

This implementation class has no extension method, which is consistent with the method defined in the Set interface.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.