Set -- & gt; Summary

Source: Internet
Author: User

I. collection and data containers
1. Features of collections and arrays:
A set stores objects only. It is a container used to store objects. It can be variable in length. To store basic data, you must first convert it to an object.
An array can store objects, but also basic data. The length is immutable and is a container.
2. Java uses collection classes to accommodate different types of data. This kind of data is based on the unknown, that is, Java uses a limited collection class to accommodate infinite types of data objects.
3. Classification: Java Collection classes can be divided into three types: Set, list, And ing.
(1) Set: corresponds to the Mathematical Concept of "Set", which is the simplest Set.
The sequence Set does not distinguish the order of elements, so it does not record the order of adding elements.
Duplicate Set does not contain duplicate elements, that is, the two elements e1 and e2 both have e1.equals (e2) = false, and at most one null element.
(2) List)
The List distinguishes the order of elements. That is, the List can precisely control the insert position of each element. You can use an index (the position of an element in the List) to access the elements in the List. A List can contain repeated elements and can be operated by badge.
(3) ing: the key-Value Pair information is saved, that is, each element stored in the Map contains the "key" and "value" of the element. The corresponding "key" is not required for data search ", to find the "value" mapped to this "key ". Therefore, the Map set cannot contain duplicate "keys", and each "key" can only Map one value at most.
Ii. Collection Interface
Java. util. Collection interface is the most basic set interface and is the root interface. methods cannot be implemented and can only be implemented in subclasses. Defines the common methods for set operations.
Public booleanadd (E e): If an element is added to the set, true is returned. Otherwise, false is returned. This method is changed in the subinterface, when repeated elements are added to the set interface, false is rejected and returned. The list interface accepts repeated elements and returns true.
Public boolean addAll (Collection <? Extends E> c): Adds all elements in the specified set to this set.
Public voidclear (): removes all elements in this set (optional ).
Public booleancontains (Object o): returns true if the set contains the specified element.
Public booleancontainsAll (Collection <?> C): If the set contains all elements in the specified set, true is returned.
Public booleanequals (Object o): checks whether the set is equal to the specified Object.
Public inthashCode (): return the hash code value of this set.
Public booleanisEmpty (): returns true if this set does not contain elements.
PublicIterator <E> iterator (): The iterator that returns the elements in this set for iteration.
Public booleanremove (Object o): removes a single instance of a specified element from this collection. If so
Public booleanremoveAll (Collection <?> C): Remove all elements in the set that are also included in the specified set.
Public booleanretainAll (Collection <?> C): only the elements in this set that are also included in the specified set are retained.
Public intsize (): returns the number of elements in the set.
Public Object [] toArray (): returns an array of all elements in the set.
Note:
The Java SDK does not provide classes that directly inherit from the Collection. The classes provided by the Java SDK are the "subinterfaces" inherited from the Collection ".
The Collection interface has multiple sub-interfaces. The two most important java. util. Set and java. util. List describe the Set and List respectively.
Iii. List
The java. util. List interface describes the List structure. It allows programmers to precisely control the insert position of List elements and adds functions such as element indexing and element searching. On the basis of inheriting the Collection of the parent interface, the corresponding method added by the List interface is as follows:
Void add (intindex, E element) inserts a specified element at a specified position in the list
Boolean add (E) inserts a specified Element in the specified position of the List
E remove (intindex) deletes the specified index number Element
Booleanremove (Object o) deletes the first specified Element
E get (int index) returns the elements at the specified position in the list.
Int size () returns the number of elements in the list.
IntindexOf (Object obj) If a specified element is found, the index value is returned. Otherwise,-1 is returned.
List <E> subList (int fromIndex, int toIndex) returns some views between fromIndex (included) and toIndex (excluded) specified in the List.
Void clear () removes all elements from the list
The Java. util. List interface has multiple implementation classes, which respectively reflect different List differentiation forms.
ArrayList
Vector
Stack
Shortlist
Iv. Vector class: (the underlying data is an array, stored, and slow to search)
Constructor:
Vector (): Construct an empty Vector so that the size of its internal data array is 10, and its standard capacity increment is zero.
Vector (Collection <? Extends E> c): constructs a vector containing elements in the specified collection, which are arranged in the order returned by the iterator of the collection.
Vector (int initialCapacity): constructs an empty Vector using the specified initial capacity increment and the Capacity Increment equal to zero.
Vector (int initialCapacity, intcapacityIncrement): uses the specified initial capacity and capacity increment to construct an empty Vector.
Method:
Void addElement (E obj): adds an element to a Vector.
Void insertElementAt (E obj, int index): Add an element to the specified index.
Void setElementAt (E obj, int index): replace the specified element.
Boolean removeElement (Object obj): deletes a specified Object element.
VoidremoveElementAt (int index): deletes the elements of a specified index.
Void removeAllElements (): delete all elements E elementAt (int index)
Int size (): gets the length of the current Vector.
5. Advanced Stack (Stack)
Stack is a subclass of the Vector class. It has the following features: "Last In First Out" type container, that is, the Last object that is pushed into the Stack, the first pop will pop up.
You can only execute the action of popping up and pressing elements from the stack from the top of the stack.
Constructor
Stack (): used to create objects that support "post-import, first-out" Access
Method
E peek () returns the top element of the stack, but no top element of the stack is displayed.
E pop () pops up the top element of the stack and returns the objects in it.
E push (E item) pushes the item object to the top of the stack and returns the item object.
Boolean empty () determines whether the stack is empty. If the stack is empty, true is returned. Otherwise, false is returned.
Push and pop are two important methods, reflecting the important features of the stack: "first-in-first-out"
6. Sort list class
Constructor:
Empty list (): Construct an empty list.
Collections list (Collection <? Extends E> c): constructs a list of elements in the specified collection, which are arranged in the order returned by the collection iterator.
Method:
Void addFirst (E e): inserts the specified element at the beginning of the list.
Void addLast (E e): add the specified element to the end of the list.
E getFirst (): returns the first element in this list.
E getLast (): returns the last element of this list.
E removeFirst (): removes and returns the first element in this list.
E removeLast (): removes and returns the last element of this list.
Boolean offerFirst (E e): inserts the specified element at the beginning of this list.
Boolean offerLast (E e): inserts the specified element at the end of this list.
E peekFirst (): gets but does not remove the first element of this list. If this list is empty, null is returned.
E peekLast (): gets but does not remove the last element of this list. If this list is empty, null is returned.
E pollFirst (): gets and removes the first element in the list. If the list is empty, null is returned.
E pollLast (): gets and removes the last element in the list. If the list is empty, null is returned.
VII. ArrayList class:
Constructor:
ArrayList (): constructs an empty list with an initial capacity of 10.
ArrayList (Collection <? Extends E> c): constructs a list of elements that contain the specified collection. These elements are arranged in the order returned by the collection iterator.
ArrayList (int initialCapacity): constructs an empty list with the specified initial capacity.
Method:
Boolean add (E e): add the specified element to the end of the list.
Void add (int index, E element): inserts the specified element into the specified position in this list.
BooleanaddAll (Collection <? Extends E> c): Adds all elements in the collection to the end of the list according to the sequence returned by the iterator of the specified collection.
Boolean addAll (int index, Collection <? Extends E> c): inserts all elements in the specified collection into the list starting from the specified position.
Voidclear (): removes all elements from this list.
Object clone (): returns a superficial copy of The ArrayList instance.
Boolean contains (Object o): returns true if the list contains the specified element.
VoidensureCapacity (int minCapacity): if necessary, increase the capacity of this ArrayList instance to ensure that it can accommodate at least the number of elements specified by the minimum capacity parameter.
Eget (int index): returns the element at the specified position in this list.
IntindexOf (Object o): returns the index of the specified element that appears for the first time in this list, or-1 if this list does not contain elements.
Boolean isEmpty (): returns true if the list contains no elements.
IntlastIndexOf (Object o): returns the last index of the specified element in this list, or-1 if this list does not contain an index.
Eremove (int index): removes the elements at the specified position in this list.
Boolean remove (Object o): removes the specified element that appears for the first time in this list (if any ).
Protected void removeRange (int fromIndex, int toIndex): removes all elements in the list between fromIndex (included) and toIndex (excluded.
Eset (int index, E element): replaces the element at the specified position in the list with the specified element.
Intsize (): returns the number of elements in this list.
Object [] toArray (): returns an array containing all elements in the list in the appropriate order (from the first to the last element.
<T> T [] toArray (T [] a): returns an array containing all elements in the list in the appropriate order (from the first to the last element; the runtime type of the returned array is the runtime type of the specified array.
VoidtrimToSize (): Adjust the capacity of the ArrayList instance to the current size of the list.
HashSet that implements the Set Interface
The "hash-based" Method for Detecting duplicate elements: the element values in the HashSet correspond to the index locations of the elements stored in the Set (hash function ), before inserting an element in a HashSet, you can calculate the insert position of the element in the HashSet based on the value and corresponding relationship) if the value of the element to be inserted already exists, the element cannot be inserted.
Constructor
HashSet ()
HashSet (<E> c)
Other methods
Booleancontains (Object o) determines whether a specified element exists
Comprehensive application of the HashSet class.
Set <String> set = newHashSet <String> ();
Set. add ("One"); set. add ("One ");
System. out. println (set. size (); // number of output elements: 1
Set. add ("Two"); System. out. println (set. size (); // number of elements: 2
System. out. println (set. contains ("One"); // true, containing the element "One"
The first collection has a common feature: the objects they store are all mona1 (linear), but the storage method is different from the data structure used, the Collection class encapsulates basic operations such as insert and delete of linear tables.
Both the List interface and the Set interface are sub-interfaces of Collection.
Implement the List interface: stores data based on linear linked lists, such as Vector
Implement the Set interface: they do not allow repeated elements, such as HashSet.
In daily life, complex objects are often managed using indexes.
In Java, we create a "key-value" pair type object represented by Hashtable, "key"-index information, and "value"-information corresponding to the index value.
Why use the "key-value" pair data structure?
Cause: It facilitates information retrieval and increases the retrieval speed of information in a large amount of data.
Working Principle
(1) If you want to query the specified data from it, you have to traverse the array in sequence, which is very inefficient.
(2) Another Way of Thinking: saving 10 to an array is not inserted into the first free space.

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.