Dark Horse programmer _ Collection framework (1)

Source: Internet
Author: User
Tags addall set set

----- Java training, Android training, IOS training, and. Net training. We look forward to communicating with you!

Before introducing a set, let's talk about arrays. As we know, arrays are a collection of a certain type of data, which emphasizes data and must be single; the difference between the set framework is that there are no restrictions on the types of stored data. You can store basic data types (which are automatically upgraded to the corresponding packaging classes) or reference data types such as objects, the more powerful of the Collection framework is that it can store any type of data at the same time, which cannot be achieved by data. On the other hand, the length of the array is fixed, but most of the containers required in real life are uncertain, this requires a variable-length container, and the Collection framework just makes up for the fixed array capacity. This is another feature of the Collection framework. Another difference is the difference in storage methods, the array data structure is an array, which is a continuous area in the memory, and the Collection framework is distributed storage (different types are not the same, for example, it is similar to the arraylist of the array structure, the sorted list of the linked list structure, and the treeset of the red/black tree structure ).

Emphasize the learning method of learning to inherit or implement relational objects: view the function of the parent class, create a subclass object, and view the function of the parent class because the parent class is a common extraction of child classes, therefore, the parent class basically covers the important methods of sub-classes. The reason for creating sub-class objects is that the parent class may not be able to create objects such as abstract classes and interfaces, one reason is that the sub-class function is greater than the parent class function, so it is more convenient to use.

Collection System

Collection

-- | List subinterface, which is characterized by element order and can be repeated

-- | Vector: the underlying data structure is an array, which is thread-safe but slow and has been replaced by arraylist.

-- | Arraylist: the underlying data structure is an array. Feature: Fast query speed. However, adding, deleting, and deleting are slow. The thread is not synchronized.

-- | Linked list: the underlying linked list data structure. Features: Fast addition, deletion, and slow query. The thread is not synchronized.

-- | Set subinterface, which is characterized by unordered elements that cannot be duplicated

-- | Hashset: the underlying data structure is a hash table, which is thread-safe and not synchronized.

-- | Treeset: sorts the elements in the Set set (Red/black tree)

Common collection methods:

1. Add

Boolean add (Object OBJ): Add a single element

Boolean addall (collection C): Add Multiple Elements

2. Delete

Void clear (): removes all elements.

Boolean remove (Object O): removes a single element. If one element is deleted, true is returned.

Boolean removeall (collection C): removes multiple elements.

3. Judgment

Boolean contains (Object OBJ): determines whether an element exists.

Boolean containsall (collection C): determines whether an element of a set is included in the current set.

Boolean isempty (): determines whether the set is empty.

4. Get

Iterator (): returns an iterator on the set.

Int size (): number of elements

5. Obtain the intersection

Boolean retainall (collection C): intersection

6. Changing the array of Sets

Object [] toarray ()

Special instructions:

Iterator

The implementation of the iterator depends on the internal class. As we know, the human body, including the heart, liver, spleen, kidney, and lung, is an independent unit inside the human body, but cannot be separated from the human body, therefore, when describing the human body, the heart, liver, spleen, kidney, and lung are defined as internal classes of the human body. When operating on the elements of a collection framework, how to operate on different data structures has different implementations, and these different implementations depend on the Collection framework, therefore, the iterator is defined as an internal class of the Collection framework, so that the elements in the collection can be directly accessed through the collection object.

Method of the iterator:

Boolean hasnext () determines whether there are any elements that can be iterated

Next () returns the next element of the iteration. The retrieved element is the object class by default.

Note:

1. The iterator is common in the collcection interface and replaces enumeration (enumeration) in the vector class)

2. The next () method of the iterator is to automatically take down elements. Avoid nosuchelementexception.

3. the type returned by the next method of the iterator is object, so remember to convert the type.

Special Functions of the List interface:

1. Add Elements

Void add (INT index, object element): add the specified element at the specified position

Boolean addall (index I, collection <? Extends E> C)

2. delete an element

Object remove (INT index): returns and deletes the elements at the specified position.

3. modify elements

Object set (INT index, object element): modifies the elements at the specified position.

4. Search for elements

Object get (INT index): obtains elements based on the specified index.

List <E> sublist (INT fromindex, int toindex) returns some views between fromindex (included) and toindex (excluded) specified in the list.

Int indexof (Object O) returns the index of the specified element that appears for the first time in this list. If not included,-1 is returned.

Listiterator () List-specific iterator

List-specific iterator

Listiterator <E> listiterator (): returns an iterator that iterates over the elements in the list in an appropriate order.

Unique Method of list iterator:

Void add (E) inserts the specified element into the list.

Boolean hasprevious () returns true if the list iterator contains multiple elements in reverse traversal.

Int nextindex () returns the index of the elements returned by subsequent calls to next.

E Previous () returns the previous element in the list.

Int previusindex () returns the index of the elements returned by subsequent calls to previous

Void set (e) replaces the last element returned by next or previous with the specified Element

Void remove () removes the last element returned by next or previous from the list.

Listiterator description:

In the iterator, only the method of the iterator can be used to operate elements, but the iterator method is limited. Only the elements can be judged, retrieved, or deleted, if you want to add or modify other operations, you need to use the list-specific iterator:Listiterator.

If a public iterator is used, it occurs when the iteration element is modified.ConcurrentmodificationexceptionException

Vector

Note: The underlying data structure is array, thread synchronization, and low efficiency have been replaced by arraylist

Features: Fast query and slow addition/Deletion

Unique Method of vector:

1. Add Elements

Void addelement (Object OBJ) after jdk1.2 is changed to add (Object OBJ)

2. Get Elements

Object elementat (INT index) jdk1.2 and then changed to get (INT index ).

3. Traverse Elements

Enumeration elements ()

Boolean hasmoreelements ()

Object nextelement ()

Enumeration elements ()

Enumeration is enumeration, and enumeration is a unique method of getting out a vector.

Arraylist

Note: The underlying data structure uses an array structure.

Feature: Fast query speed. However, adding, deleting, and deleting are slow. The thread is not synchronized.

Linklist

Description: The linked list data structure used at the underlying layer.

Features: Fast addition/deletion, slow query, and non-synchronization of threads.

Special linklist method:

1. Add elements:

Void addfirst (E) inserts the specified element at the beginning of the list.

Void addlast (E) adds the specified element to the end of this list.

2. Get elements:

E getfirst () returns the first element in this list.

E getlast () returns the last element of this list.

3. Delete elements:

E removefirst () removes and returns the first element in this list.

E removelast () removes and returns the last element of this list.

Dark Horse programmer _ Collection framework (1)

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.