Java Learning Lesson 34th (Common Object API)-set FRAME (ii)-list collection and its subclass characteristics

Source: Internet
Author: User

The 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 ). Users of this interface can precisely control the insertion position of each element in the list. The user can access the element based on the integer index of the element (where it is located 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 Explanation: A collection that does not contain duplicate elements. Rather, the set contains no satisfied e1.equals(e2) element pairs e1 and e2 contains at most one null element. As its name implies, this interface mimics the mathematical set abstraction.

....

Use the corresponding container according to the problem


Second, list common methods

Because it is a subclass of collection, it only demonstrates its own method

1. Add void Add (index,element) void Add (index,collection) 2. Remove Object Remove (Index) 3. Modify 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) {//Add 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);//Modify (collection does not have modification)// 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 that you can manipulate the angle mark , and delete the change


Third, Listiterator interface: List-specific

The API documentation explains: series table iterators, which allow programmers to traverse the list in either direction, modify the list during iteration, and get the current position of the iterator in the list.

Listiterator iterators, which are the exception problems that occur when you are dealing with concurrent modifications to iterator iterations and collection operations


/p>

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 storage method//So 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 modifications to an object, but does not allow this modification. }else {System.out.println (object);}} *///FIX: Iteration and collection simultaneous access causes concurrency, so the collection operation is incessantly generation, and the iteration does not use the set operation System.out.println ("Old List:" +list); Listiterator iterator = List.listiterator ();//list-specific iterator that enables the completion of additions and deletions to elements in the iterative process/*listiterator (int index) Returns a list of 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 ());}}}

Four common sub-class features of list


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

API documentation explains: Vector classes can implement an array of objects that can grow . Like an array, it contains components that can be accessed using an integer index. However, Vector the size can be scaled up or down as needed to accommodate 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, including null , are allowed. 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. Implements all optional list operations, and allows 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 operations allow the link list to be used as a stack, queue, or double-ended queue.


Java Learning Lesson 34th (Common Object API)-set FRAME (ii)-list collection and its subclass characteristics

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.