Java Collection "Fight The Forgotten series"--the Collection of the constant grooming

Source: Internet
Author: User
Tags array length

          

JAVA2 's set of frames, pumping its core, has three main types: List, set, and map. As shown in the following:

It is important to note that Collection, List, set, and map are interfaces (Interface), not specific class implementations. List lst = new ArrayList (); This is the statement we usually use to create a new list, where list is the interface, and ArrayList is the specific class.


The inheritance structure of common collection classes is as follows:

Collection<--list<--vector

Collection<--list<--arraylist

Collection<--list<--linkedlist

Collection<--set<--hashset

Collection<--set<--hashset<--linkedhashset

Collection<--set<--sortedset<--treeset

Map<--sortedmap<--treemap

Map<--hashmap

-----------------------------------------------SB Split Line------------------------------------------

List:

The list is an ordered collection, using this interface to precisely control where each element is inserted. The user is able to access the elements in the list using an index (where the element is located in the list, similar to an array of >), similar to an array of java.


Vector:

Array-based list, in fact, encapsulates the array does not have some of the features convenient for us to use, so it is difficult to avoid the limitations of the array, while the performance can not be beyond the array. So, if possible, we need to use arrays more. Another important point is that vectors are thread-synchronized (sychronized), which is one of the important differences between vectors and ArrayList.

ArrayList:

The same vector is an array-based list, but the difference is that ArrayList is not synchronous. So it's better to be more performance than a vector, but when running into a multithreaded environment, you need to manage your thread's synchronization problems.

LinkedList:

LinkedList differs from the previous two list, which is not array-based, so it is not limited by the performance of the array.

Each node of it contains two things:

1. Data of the node itself;

2. Information for the next node (nextnode).

So when adding to LinkedList, deleting an action does not have to be a lot of data movement like an array-based ArrayList. As long as you change the relevant information NextNode can be achieved, this is the LinkedList advantage.

List Summary:

All lists can contain only a single table of different types of objects, not key-value key-value pairs. Example: [Tom,1,c]

All lists can have the same elements, such as vector can have [Tom,koo,too,koo]

All lists can have null elements, for example [tom,null,1]

Array-based list (vector,arraylist) is suitable for querying, while LinkedList is suitable for adding, deleting operations

--------------------------------------------------------------------------


Set:

Set is an unordered collection that does not contain duplicate elements.

HashSet:

Although both set and list implement the collection interface, they are implemented in a very different way. The list is basically based on an array. But set is implemented on the basis of HashMap, which is the fundamental difference between set and list. The hashset is stored in the HashMap key as the corresponding storage of the set. Look at the implementation of the HashSet's add (Object obj) method to see it at a glance.

Java Code Collection Code

Public boolean Add (Object obj) {

return Map.put (obj, PRESENT) = = NULL;

}

This is also why it is not possible to have duplicate entries in the set as in the list, because the HashMap key cannot be duplicated.

Linkedhashset: A sub-class of HashSet, a linked list.

TreeSet: SortedSet, which is different from HashSet, is that TreeSet is orderly. It is achieved through SORTEDMAP.

Set Summary: set implementation is based on map (HASHMAP)

The elements in set cannot be duplicated, and if an existing object is added using the Add (Object obj) method, the previous object is overwritten

--------------------------------------------------------------------------


MAP:

A map is a container that associates a key object with a value object, and a value object can be a map, and so on, so that a multilevel map can be formed. For a key object, like set, a key object in a map container does not allow repetition, which is to keep the consistency of the search results, and if there are two key objects, then you have a problem with the value object that the key object has, and you may not get the value object you want, and the result will be confusing. So the uniqueness of the key is very important, and it conforms to the nature of the set. Of course, in the process of use, the value object corresponding to a key may change, then the last modified value object corresponds to the key. There is no unique requirement for a value object, and you can map any number of keys to a value object without any problems (although it may be inconvenient for you to use it, you do not know what you are getting for the value object that corresponds to that key).


There are two more common implementations of map: HashMap and TreeMap.


HashMap also uses the hash code algorithm to quickly find a key,


TreeMap is stored in the order of keys, so it has some extended methods, such as Firstkey (), Lastkey (), and so on, you can also specify a range from TreeMap to get its child map.

The association of Keys and values is simple, and a key can be associated with a value object using the put (Object Key,object value) method. Use Get (object key) to get the value object corresponding to this key object.


--------------------------------------------------------------------------


Other:

The difference between a few common classes

1. ArrayList: single element, high efficiency, more for querying

2. Vector: element single, thread safe, more for querying

3. LinkedList: Element single, multiple for insert and delete

4. HashMap: element is paired, element can be empty

5. HashTable: Element pair, thread safe, element not empty


Ii. vectors, ArrayList and LinkedList

Most of the time, the performance of the ArrayList is best, but when the elements in the collection need to be inserted frequently, delete LinkedList will have a better performance, but they are three performance than the array, and the vector is thread synchronization. So:

If you can use an array (the element type is fixed and the array length is fixed), try using an array instead of a list;

If there is no frequent deletion of the insert operation, but also do not consider multithreading problems, the preferred choice of ArrayList;

If used in multi-threaded conditions, vector can be considered;

If you need to delete inserts frequently, LinkedList has a place to go;

If you don't know anything, use ArrayList right.


Iii. Collections and Arrays

In the Java Collection Class framework there are two classes called collections (note, not collection! and arrays, which is a powerful tool inside JCF, but beginners tend to ignore it. According to the JCF documentation, these two classes provide wrapper implementations (Wrapper implementations), data structure algorithms, and array-related applications.

Presumably you won't forget the classic algorithms of "binary find" and "sort" mentioned above, and the collections class provides a rich static approach to help us do these annoying tasks in the data structure class:

BinarySearch: Binary Find.

Sort: Sorting, here is a method similar to the quick sort, the efficiency is still O (n * log n), but it is a stable sorting method.

Reverse: the linear table in reverse order, this is the previous data structure of the classic questions Oh!

Rotate: "Rotates" the linear table with an element as its axis.

Swap: Swaps the position of two elements in a linear table.

......

Another important feature of collections is the "wrapper" (Wrapper), which provides a way to convert a collection into a special set, as follows:

Unmodifiablexxx: Converted to a read-only collection, where xxx represents six basic set interfaces: Collection, List, Map, Set, SortedMap, and SortedSet. If you insert the delete operation on a read-only collection, the Unsupportedoperationexception exception will be thrown.

Synchronizedxxx: Converts to a synchronous collection.

Singleton: Creates a collection of only one element, where Singleton generates the cell set,

Singletonlist and Singletonmap each generate a single-element list and map.

Empty set: Represented by collections static properties Empty_set, Empty_list, and Empty_map.

This is an overview of the Java Collection class, and the next time we'll cover the specific application of the Java Collection class, such as list ordering, removing duplicate elements.





Java Collection "Fight The Forgotten series"--the Collection of the constant grooming

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.