---refactoring design of the Java Collection framework

Source: Internet
Author: User

Interface-Oriented Programming:

Interface type variable =new implementation class ();

List list=new ArrayList ();

---------------------------------------

Characteristics and performance analysis of List implementation class

--------------------------------------

The common characteristics of the three and the following norms:

1): Allow element repetition;

2): Record the order in which elements are added successively

--------------------------------------

Vector class:

The bottom of the array structure algorithm, methods are used synchronized decoration, thread safety, but the equivalent of ArrayList lower;

ArrayList class:

The bottom layer has an array structure algorithm, the method does not use synchronized decoration, thread is unsafe, the equivalent of vector higher.

Arraylis now basically has replaced the status of Vector.

To ensure Arraylist thread safety, List lsit =collections.synchronizedlist (New Arraylist (...));

linklist class:

The underlying only has a doubly linked list structure algorithm, the method does not use synchronized adornment, thread is unsafe.

The performance problems of array structure algorithm and doubly linked list structure algorithm:

Array structure algorithm: Low insertion and deletion operations, high query and change;

Linked list structure algorithm: Insert and delete operation is fast, query and change is slow;

---------------------------------------------------------------------------

Use the selection:
Vector class Kill no use! Even if you want to use it, use the ArrayList class.

If the insert and delete operations are frequent, the Linklist class should be used;

If the query and change operations are frequent, the ArrayList class should be used;

In the development. More use of ArrayList class, according to the specific needs of the environment to choose;

 Public Static voidMain (string[] args) {List List=NewArrayList (); List.add ("B"); List.add (C); List.add ("D"); List.add (E); //mode 1:for Cycle     for(intindex = 0; Index < list.size (); index++) {Object rest= List.get (index);//removes the element at the specified index positionSystem.out.println (rest); } System.out.println ("---------------------------------------------------------------------------"); //mode 2:for-each enhanced for loop     for(Object ele:list) {System.out.println (ele); } System.out.println ("---------------------------------------------------------------------------"); //Method 3: Use the while loop to manipulate the iterator iteratorIterator it=List.iterator ();  while(It.hasnext ()) {System.out.println (It.next ()); } System.out.println ("---------------------------------------------------------------------------"); //Method 4: Use the For loop to manipulate the iterator iterator     for(Iterator it2=list.iterator (); It2.hasnext ();)    {System.out.println (It2.next ()); } System.out.println (list);}



---refactoring design of the Java Collection framework

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.