Java Learning Lesson 34th (often using the object API)-list collection and its sub-class features

Source: Internet
Author: User

List (list) and set (set) are most commonly used throughout the collection framework


I. Features of list set && set

Collection Sub-interfaces:

1, List: ordered (in the same order of deposit and withdrawal), the elements are indexed and can be repeated

The API documentation explains: ordered collection (also known as sequences ). The user of this interface is able to precisely control the insertion position of each element in the list. The user can access elements based on the integer index of the element (in the list) and search for the elements in the list.

2, Set: elements can not be repeated, disorderly, there may be orderly

API documentation explains: A collection that does not include repeated elements. Rather, the set does not include the pair of elements that are satisfied e1.equals(e2) e1 and e2 includes at most one null element. As its name implies, this interface mimics the mathematical set abstraction.

....

Need to use the appropriate container according to the problem


Second, list common methods

Because it is a subclass of collection, it only demonstrates its unique approach

1. Add void Add (index,element) void Add (index,collection) 2. Remove Object Remove (Index) 3. Change the object Set (Index,element) 4. Get Object Get (Index) int indexOf (object) int LastIndexOf (object) List sublist (from,to)//with head, without tail


Code Demo

Import Java.util.arraylist;import Java.util.list;public class Main {public static void main (string[] args) {List List = NE W ArrayList ();//Note the list of packages do not import errors, java.awt also have a listshow (list);} public static void Show (List list) {//Join List.add ("A1"); List.add ("A2"); List.add ("A3"); System.out.println ("List =" +list);//add Element//list.add (2, "a250");//delete element//list.remove (2);//Change (collection not change)// List.set (2, "A25");//Get//list.get (1);//system.out.println ("list =" +list.get (1));//Get sub-lists List LL = List.sublist (0, 2) ;//With head, not including tail System.out.println ("ZI List =" +ll);}}

From the above, the unique common method of list has a commonality is to be able to manipulate the corner mark , and delete and change


Third, Listiterator interface: List-specific

API documentation explained: Series table iterator, consent program the ape traverses the list in either direction, changes the list during iteration, and obtains the iterator's current position in the list.

Listiterator iterators, which are the exception problems that occur when you are dealing with concurrency changes in iterator iterations and collection operations


Import Java.util.arraylist;import java.util.iterator;import Java.util.list;import java.util.listiterator;public Class Main {public static void Main (string[] args) {List List = new ArrayList ();//show (list); Listiteratordemo (list);} public static void Listiteratordemo (List list) {List.add ("A1"); List.add ("A2"); List.add ("A3");//iterator it = List.iterator ();//At this time it only knows the current state of the list of the internal data structure of the storage method//The following code will throw an exception, because later the list structure changes, it does not know/*while (It.hasnext ()) {Object Object = It.next (), if (Object.Equals ("A2")) {List.add ("a250");//concurrentmodificationexception// The reason for this exception is that this exception is thrown when the method detects concurrent changes to an object but does not agree to such a change. }else {System.out.println (object);}} *///Solution: Iteration and collection at the same time the access caused the concurrency, so the collection operation when the incessantly generation, iterative time does not use the set operation System.out.println ("Old List:" +list); Listiterator iterator = List.listiterator ();//list-specific iterator, capable of adding additions and deletions to elements in the iterative process/*listiterator (int index) Returns a list of the elements in the list Iterators (in the appropriate order), starting at the specified position in the list. */while (Iterator.hasnext ()) {Object object = Iterator.next (), if (Object.Equals ("A2")) {Iterator.add ("a250");// Note that this is not a list, but an iterator used to avoid concurrent}}SYSTEM.OUT.PRIntln ("New List:" +list);//system.out.println ("Hasnext?") +iterator.hasnext ());//system.out.println ("Hasprevious?"); +iterator.hasprevious ()); while (Iterator.hasprevious ()) {System.out.println (iterator.previous ());}}}

Iv. list often uses sub-class features


1.Vector: internal array structure, and synchronization, adding and deleting slow, query slow

API documentation explains: Vector classes are able to implement an array of objects that can grow . Like an array, it includes components that can be interviewed using an integer index. However, Vector the size can be scaled up or down to suit the Vector action of adding or removing items after creation.

2.ArrayList: The interior is an array structure, the asynchronous substitution vector, query fast

API documentation explains: The implementation of a variable array of size for the List interface. All optional list operations are implemented and all elements that contain null are agreed. In addition to implementing the list interface, this class provides methods to manipulate the size of the array used internally to store the list. (This class is roughly equivalent to the Vector class, except that this class is not synchronized.) )

3.LinkedList: internal chain storage structure, non-synchronous, and delete the speed quickly

API documentation explains: The list interface is implemented as a link listing. Implement all optional list operations, and agree to all elements (including null). In addition to implementing the list interface, theLinkedList class provides a uniform naming method for the get,remove , and insert elements at the beginning and end of the list. These actions agree to use the link list as a stack, queue, or double-ended queue.


Java Learning Lesson 34th (often using the object API)-list collection and its sub-class features

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.