Java Collection comparison ---- collection, java ---- Collection

Source: Internet
Author: User

Java Collection comparison ---- collection, java ---- Collection

I have never figured out the differences between various java collections before, so I will combine them here for comparison.

Collection and Map 

All java collections come from one of the Collection and Map interfaces.

The Collection interface is a common Collection interface.

The Map interface is a bit special. all its elements are stored in the form of Key-Value.

 

First, compare the Collection under the Collection, which has two categories, one list interface and one set interface. The biggest difference is that the set does not allow repeated elements.

List Interface

List inherits the Collection interface, which is an ordered set. There are four classes to implement this interface: ArrayList, sorted List, Stack, and Vector.

 

ArrayList:I am used to regard him as a dynamic array with a length that can automatically grow. It saves elements through an array named elementData, which is not thread-safe. If the size is not specified during initialization, the default value is 10. For each small auto-increment, I saw some blogs saying: new capacity = "(original capacity x3)/2 + 1", but I found that when I break a breakpoint in eclipse, it is: new capacity = "(original capacity x3)/2", rounded up. In the following code, 11 lines break points.

 1 public class test { 2      3     public static void main(String[] args){ 4          5         List<Integer> list = new ArrayList<Integer>(3); 6          7         for (int i = 0; i < 3; i++) { 8             list.add(i); 9         }10         11         for (int i = 0; i < 1; i++) {12             list.add(i);13         }14 15     } 16 }

 

We can see that the length of elementData is 3. When I add a data entry to the list,

Shortlist:It is stored as a linked list and a two-way linked list. It can be randomly accessed by get () and used as a stack, queue, and dual-end queue because it implements pop, push, peek, peekFrist, peekLast, poll, pollFrist, pollLast method, poll return header, and remove. Peek to return. It is not thread-safe.

 1 public class test { 2      3     public static void main(String[] args){ 4          5         LinkedList<Integer> list = new LinkedList<Integer>(); 6          7         for (int i = 0; i < 10; i++) { 8             list.push(i); 9         }10         11         System.out.println(list.size());//1012         list.poll();13         System.out.println(list.size());//914         list.peek();15         System.out.println(list.size());//916 17     } 18 }

 

ArrayList andSelection of consumer list:To add an element to an array, especially adding an element in the middle, you need to move other elements. This problem does not exist in the linked list. You only need to change the pointer address of the linked list. However, for random access, the linked list has a problem, that is, the data structure of the linked list determines that he can only access the table in sequence. Even if he can use get () for random access, he can find it one by one. Therefore, when selecting which one to use, you should consider whether the current set frequently changes the size and whether frequent random access is required.

Vector:It seems like ArrayList, but it is thread-safe.

Stack:The standard stack (FILO) inherits from the Vector, which is also implemented using arrays. The peek method is only available.

Thread security of the Set

First, Vector and Stack Are thread-safe, while ArrayList and worker list are not thread-safe. I use the code running result to prove that the idea is like this. I use two threads, and each thread adds 100 numbers to the list. So, according to the normal idea, the size list of the last list. size () is equal to 200.

 

1 public class test {2 3 // List <Integer> list = Collections. synchronizedList (new ArrayList <Integer> (); 4 // List <Integer> list = Collections. synchronizedList (new ArrayList <Integer> (); 5 // List <Integer> list = new ArrayList <Integer> (); 6 // List <Integer> list = new Vector <Integer> (); 7 List <Integer> list = new empty List <Integer> (); 8 // Stack <Integer> list = new Stack <Integer> (); 9 public static void main (String [] args) {10 test t = new test (); 11 12 ThreadA tA = t. new ThreadA (); 13 ThreadB tB = t. new ThreadB (); 14 // start thread 15 tA. start (); 16 tB. start (); 17 try {18 tA. join (); 19 tB. join (); 20} catch (InterruptedException e) {21 e. printStackTrace (); 22} 23 System. out. println ("list size:" + t. list. size (); 24} 25 class ThreadA extends Thread {26 @ Override27 public void run () {28 super. run (); 29 for (int I = 0; I <100; I ++) {30 list. add (I); 31 // list. push (I); 32} 33} 34} 35 class ThreadB extends Thread {36 @ Override37 public void run () {38 super. run (); 39 for (int I = 0; I <100; I ++) {40 list. add (I); 41 // list. push (I); 42} 43} 44} 45}

 

 

 

The result of the list type is ArrayList, no matter how many times is not equal to 200

Why is this happening in ArrayList?

No matter how it is executed, the size of the ArrayList will not exceed 200. There are two steps to add data to ArrayList: First add the data to the array, and then size ++. Assuming that the first data is added, size = 0, and ThreadA should add a value at 0, however, ThreadA is blocked and ThreadB is executed. Since ThreadA has not been able to execute size ++, the size is still equal to 0, so ThreadB should also add a value at 0, overwrite the value assigned by ThreadA, and ThreadB continues to execute size ++, size = 1. Then ThreadA continues to execute, size ++, size = 2; but in reality, my list only has one value, and size is equal to 1. Therefore, it can be inferred that the size of the ArrayList will not exceed 200 because ThreadA and ThreadB write data to a single location at the same time, resulting in data loss.

How can this problem be solved?

 

Like the third line of the code above, you can use the Collections. synchronizedList () function during initialization to turn ArrayList and history list into thread-safe sets.

Here I strongly recommend a blog, the collection is very detailed: http://www.cnblogs.com/skywang12345/p/3308513.html

 

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.