Definition and considerations of list in Java set framework

Source: Internet
Author: User

As you know, the set framework is a unified standard architecture for representing and manipulating a set, and learning set knowledge helps us to solve a series of problems, such as saving data and objects.

The common set defines two major interfaces in the system, list and set

Here we will discuss the definition of list and some common problems

The list defines an ordered set of data that can be duplicated, so let's take a look at the following code:

Import java.util.ArrayList;
    
Import java.util.List;
    
Publicclass listtest{
    
    publicstaticvoid Main (String args[]) {listtest lt=new listtest
    
       ();
    
       List list=new ArrayList ();
    
       for (int i=0;i<5;i++) {
    
           list.add (i);
    
       }
    
       for (int i=0;i<list.size (); i++) {
    
           System.out.println (List.get (i));}}}
    

This code can be run in eclipse, but when we compile this code at cmd (Command prompt) we find that this text is displayed

Note: Listtest.java is using an unchecked or unsafe operation.

Note: For more information, please recompile using-xlint:unchecked.

This means that we do not specify a type at the time of the list definition, and Eclipse automatically specifies generics for the list at the time of execution, so there is no error at compile time, and in order to solve such errors, we should define

List<object> list=newarraylist<object> ();

The order here is not to say that after the list is defined, the elements inside are sorted in order, but in the order in which they are inserted into the list. In the list definition, we want to use the implementation class of the list, which has shown the implementation of ArrayList, where the ArrayList is like the array we are familiar with, including inserting the array, adding elements, and so on, and it should be noted that Because ArrayList uses linear operations, from a performance standpoint, in many implementations, they perform high overhead linear searches.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

The construction method in Arrarlist is interesting to say, ArrayList () is an empty list with an initial capacity of 10. But if we do not add elements to the ArrayList, the following:

Import java.util.ArrayList;
    
Import java.util.List;
    
Publicclass listtest{
    
    publicstaticvoid Main (String args[]) {
    
       list<object> list=new arraylist<object > ();
    
       System.out.println (List.size ());
    
    }
    

We will find that the output list length is not 10, but 0, because the size method returns the number of elements in the list, and we do not add elements to the list, so the length is 0.

In order to solve the time complexity of the queue reordering operation in ArrayList, we can define linkedlist to implement the list method, and when a new node is inserted, only the reference to the node in which the relationship is maintained is changed. The same is the case when records are deleted, and all actions are performed according to the need for a doubly linked list. Indexing in a list traverses the list from the beginning or end (from one end of the specified index).

Note that this implementation is not synchronized. If more than one thread accesses a list of links at the same time, and at least one of the threads has modified the list structurally, it must maintain an external synchronization.

So the list that is implemented is not accessible, although the get () method is provided, but this method is positioned by traversing the node, so the speed is slow.

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.