Java Collection class List Set MAP thread Safety

Source: Internet
Author: User
Tags set set

The collections in Java consist of three main classes, which are set, list, and map, all in the Java.util package, and set, list, and map are interfaces that have their own implementation classes.
(1) The implementation class of list mainly has Arraylist,linkedlist,vector
(2) The implementation classes of set are mainly HashSet and TreeSet
(3) The implementation classes of map are mainly hashmap and treemap,hashtable;
Explain:
(1) List,set are inherited from the collection interface, map is not

(2) The objects in the list are sorted by index, and can have duplicate objects that allow objects to be retrieved by the object at the index in the collection, such as by List.get (i) to get the elements in the list collection.

(3) Objects in set are not sorted in a particular way, and there are no duplicate objects. However, some of its implementation classes can sort the objects in the collection in a particular way, such as the TreeSet class, which can be sorted by default, or by implementing the Java.util.comparator<type> interface to customize the ordering method.
  
(4) Each element in a map contains a key object and a value object, which appear in pairs. Key objects cannot be duplicated, and value objects can be duplicated.

Thread-Safe classes

In the collection framework, some of the classes are thread-safe, and these are all occurrences of jdk1.1. After the jdk1.2, there are many non-thread-safe classes. The following are the thread-safe classes of synchronization:

Vector: It's more of a synchronization mechanism (thread-safe) than ArrayList, because it's less efficient and it's not recommended anymore. In the Web application, especially the foreground page, the efficiency (page response speed) is often preferred.

STATCK: Stack class, advanced post-out

Hashtable: It's a thread-safe more than HashMap.

Enumeration: enumeration, equivalent to an iterator

In addition to these, the others are non-thread-safe classes and interfaces.

Thread-safe classes whose methods are synchronous, can only have one access at a time. is a heavyweight object with low efficiency.

Other:

1. The difference between Hashtable and HashMap

Hashtable is thread-safe, that is, the Hashtable method provides a synchronization mechanism, HASHMAP is not thread-safe, that is, does not provide a synchronization mechanism; Hashtable does not allow null values to be inserted, hashmap allow!

2. How to modify a collection with multithreading concurrency

With the old Vector/hashtable class

StringBuffer is thread safe, and StringBuilder is thread insecure. For security and insecurity without deep understanding of the situation, it is easy to create such an illusion, if the operation of StringBuffer is thread-safe, however, Java gives you the assurance of thread safety, is to say that its method is to execute is exclusive of it, rather than the object itself in the case of multiple calls, It's still safe. Take a look at the example below, there is a data member in Stringbuffertest contents it is used to extend, each append is thread-safe, but the combination of many times append is not thread-safe, this output is not too controllable, However, if you add the keyword synchronized to the log and Getcontest methods, the result will be very organized, and if you switch to Stringbuider or even append to half, it will also allow the other threads to operate on this basis:

Finally, write a set example:

Set set =new HashSet (); String S1=new string ("Hello"); String s2=s1; String S3=new string ("World"); Set.add (S1); Set.add (S2); Set.add (S3); System.out.println (Set.size ());

The result is:

2

Duplicate elements are not allowed in set, so it is 2,

Neither the key set nor the set set of the map allows duplicate elements to exist, and the list can have duplicate elements.

Java Collection class List Set Map thread safety

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.