Introduction to the set of Java collections

Source: Internet
Author: User

A. Set set
Set: A collection that does not contain duplicate elements. More formally, the collection does not contain a pair of elements E1 and E2, making E1.equals (E2), and at most one empty element. As its name implies, this interface simulates the abstraction of a mathematical set.
Some collection implementations have restrictions on the elements they may contain. For example, some implementations prohibit empty elements, and some implementations have restrictions on the types of elements. Attempting to add an unqualified element throws an unchecked exception, typically nullpointerexception or classcastexception. Attempting to query for the presence of unqualified elements may throw an exception, or it may simply return false; Some implementations will show the former behavior, and some implementations will show the latter. More generally, attempting an operation on an element that does not meet the criteria does not result in the unqualified element being inserted into the collection, which may cause an exception, or the option may be successfully executed. This exception is marked "optional" in the specification of this interface.
Two. Differences between set set and list collection
The set set is unordered, its value cannot be duplicated, and list is an ordered set with values that can be duplicated.
The disorder here is that the order of the storage is different from the order taken out, but it is the same order of each fetch, not the order of each fetch, the disorder here is different from the concept of the dictionary unordered in Python, and the order in the Python dictionary refers to the sequence in which the key value is removed each time.
Three. Common methods of Set

return value type Method Name Notes
Boolean Add (e E) If the specified element does not exist, add its specified element (optional action).
Boolean AddAll (collection<? extends e> c) Adds all the elements in the specified collection to this collection if they do not already exist (optional).
void Clear () Removes all elements from this collection (optional action).
Boolean Contains (Object o) Returns True if this collection contains the specified element.
Boolean Containsall (collection<?> c) Returns True if this collection contains all the elements of the specified collection.
Boolean Equals (Object o) Compares the specified object to this collection to achieve equality.
Int Hashcode () Returns the hash code value for this collection.
Boolean IsEmpty () Returns True if this collection does not contain elements.
Iterator<e> Iterator () Returns an iterator for the elements in this collection.
Boolean Remove (Object o) If present, removes the specified element (optional action) from the collection.
Boolean RemoveAll (collection<?> c) Removes all elements contained in the specified collection from this collection (optional operation).
Boolean Retainall (collection<?> c) Retains only the elements in the collection that are contained in the specified collection (optional operations).
Int Size () Returns the number of elements in this collection (its cardinality).
Default spliterator<e> Spliterator () Creates a spliterator on the elements in this collection.
Object[] ToArray () Returns an array that contains all the elements in this collection.
<T> t[] ToArray (t[] a) Returns an array containing all the elements in this collection; The run-time type of the returned array is the run-time type of the specified array.

Four. Set common sub-class objects:
HashSet
TreeSet

Introduction to the set of Java collections

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.