Dark Horse Programmer-java Basics-Set frame-collection, List, set

Source: Internet
Author: User
Tags addall set set

First Lecture: System overview

I. Overview

1. Function: Container for storing objects

2, features: Only for storing objects, set length variable, set can store different types of objects;

3, and the difference between the array: Although the data can also store objects, but the length of the array is fixed, and the set length is variable, the emergence of the collection class can solve the real life of the problem of uncertainty;

4. Common Collection class and structure

Second Lecture: Common methods

The collection implements the collection interface, and the collection interface declares the following common methods:

1) Adding elements to the collection: Add (Object obj), AddAll ()

The Add () method adds an object to the collection, and instead of actually adding the object itself, the address of the object is added to the collection. As follows: The Hello_1, Hello_2, Hello_3, three string objects in the heap memory are added to the Ar1 collection.

Summarize:

    • The argument to the Add (object obj) method is an object that is used to receive arbitrary types of objects;
    • The collection is stored in the object's reference or address;

2) Delete the elements in the collection: Remove (Object obj)

3) Determine if the element exists in the collection

4) Take the intersection of two sets: Retainall (Collection c)

Ar1.retainall (AR2); Gets the common elements in AR1 and AR2, and eventually assigns them to AR1.

5) Remove intersection: RemoveAll (), remove common elements from two sets

Ar1.removeall (AR2); Remove the same elements in the AR1 as the ar2.

Third Lecture: iterators

concept: the way in which an element in a collection is defined inside a set;

The reason for defining an iterator to take the elements in the collection: different sets, the structure of the data is different, the action details are different, therefore, can not simply define a function to read the elements in the collection, we need to define different elements according to the characteristics of different sets of element reading methods, These methods are encapsulated in the inner class of the collection. When we need to invoke the elements of a collection, we simply invoke these methods with an inner class object, and the collection interface provides a way to get an inner class object from the collection that inherits from the collection interface: iterator ().

Ar1.iterator ()--Gets the iterator for the collection ar1.

Iterator () attribute: The method returns an iterator (Iterator<e> interface) object,iterator<e> interface that declares three methods:

    • Boolean Hasnext ()--determines if there are still elements in the set that can iterate, returns True, otherwise returns false;

Ar1.hasnext ()--Determines whether elements and iterations are still in the set AR1.

    • E Next ()-returns the next element of the iteration, which is the next element of the collection;

Ar1.next ()--Returns the next element of the ar1 iteration.

    • Void Remove ()-Removes the last element returned by the iterator from the collection pointed to by the iterator (operable).

Fourth Lecture: List Set Commonality Method

Both list and set are classes under collection, and the differences are as follows:

    • List: Elements are ordered and elements can be duplicated because the set system has an index ;
    • Set: Elements are unordered and elements cannot be duplicated ;

List characteristics: All the methods that can operate the angle mark are the methods peculiar to the system.

List Method:

1) Increase

Add (index,element)--Adds an element at the specified location

AddAll (index,collection)--Adding an element set at a specified location

2) by deleting

Remove (index)--Deletes the element at the specified position

3) Change

Set (index,element)--replaces the specified position element with elements

4) Check

Get (Index)--Gets the element at the specified position;

Sublist (from,to)--Gets the element of the specified position interval

Listiterator ()--using iterators to get elements

Considerations for using Iterators

During an iteration, when you add or delete a collection element, you cannot add or delete a collection element by using both iterators and collection references, and the following code appears with an exception:

The correct wording is as follows:

Fifth Lecture: Listiterator

Overview: Listiterator is a unique iterator to the list collection and is a sub-interface of the iterator.

Listiterator The origin of: at the time of iteration, it is also not possible to manipulate the elements in the collection by the method of the collection object, because the concurrentmodificationexception exception occurs. So, in the iteration can only use an iterator to manipulate the elements, but the method in the iterator is limited, only the elements in the collection to judge, remove, delete operations, if you want to do other operations, such as add, modify, etc., you need to define the iterator sub-interface, and add the required method to the interface, the self-interface is listiterator.

Get method: This interface can only be obtained through the Listiterator () method of the list collection. Examples of usage are as follows:

Sixth Lecture: List characteristics of a collection of specific objects

ArrayList : The underlying data structure uses the structure of the array, features: Fast query speed, but the deletion is slightly slower, the thread is not synchronized, but Java gives a convenient solution;

LinkedList : The underlying use of the linked list data structure, features: Deletion speed quickly, but query slightly slow;

Vector : The bottom layer is the array data structure, thread synchronization, has been replaced by ArrayList.

