JAVA basics-List, Set, and Map discussions, and javasetmap discussions

Source: Internet
Author: User

JAVA basics-List, Set, and Map discussions, and javasetmap discussions

Today, we will discuss the collection classes that we often use. Although they are commonly used, I believe there are still a lot of ape people who still abuse them. So today we will popularize them.

First, take a look at the image:

Start with Collection. Collection is an interface that contains the List and Set sub-interfaces (Quene is not discussed here). Today we will mainly discuss: List, Set, and Map.

List is an interface that implements the Collection interface. Implementation classes: collections List, ArrayList, and Vector

Set is an interface that implements the Collection interface. Its implementation classes include HashSet, TreeSet, and sorted Set.

Map is an interface implementation class: HashMap, Hashtable, TreeMap, LinkedHashMap, WeakHashMap, IdentityHashMap

 

The main difference between the Collection and Map interfaces is that the Collection stores a group of objects, while the Map stores the keyword/value pairs.

 

The List, Set, and Map interfaces are different:

A List stores a single element. elements can be repeated and Null elements can be stored.

Set stores a single element and cannot be repeated.

Map stores key-value pairs, keys are repeatable, and values are repeatable. key-value pairs can all be Null, but keys can only have one Null.

 

The following describes the Common Implementation classes of these three interfaces:

List:

Vector:

An Array-based List is a thread-synchronized (sychronized) List.

ArrayList:

Like Vector, it is an array-based linked list, but the difference is that ArrayList is not synchronized. Therefore, the performance is better than the Vector, but when running in a multi-threaded environment, you need to manage the thread synchronization issues on your own.

Shortlist:

The sorted List is different from the previous two lists. It is not based on arrays, so it is not limited by Array Performance. Each Node contains two aspects:

1. data of the node itself );

2. Information about the next node (nextNode ).

Therefore, you do not need to move a large amount of data like the array-based ArrayList when adding or deleting the rule list. You only need to change the relevant information of nextNode. This is the advantage of the nextNode list.

Set:

HashSet:

Although Set and List all implement the Collection interface, their implementation methods are quite different. List is basically based on Array. However, Set is implemented based on HashMap, which is the fundamental difference between Set and List.

The HashSet storage method uses the Key in HashMap as the corresponding storage item of the Set. This is also the root reason Why duplicate items cannot exist in the Set as in the List, because HashMap keys cannot be duplicated.

LinkedHashSet:

A subclass of HashSet, a linked list.

TreeSet:

Different from HashSet, TreeSet is ordered by SortedMap.

Map:

HashMap

The hash code algorithm is used to quickly find a key and its corresponding values.

TreeMap

It stores keys in order, so it has some extension methods, such as firstKey () and lastKey (). You can also specify a range from TreeMap to obtain its submap.

When the order of elements is very important, use TreeMap. When elements do not need to be stored in a specific order, use HashMap.

 

At this point, we have provided the advantages and disadvantages of common collection classes. Sometimes, the performance of your system is greatly reduced by these small knowledge points. In terms of usage, we should note that it is a saving for the system!



In java, what scenarios are list, set, and map used?

List
Set
Map ing

We can see their approximate functions in terms of meanings:

The list stores objects sequentially and can have the same objects, which are accessed through indexes;

The Set stores objects unordered, and there cannot be repeated objects (unique, recalling the set features learned in mathematics in High School). The set has no index and can only be retrieved for multiple times;

Ing stores the ing between keys and values. The middle key is unique (there cannot be repeated objects), and the value can have repeated objects, you need to specify the key and the corresponding value. You can obtain the value based on the key name or traverse it.

As to when to use it, this should be tailored to local conditions.
List. When Arrays can be used directly, the list is used. For example, the scores of students in a class can be repeated;
Set, which is generally used to store unordered objects (indicating that the order is not important) that cannot be repeated. For example, the student ID of a class cannot be repeated;
Ing is used to store key-value pairs with corresponding relationships. For example, a student's student ID and name ing corresponds to a student's name, and the student ID cannot be repeated, however, the name may be repeated;

In actual Java Development, where are set, Map, and List used?

In fact, in actual development, multiple points are used for list and map.
List. For example, if you read many records from the database and encapsulated them into an object, you can install them in a list of the object type to iteratively access each object, the objects in the table are ordered and only indexed. You cannot get her by naming them;
Map is a key-value pair composed of key-value; value can be any object; key is a String; you can access value through key; for example, in servlet technology, all the things in response in request are map, and the things you read when parsing xml can also be saved by map.

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.