Java Collection Framework Learning (a) List

Source: Internet
Author: User

Attach a Java collection frame diagram first.

As you can see from the frameset above, the Java Collection Framework consists of two types of containers, one set (Collection), one collection of elements, the other is a graph (map), and a key/value pair mapping is stored. Collection interface also has 3 seed types, List, set and queue, and then the following are some abstract classes, and finally the implementation of specific classes, commonly used are ArrayList, LinkedList, HashSet, Linkedhashset, HashMap, Linkedhashmap and so on. (PS: With a dashed border is not directly used by the interface) collection interface

The collection interface is the root interface that handles the collection of objects, which defines a number of methods for manipulating elements, and abstractcollection is an abstract class that provides collection partial implementations. Shows all the methods in the collection interface.


Collection Interface Structure

Among them, there are several more commonly used methods, such as the method Add () adds an element to the collection, AddAll () adds all the elements in the specified collection to the collection, the contains () method detects whether the collection contains the specified element, and the ToArray () method returns an array representing the collection. The collection interface has three sub-interfaces, which are described in detail below.

1.List

The list interface is extended from collection, which can define an ordered set of allowed duplicates, from the list interface method, the list interface mainly increases the position-oriented operation, allowing the element to be manipulated at the specified location. It also adds a new list iterator listiterator that can traverse the linear table in both directions. The Abstractlist class provides a partial implementation of the list interface, Abstractsequentiallist extended from Abstractlist, primarily to provide support for linked lists. The following is a list of the two important implementation classes of the interface, but also we may be the most commonly used classes, ArrayList and LinkedList.

ArrayList

By reading the source code of the ArrayList, we can clearly see the logic inside, which is stored with an array of elements, the array can be created dynamically, if the number of elements exceeds the capacity of the array, then create a larger new array, and all elements in the current array are copied into the new array. Assuming that the first is a collection without any elements, here is an example of inserting an element to see the implementation of the source code.

1、方法add(E e)向集合中添加指定元素。
1  Public Boolean Add (e e) {2         ensurecapacityinternal (size + 1);  // increments modcount!! 3         elementdata[size++] = e; 4         return true ; 5     }
2、此方法主要是确定将要创建的数组大小。
1 Private voidEnsurecapacityinternal (intmincapacity) {2         if(Elementdata = =defaultcapacity_empty_elementdata) {3Mincapacity =Math.max (default_capacity, mincapacity);4         }5 6 ensureexplicitcapacity (mincapacity);7     }8 9     Private voidEnsureexplicitcapacity (intmincapacity) {Tenmodcount++; One         if(Mincapacity-elementdata.length > 0) A grow (mincapacity); -}
3、最后是创建数组,可以明显的看到先是确定了添加元素后的大小之后将元素复制到新数组中。
1 Private voidGrowintmincapacity) {2         //overflow-conscious Code3         intOldcapacity =elementdata.length;4         intNewcapacity = oldcapacity + (oldcapacity >> 1);5         if(Newcapacity-mincapacity < 0)6Newcapacity =mincapacity;7         if(Newcapacity-max_array_size > 0)8Newcapacity =hugecapacity (mincapacity);9         //mincapacity is usually close to size, so this is a win:TenElementdata =arrays.copyof (Elementdata, newcapacity); One}

LinkedList

Again, we open the source file of LinkedList, it is not difficult to see that linkedlist is storing elements in a linked list.

As we learn about data structures, we know that the biggest difference between linked lists and arrays is that they are different in how they are stored, resulting in different efficiencies in different operations on the data, as well as ArrayList and LinkedList. In actual use we need to choose the appropriate class according to the specific needs, if the other than at the end can not insert or delete elements, then ArrayList more efficient, if you need to insert or delete elements frequently, select LinkedList.

Vector

In usage, vectors are basically consistent with ArrayList, except that vectors use the keyword synchronized to synchronize the methods of accessing and modifying vectors, so for applications that do not need to be synchronized, Class ArrayList are more efficient than class vectors. (synchronized keyword )

Reference:

http://www.jianshu.com/p/63e76826e852

Java Collection Framework Learning (a) List

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.