Seventh Lecture: Vector the enumeration in

Vector has a special method of extracting elements: Enumeration Method Enumeration

Enumerations are like iterators, and their functions are repetitive.

Because the name of the enumeration method and the method name are too long, it is replaced by the subsequent iterator. The enumeration method reads the element method as follows:

Eighth Lecture: LinkedList

1, the unique method of LinkedList:

added:addfirst (), AddLast ()

Get:GetFirst (), GetLast ()--Gets the element, but does not delete the element. Nosuchelementexception exception occurs if there are no elements in the collection

Delete:Removefirst (), Removelast ()--Gets the element, but the element is deleted. Nosuchelementexception exception occurs if there are no elements in the collection

2, JDK1.6 an alternative method, you can avoid the collection because there are no elements of the exception

added:offerfirst (), Offerlast ()

gets:Peekfirst (), Peeklast ()--Gets the element, but the element is not deleted. If there are no elements in the collection, NULL is returned and no exception is thrown;

Delete:Pollfirst (), Peeklast ()--Gets the element, but the element is deleted. If there are no elements in the collection, NULL is returned and no exception is thrown;

Exercise: Using LinkedList simulating a stack or queue data structure

Stack: Advanced after out, like a cup

Queue: FIFO, like a water pipe

idea: using the LinkedList data structure characteristic, encapsulates one conforms to own request the database construction, this data structure provides the addition, obtains, deletes and so on outside the operation.

Exercise One: Remove ArrayList repeating elements in a collection

Idea: Create a new collection, read the element from the old collection and add it to the new collection, and assign the new collection address to the old collection, before adding it to determine whether the element is already in the new collection and, if so, not added;

Exercise Two: Save the custom object as an element to ArrayList collection, and removes the repeating element

1, Ideas:

1) Describe an object that encapsulates the data into this object, taking human as an example;

2) define a container to deposit the person;

3) Remove duplicates-the same person whose name and age are the same;

2. Realize the following:

3. Precautions:

1) The Remove (object obj) method in the collection is implemented by traversing the collection to find the object to be deleted, and the procedure calls the Equals method to determine whether the element in the collection is to be deleted, so you need to override equals in the actual case.

2) The implementation principle of the CONTAINS (Object obj) method in the collection is to iterate over the collection and invoke the Equals method to determine whether the elements in the collection exist with the same elements as obj, so you need to rewrite equals according to the actual situation.

Section A speak HashSet

Set : elements are unordered (the order in which they are deposited and taken out is not necessarily consistent), and elements cannot be duplicated. When adding elements, determine whether the collection already exists with this object address consistent, if present, compare their values are consistent, if consistent, do not add this element, if not consistent, add a new object under the same address value.

The functions and collection of the set set are consistent.

1. HashSet

The underlying data structure is a hash table. HashSet methods to guarantee element uniqueness are as follows:

Through the elements of the two methods, hashcode and equals to complete. If the hashcode value of the element is the same, the Equals is true, and equals is not called if the element's Hashcode value is different.

Therefore, when customizing objects, it is often necessary to override the hashcode and Equals methods, overriding the following example:

Note: For the determination of whether an element exists, as well as the operation, HashSet relies on the hashcode of the element and the equals;arraylist dependent on only equals.

Summary of small knowledge points

1. The collection has the method of adding, deleting, changing and checking the elements of the set, and different sets of sub-classes have their own different operation methods. The unique methods of the list are: Add (index,element), remove (index), Set (index,element), Get (index), and so on.

2. iterators

Iterators are like a collection of connections and external tools for externally reading elements inside a collection.

It encapsulates the concrete details of the read element into the inside of the collection, while providing an externally common operation, that is, to get the iterator object and invoke the method to read the elements in the collection.

3. There are three ways to read the elements in the collection:

1) Use the Remove () method to iterate through the elements in the collection

2) using iterators to read elements

3) Use the enumeration method to read the elements in the set-vector only

4, the use of ArrayList or LinkedList scenario judgment

It is recommended to use ArrayList when you need to query elements frequently in your application, but when you have fewer additions and deletions, you are advised to use LinkedList when you need frequent additions and deletions, but less query elements; ArrayList is recommended when you want to query and delete elements frequently.

5. Custom Data structure

We can use the existing data structures to customize the structure of the structures that we need, and to customize the method of the structure to limit or extend the characteristics of the structure. such as customizing the data structure of the increase, deletion, change, check and other functions.

Dark Horse Programmer-java Basics-Set frame-collection, List, set

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.