In-depth understanding of the Java Collections Framework, look at this on the same subject

Java Collections Framework

Long before the Java 2, Java provides ad hoc category. For example: Dictionary, Vector, Stack, and Properties of these classes for storage and manipulation of objects.

Although these classes are very useful, but they lack a central, unifying theme. For this reason, use the Vector class and use the Properties class has very different.

Collections Framework is designed to meet the following objectives:

The framework must be high-performance. The basic set (dynamic arrays, linked lists, trees, hash tables) implementation must also be efficient.

This framework allows collection of different types, operate in a similar manner, a high degree of interoperability.

Extension and adaptation of a collection must be simple.

To this end, the entire collection is designed around a frame on a set of standard interfaces. You can use these standard interfaces to achieve, such as: LinkedList, HashSet, and so on TreeSet, except that you can implement your own collection via these interfaces.

Collections Framework is used to represent and manipulate a unified schema collection. Set of all frameworks comprising the following:

Interface: a collection of abstract data type represents. Interface allows manipulation of a collection of separate details of its representative. In object-oriented language, an interface generally form a hierarchy.

Implement (s): it is a concrete implementation of a set of interfaces. Essentially, they are reusable data structure.

Algorithms: Some useful computational objects that implement collection interfaces in the method of execution, such as: searching and sorting. These algorithms are referred to as polymorphism, it is because the same can have different method implemented on a similar interface.

In addition to the collection, the frame also defines several interfaces and Map classes. Map in storage is the key / value pairs. Despite Map not collections, but they are fully integrated in the collection.

 

Framework set as shown:

In-depth understanding of the Java Collections Framework, look at this on the same subject

 

Java framework provides a set of a high performance, easy to use interfaces and classes, java java.util package set frame is located, so when using a collection bag guiding frame required. Here are a few common interface and the interface implementation class.

Common interface implementation class List

A List is an element of order, can be repeated, you can be null collection of (sometimes we call it the "sequence").

ArrayList

The most commonly used classes List interface, the underlying dynamic array using variable length achieved. ArrayList has an initial capacity (capacity = 10), for expansion when the element is greater than the number of initial capacity, a new array length = length + old old array array length / 2.

Because each element has a fixed position of the index, so the index query element according to the speed very quickly. If the element is inserted in the middle, since all the elements to the back of a shift, so the performance will be relatively poor.

In the absence of doing concurrent access control, so it is a non-thread-safe collection. It allows repeated elements or null elements.

LinkedList

Two-way link List interface implementation class that allows NULL element. It is an ordered set of its performance, but memory is actually a disorder save.

Reasons, so it will soon insert the speed, but the query an element of speed much slower speed than the ArrayList. Is a non-thread-safe collection.

Vector

Vector underlying dynamic array implemented using the default initial capacity of 10, the initial capacity can be specified by the construction method, and can specify the increment of expansion.

Capacity expansion rule refers to new = old + expansion capacity increment, if not specified incremental expansion of the new capacity = 2 * old capacity. Its key methods are added synchronized, so there is a thread-safe collection.

Set collection interfaces

If the collections is to achieve Set interface includes features: random, non-repeatable

The order of addition order of the elements and the elements out is inconsistent.

Pay attention to the unique nature of the collection system can tell if something is almost exists in the collection, it does not store duplicate elements.

hashSet

HashSet underlying hash table is used to support, characteristics: fast access speed.

When Hashset to add elements, HashSet will first call the hashCode method to get the elements of the hash value of the element,

Then the hash value of the element after rotate operations, it can calculate the position of the storage element in the hash table.

LinkedHashSet

Use LinkedHashMap LinkedHashSet bottom to hold all the elements, which inherits the HashSet, operatively HashSet and the same for all the methods which, therefore LinkedHashSet on the implementation is very simple, providing only four construction method.

And by passing a parameter identification, call the parent class's constructor, a bottom layer configured to implement LinkedHashMap, the operation is the same as in the parent class HashSet relevant operation, the method can directly call the parent class HashSet.

treeSet

treeSet bottom is red - Black Tree data structure implementation, the default ordering of the natural elements (String).

If two objects when comparing the return value is 0, then repeat elements.

Common Map interface implementation class

Map provides a more general method of storage elements. Map for storing collections of elements (referred to as a "key" and "value"), wherein each key is mapped to a value.

Conceptually, you can be seen as Map List with a numerical key. In fact, in addition to the definition of List and Map in java.util Chinese and foreign, both of which are not directly linked.

This article will focus on the core Java distributions comes in the Map, will also introduce how to adopt or implement more suitable for your dedicated Map application-specific data.

HashMap

When the additive element to the HashMap, the hashCode method first calls the key element to obtain the hash code value, and can be calculated through the operation of the storage location in the hash table element.

And null null value is permitted and the key. This class does not guarantee the order of mapping, in particular, it does not guarantee that the order lasts forever.

HashTable

HashTable is synchronized (synchronized function), while HashMap is not synchronized, so HashTable slower HashTable not tolerate null keys and values.

The HashMap accept a null keys and null values ​​in addition to numerous keySet (), entrySet (), values ​​() out of the iterator support these HashMap, HashTable also supports an Enumeration keys (), elements (), but now they are implementation is based on a class that implements the Enumeration and Iterator interfaces.

TreeMap

TreeMap also based on red-black tree (binary) data structure implemented features: sorting the storage element will bond.

Note: Set elements can not be repeated, if the set will add an element repeated unsuccessful.

Map key can not be repeated, if the key is repeated overwrites.

Source: SEO Technology

Guess you like

Origin www.cnblogs.com/1994jinnan/p/12287832.html