Summary of Java Collections

Source: Internet
Author: User

A. The similarities and differences between HashSet and TreeSet :

The same point: All implements the set interface, the element is not duplicated, and the inserted duplicate object is overwritten.

Different points:

1. HashSet is an unordered collection of hash tables, which can be put into null,but only one null, and the values in both cannot be duplicated; HashSet requires that the object put in must implement the hashcode () method, put in the object, is to hashcode Code as the identification

2. TreeSet is a collection of red and black trees, sorted, and the data is automatically sequenced (the element object must be implemented

Comparable interface), null values are not allowed .

two. The similarities and differences between ArrayList and vectors :

Same point: Both ArrayList and vectors implement the List interface, saving the order of insertions and allowing repetition. The lower layer encapsulates a one-dimensional array of objects, the default initial capacity is ten,and if the number of elements added in the List interface is greater than the low-level array length, the lower -level array is re-created elementdata = Arrays.copyof (Elementdata, newcapacity);

can be iterated using iterator .

Different points:

1. :Vector is synchronous (thread-safe), running less efficient, mainly used in multi-threaded environment;

and The ArrayList is not synchronized and is suitable for use in single-threaded environments.

Java.util.Stack class inherits the The vector class, which stores and operates data in a "LIFO" manner in the corresponding data structure,is implemented using the underlying array of the vector package.

LinkedList This class implements the queue ( Queue ) Interface , The lower layer is based on the data structure of the linked list , which stores and operates in a "FIFO" manner.

three. map structure :hashmap,hashtable,treemap

Same point: HashMap and the Hashtabel The lower level is based on Hash table to implement the key - value-mapping structure;

Hashtable and HashMap are used in exactly the same format.        

different points:

1). the keys and values of elements in Hashtable are not allowed to be null, while HashMap is allowed.

2). Hashtable is synchronous, that is, thread-safe , relatively low efficiency, suitable for use in multi-threaded environment, and HashMap is not synchronized, relatively high efficiency, Advocated for use in single-threaded environments.

3). The default initial capacity of the HashMap is 0.75, and theload factor is

the default initial capacity of the Hashtable is one and the load factor is 0.75.

Properties class Inherits the Hashtable class, representing a persistent set of properties. Properties can be saved in a stream or loaded from a stream.

TreeMap: realized the SortedMap interface (based on the implementation of the red-black tree). Constructs a new empty map that is sorted by the natural order of the keys. All keys inserted into the map must implement the comparable interface ;

Automatic ascending sort.

to summarize:HashMap In addition to not synchronizing and allowing nulls ,theHashMap class Hashtable roughly the same


Summary of Java Collections

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.