Common collection Frameworks in Java

Source: Internet
Author: User
Tags repetition

1.

One, collection (ordered) interface implementation of the interface set list

Where the implementation class of the set interface is the implementation class of the Hashset,list interface is ArrayList, Linklist, Vector

The implementation class of MAP (unordered) interface is HashMap, HashTable

Both ArrayList and vectors use arrays to store data, which is larger than the actual stored data in order to add and insert elements, both of which allow the element to be indexed directly by ordinal, but the insertion element involves memory operations such as array element movement, so the index data is fast and the data is inserted slowly. Vector because of the use of the Synchronized method (thread-safe), usually performance is worse than ArrayList, and LinkedList using a doubly linked list for storage, index data by ordinal need to be forward or backward traversal, but when inserting data only need to record the item before and after items can be , so the insertion speed is faster.

2. The difference between Collection and collections.

Collection is the ancestor interface of the collection class, and the main interface for inheriting it is set and list. Collections is a helper class for a collection class that provides a series of static methods for searching, sorting, threading, and so on for various collections.

3. The difference between HashMap and Hashtable.

HashMap is a lightweight implementation of Hashtable (non-thread-safe implementation), they all complete the map interface, the main difference is that the HASHMAP allows null (NULL) key value (key), because of non-thread security, the efficiency may be higher than Hashtable.  HashMap allows NULL to be used as a entry key or value, and Hashtable is not allowed. HashMap hashtable contains method removed, changed to Containsvalue and ContainsKey. Because the contains method is easy to cause misunderstanding.  Hashtable inherits from the dictionary class, and HashMap is an implementation of the map interface introduced by Java1.2. The biggest difference is that the Hashtable method is synchronize, and HashMap is not, in multiple threads to visit

When asked Hashtable, you do not need to synchronize its methods, and HashMap must provide external synchronization. The Hash/rehash algorithms used by Hashtable and HashMap are probably the same, so there is no big difference in performance.

4, List, Set, map inherit from collection interface?

List,set Yes, map is not

5, set of elements can not be repeated, then what method to distinguish the repetition or not? Are you using = = or equals ()? What is the difference between them?

The elements in the set cannot be duplicated, so use the iterator () method to distinguish between duplicates or not. Equals () is the interpretation of two sets for equality. The Equals () and = = methods Determine whether the reference value points to the same object Equals () is overwritten in the class so that when the contents and types of the two detached objects match, the truth is returned.

6. What are the collection classes you know? The Main method?

The most common collection classes are List and Map. The specific implementation of the list includes ArrayList and vectors, which are variable-sized lists that are more appropriate for building, storing, and manipulating any type of object. List is useful for cases where elements are accessed by numeric indexes. MAP provides a more general method of storing elements. The Map collection class is used to store element pairs (called "Keys" and "values"), where each key is mapped to a value.

The elements in the 7,set cannot be duplicated, so what is the way to distinguish between repetition or not? Are you using = = or equals ()? What is the difference between them?

The elements in the set cannot be duplicated, so use the iterator () method to distinguish between duplicates or not. Equals () is the interpretation of two sets for equality. The Equals () and = = methods Determine whether the reference value points to the same object Equals () is overwritten in the class so that when the contents and types of the two detached objects match, the truth is returned.

8. Differences between ArrayList and vectors, HashMap and Hashtable

A: ArrayList and vectors are mainly from two aspects.  I. Synchronization: vector is thread-safe, that is, synchronous, and ArrayList is a thread program is not secure, not synchronous two. Data growth: When growth is needed, vectors grow by default to the original, while ArrayList is half the original. HashMap and Hashtable are mainly from three aspects. I. Historical reasons: Hashtable is based on the old dictionary class, HashMap is an implementation of the map interface introduced by Java 1.2 two. Synchronization: Hashtable is thread-safe, that is, it is synchronous, and HashMap is unsafe for the line program. , not synchronous three. Value: Only HashMap can let you use NULL as the key or value of an entry for a table

Common collection Frameworks in Java

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.