A big wave of Java Attack (eight) list interface for--java collection

Source: Internet
Author: User
Tags set set

This paper mainly describes one of the collection interface and its implementation class.

List collection, features: Ordered, repeatable


Unlike set above, the list collection is ordered, which is expressed in the form of an index that records the order in which elements are added.

The implementation class for the list interface:

1. Does the ArrayList have anything to do with arrays?           

2.Vector

3.LinkedList

In the previous study of set set, we learned that Linkedhashset is linked by the elements stored in the hash algorithm in the form of a linked list, is it not that the list collection with the index is also connected by the form of a linked list?

See, realize the Deque interface, guess should realize the effect of two-way queue.


Wait for me to slowly ...

One, list interface and Listiterator interface

The list interface is characterized by an "index" that records the order in which elements are added. Therefore, compared to the parent interface collection, some methods for manipulating the collection elements based on the index are added.

Added a method to specifically manipulate the list: Listiterator ()

Listiterator vs. iterator:Listiterator adds functionality to the forward iteration (iterator can only iterate backwards), Added adding elements to the list collection via the Add method (iterator can only delete elements)

public class Testlistiterator {public static void main (string[] args) {string[] books = {"Englishbook", "Computerbook", "Mu Sicbook "}; List Booklist = new ArrayList (), for (int i = 0; i < books.length; i++) {Booklist.add (books[i]);} Listiterator booklistiterator = Booklist.listiterator (); while (Booklistiterator.hasnext ()) {System.out.println (" Book Forward Iteration: "+booklistiterator.next ()"); Booklistiterator.add ("---------delimiter--------------");} SYSTEM.OUT.PRINTLN (Start reverse Iteration----------------under-------); while (Booklistiterator.hasprevious ()) {System.out.println ("Book Reverse Iteration:" +booklistiterator.previous ());}}}

The results are as follows:


Second, ArrayList and vector implementation classes

1. The same point:

is a list class based on an array implementation, so encapsulates a dynamically redistributed object[] array. The length is represented by the attribute capacity, and it grows automatically.

If you add a large number of elements, you can use ensurecapacity to add capacity at once. Even the size of the capacity is directly specified to reduce the number of reallocation and improve performance.

2. Different points:

simply understand the function of replacing the old vector with the new ArrayList.

ArrayList is thread insecure, vector thread safe (high performance, but not recommended)

Stack is a subclass of vector, simulating "stack" data structure, LIFO.

LinkedList List class based on linked list, optimize sequential access, especially: Insert, delete, very fast. (Simultaneous implementation of Deque interface, bidirectional queue)

Third, fixed-length list

Tool class for manipulating Data Arrays.aslist (...) Converts an array to a list collection "an instance of the arrays inner class ArrayList "

Note: The length is fixed at this time! Cannot be changed

Four, performance ranking list

The performance comparison of each container depends primarily on the underlying implementation of the collection, which is: array / Linked list

Insert, Delete, iterate : Array < linked list.

random access : Arrays > linked lists. (Array, contiguous memory area)

A big wave of Java Attack (eight) list interface for--java collection

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.