Introduction to the Java Collection set collection

Source: Internet
Author: User
Tags addall

Collection<e>

A collection that does not contain duplicate elements. More formally, theset does not contain the e1.equals(e2) element pairs that are satisfied e1 and e2 contains a maximum of one null element. As its name implies, this interface mimics the mathematical set abstraction.

On all construction methods and on the contract for the add,equals , and hashcode methods, theset interface also adds additional provisions that are beyond the The content inherited by the Collection interface. For convenience, it also includes declarations of other inherited methods, which have been specifically modified for the Set interface, but do not contain any other provisions.

The other rules for these constructs are (not surprisingly) that all construction methods must create a setthat does not contain duplicate elements (as defined above).

Note: If you use a Mutable object as a set element, you must be extremely careful. If the object is an element in set and changes the value of an object in a way that affects the equals comparison, then the behavior of the set is indeterminate. A special case of this prohibition is that a set is not allowed to contain itself as an element.

Some set implementations have restrictions on the elements that they contain. For example, some implementations prohibit null elements, while others have all restrictions on the type of their elements. Attempting to add an unqualified element throws an unchecked exception, usually nullpointerexception or classcastexception. Attempts to query for the presence of unqualified elements may throw an exception, or it may simply return false; Some implementations take the previous behavior, while others use the latter. In summary, when attempting to perform an operation on an unqualified element, if the operation does not result in the insertion of an unqualified element in the set , the operation may throw an exception or succeed, depending on the choice of implementation. The specification for this interface marks such exceptions as "optional."

Method Details:

an int size () returns the number of elements in the set (its capacity). If the set contains more than one integer.max_value element, the Integer.max_value is returned.

Boolean IsEmpty () returns True if the set contains no elements.

Boolean contains (Object o) returns True if the set contains the specified element. More formally, returns true if and only if the set contains an element e that satisfies the following conditions: (o==null? E==null:o.equals (e)).

Iterator<e> Iterator () returns an iterator that iterates over the elements in this set. The returned elements do not have a specific order (unless this set is an instance of a class that provides order guarantees).

Object[] ToArray () returns an array that contains all the elements in the set. Abide by the general agreement of the Collection.toarray method.

<T> t[] ToArray (t[] a) returns an array containing all the elements in the set; the run-time type of the returned array is the type of the specified array. Abide by the general agreement of the Collection.toarray (object[]) method.

Boolean Add (optional action) if the specified element is not already present in the set. More formally, if this set does not contain element e that satisfies the following criteria, the specified element o: (o==null e==null:o.equals (e)) is added to the set. If this set already contains the specified element, the call does not change this set and returns FALSE. By combining the constraints on the construction method, this ensures that the set never contains duplicate elements.
The foregoing does not imply that the set must accept all elements; The set can refuse to add any particular element, including NULL, and throws an exception, as described in the Collection.add specification. Each set implementation should explicitly record all restrictions on the elements that it might contain.

A Boolean remove (Object o) removes the specified element if it exists in the set (optional action). More formally, if the set contains an element e that satisfies the following conditions, it is removed: (o==null? E==null:o.equals (e)). Returns True if the set contains the specified element (or equivalent element, if the result of this set Tiaogan is changed) (once the call returns, the set no longer contains the specified element).

Boolean containsall (collection<?> c) Returns True if this set contains all the elements of the specified Collection. If the specified collection is also a set, returns True when the collection is a subset of this set.

Boolean AddAll (collection<? extends e> c) if all the elements in the Collection are not specified in the set, add them to this set (optional operation). If the specified collection is also a set, the AddAll operation actually modifies the set so that its value is a set of two sets. If the specified collection is modified while the operation is in progress, the behavior of this operation is not specified.

Boolean retainall (collection<?> C) preserves only those elements in the set that are contained in the specified Collection (optional operation). In other words, remove all elements in this set that are not contained in the specified collection. If the specified collection is also a set, this operation actually modifies the set so that its value is an intersection of two sets.

The Boolean removeall (collection<?> c) removes the elements in the set that are contained in the specified Collection (optional operation). If the specified collection is also a set, this operation actually modifies the set so that its value is an asymmetric difference of two sets.

void Clear () Removes all elements in the set (optional operation). When this call returns, the set will be empty (unless it throws an exception).

The Boolean equals (object O) compares the equality of the specified object with this set. Returns true if the specified object is also a set, the size of two sets is the same, and all members of the specified set are included in this set (or all members of this set are included in the specified set). This definition ensures that the Equals method works correctly between different set interface implementations.

int Hashcode () returns the hash code value of the set. A set hash code defines the hash code for all elements in this set and, where the hash code of the null element is defined as zero. This ensures that for any two set S1 and S2, s1.equals (s2) means S1.hashcode () ==s2.hashcode (), as required by the Object.hashcode conventional Protocol.

Usage of Set collection:

1 , HashSet class

The Java.util.HashSet class implements the Java.util.Set interface.

It does not allow duplicate elements to appear;

The order of elements in the political and political collections is not guaranteed

Allows an element that contains a value of null , but can have at most one null element.

Import Java.util.date;import Java.util.hashset;import java.util.Iterator; public class testhashset{public    static void Main (String [] args)    {       HashSet h=new HashSet ();       H.add ("1st");       H.add ("2nd");       H.add (New Integer (3));       H.add (new Double (4.0));       H.add ("2nd");            Duplicate element, not added       H.add (new Integer (3));      Duplicate element, not added       H.add (new Date ());       System.out.println ("Start: Size=" +h.size ());       Iterator it=h.iterator ();       while (It.hasnext ())       {           Object o=it.next ();           System.out.println (o);       }        H.remove ("2nd");       System.out.println ("After removing elements: size=" +h.size ());       System.out.println (h);    }}

  

2 , TreeSet

TreeSet describes a variant of set -a collection of functions such as sorting, which is automatically inserted into an ordered sequence of objects by a comparison rule when the object element is added to the collection, and guarantees that the collection element consists of a read Uixiangxulie the moment in ascending order.

Import Java.util.treeset;import java.util.Iterator; public class testtreeset{public    static void Main (String [] args)    {       TreeSet ts=new TreeSet ();       Ts.add ("Orange");       Ts.add ("Apple");       Ts.add ("banana");       Ts.add ("Grape");        Iterator it=ts.iterator ();       while (It.hasnext ())       {           string fruit= (String) it.next ();           SYSTEM.OUT.PRINTLN (fruit);}}}    

  

Introduction to the Java Collection set collection

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.