Java Container class 3:set/hastset/mapset in-depth interpretation

Source: Internet
Author: User

Introduced

Set: A collection, which is a collection that does not contain duplicate data. (A collection that contains no duplicate elements. )

The set contains a maximum of one null element, the no contains two identical elements, and does not conform to the definition.

The previous article learned some of the basic interfaces of the container classes in Java, and the list branches in the three branches of the collection interface (ArrayList and LinkedList). This article will explain the Set branches in the three major branches of collection (list, set, Queue), and the derived subclasses.

Java Container class Analysis: collection,list,arraylist,linkedlist in-depth interpretation

The total integration relationship of the set interface is as follows:

Set interface

Public interface Set<e> extends collection<e>

Looking at the source of the set, you can see that the interface functions in the set are identical to the collection, and no new interfaces are added. Only subclasses need to consider the principle that there cannot be duplicate elements in the set when implementing these interfaces. The functions and descriptions in the interface can be viewed in the previous blog post.

Abstractset Abstract class

Similar to the role of Abstractlist and Abstractcollection, the Abstractset class, as an abstract class, implements some functions in the set interface and reduces the implementation of subsequent set subclasses.

Public abstract class Abstractset<e> extends abstractcollection<e> implements Set<e>

Specifically, the abstract class implements the following interfaces in the set:
public boolean equals (Object o) {        if (o = = this)            return true;        if (! (o instanceof Set))            return false;        collection<?> C = (collection<?>) o;        if (C.size ()! = Size ())            return false;        try {            
return Containsall (c);
        } catch (ClassCastException unused)   {            return false;        } catch (NullPointerException unused) {            return false;        }    }

Hashcode (): The hash value of set equals the addition of the hash value of all the elements in the set, which ensures that if (Set1.equals (Set2)) then set1.hashcode () = = Set2.hashcode ().

public int hashcode () {        int h = 0;        iterator<e> i = Iterator ();        while (I.hasnext ()) {            E obj = I.next ();            if (obj = null)                H + = Obj.hashcode ();        }        return h;    }

RemoveAll (): Removes the intersection of C with the current set, the return value represents whether an element is removed from the current set, and the intersection is NULL returns FALSE otherwise true.

public boolean RemoveAll (collection<?> c) {        objects.requirenonnull (c);        Boolean modified = false;        if (Size () > C.size ()) {for            (iterator<?> i = C.iterator (); I.hasnext ();)                Modified |= Remove (I.next ());        Else {for            (iterator<?> i = Iterator (); I.hasnext ();) {                if (c.contains(I.next ())) {                    i.remove ();                    modified = true;}}        }        return modified;    }

SortedSet

Public interface Sortedset<e> extends set<e>

 Public Interface extends set<e> {//After some sort of set       JDK1.7   java.util      Super e> Comparator ();  Returns the comparer  for sorting the elements in this set    sortedset<e> subset (e fromelement, E toelement);  Returns a partial view of this set whose elements are from fromelement (including) to Toelement (not included). (      sortedset<e> headSet (E toelement);  Returns a partial view of this set whose elements are strictly less  than toelement    sortedset<e> tailset (E fromelement);  Returns a partial view of this set whose elements are greater than  or equal to Fromelement    E First ();  Set in the first element      E last ();  The last element  in set }
Navigableset
 Public InterfaceNavigableset<e>extendssortedset<e> {//Extended SortedSet, with a search matching element method JDK1.7 Java.utile Lower (e e);//Returns the largest element in this set that is less than the given elementE floor (e e);//Returns the largest element in this set that is less than or equal to the given elementE ceiling (e e);//Returns the smallest element in this set greater than or equal to the given elemente higher (e e);//Returns the smallest element in this set greater than the given elementE Pollfirst ();//Gets and removes the first elementE Polllast ();//Gets and removes the last elementIterator<e> Iterator ();//An iterator that iterates over the elements of this set in ascending orderNavigableset<e> Descendingset ();//Returns an inverted view of the elements contained in this setIterator<e> Descendingiterator ();//Returns an iterator that iterates over the elements of this set in descending order.   The effect is equivalent to Descendingset (). iterator ().     //Returns a partial view of this set whose elements range from fromelement to ToelementNavigableset<e> subset (E fromelement,BooleanFrominclusive, E toelement,Booleantoinclusive);//Returns a partial view of this set whose elements are less than (or equal to, if inclusive is true) ToelementNavigableset<e> HeadSet (E toelement,Booleaninclusive);//Returns a partial view of this set whose elements are greater than (or equal to, if inclusive is true) FromelementNavigableset<e> Tailset (E fromelement,Booleaninclusive);//Returns a partial view of this set whose elements are from fromelement (including) to Toelement (not included). Sortedset<e> subset (e fromelement, E toelement);//Returns a partial view of this set whose elements are strictly less than toelementSortedset<e> HeadSet (E toelement);//Returns a partial view of this set whose elements are greater than or equal to FromelementSortedset<e> Tailset (E fromelement);}
HashSet

Public class Hashset<e> extends abstractset<e> implements Set<e>, cloneable, Java.io.Serializable

Reading HashSet source code, in fact, is very simple, is to maintain a hashmap type of member variables, and then abstracted a few additions and deletions to change the method. If you need to know more, you can read: Java container class 2:map and HashMap in-depth interpretation

Here is a question, HashMap is to store the key value pairs, and HashSet is to store a set of data, why can I save the set data with HashMap?

The keyset () function in HashMap returns a set type of data (cannot have duplicate key values), so HashSet holds the set array all in HashMap keyset, and the value is all represented by a constant of the default object type.

The Add () function in HashSet can be seen

 Public Boolean Add (e e) {        return map.put (e, PRESENT) = =null;    }
TreeSet

TreeSet's inheritance is the following, and it looks pretty complicated.

As TreeSet inherits from Sortset, the sorted array is stored in TreeSet (each element value cannot have duplicate elements).

Identical to the HashSet implementation method, a member variable of type map (navigablemap type) is maintained to ensure that all data is ordered. The keyset field in map can hold all the values of TreeSet.

TreeSet in the method are based on the navigablemap of the additions and deletions to the operation, here is not detailed analysis.

Reference:

Http://www.cnblogs.com/NeilZhang/p/8577991.html

Java Container class 3:set/hastset/mapset in-depth interpretation

Related Article

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.