Collection of Java (set, list, map)

The Java collection class is stored in the Java, UTI package and is a container for storing objects.

Java collections can be divided into three systems: set, map and list.

After jdk5, generics are added, and Java collections can remember the data types of objects in the container.

1、 HashSet

HashSet is a typical implementation of the set interface. This implementation class is used most of the time when using the set set. Most of the time, set refers to HashSet.

HashSet is a hash algorithm to store the elements of a collection, so it has good access and search performance.

HashSet has the following characteristics:

When an element is stored in the HashSet, the HashSet will call the hashcode () method of the object to get the hashcode value of the object, and then determine the storage location of the object in the HashSet according to the hashcode value. If the equals () method of two elements returns true, but their hashcode () return values are not equal, the HashSet will store them in different locations, but they can still be added successfully.

HashSet -- implements the set interface, which inherits the collection interface.

2、 TreeSet

TreeSet is the interface implementation class of sortedset. Its main relationships are as follows: TreeSet class implements navigableset interface, navigableset interface inherits sortedset interface, sortedset inherits set interface, and set interface inherits collection interface. TreeSet ensures that collection elements are in a sorted state. TreeSet supports two sorting methods: natural sorting and custom sorting. By default, TreeSet adopts natural sorting.

Natural sorting: TreeSet will call the CompareTo (object obj) method combining elements to compare the size relationship between elements, and then arrange the elements in ascending order. If this > obj, it returns 1; if this < obj, it returns - 1; if this = obj, it returns 0, the two objects are considered equal. Note: objects of the same type must be placed (sorted by default), otherwise type conversion exceptions may occur, and generics can be used for restriction.

The elements inside are ordered, i.e. [2,3,4,5].

We can also customize the arrangement rules:

Output:

Name: Mike age: 18 Name: Tom age: 21 Name: Bob age: 43

It can be found that it is indeed sorted by age. If you want to sort in descending order, you only need to exchange the returned 1 and - 1 in person.

3、 List and ArrayList

List represents an ordered and repeatable collection, and each element in the collection has its corresponding sequential index.

List allows you to use duplicate elements, and you can access collection elements at a specified location through an index.

List sets the index by default according to the order in which the elements are added.

The list collection adds some methods to manipulate the collection elements according to the index.

The ArrayList class implements the list interface, which inherits the collection interface.

ArrayList and vector are typical implementations of the list interface:

difference:

4、 Map

Map is used to save data with mapping relationship. Therefore, two groups of values are saved in the map set, one is used to save the key in the map and the other is used to save the value in the map.

The key and value in the map can be data of any reference data type.

Duplicate keys are not allowed in the map, that is, any two keys of the same map object will return false when compared through the equals method.

There is a one-way one-to-one relationship between key and value, that is, the unique and determined value can always be found through the specified key.

HashMap and hashtable are two implementation classes of Mao interface.

difference:

Just as the HashSet set cannot guarantee the order of elements, hashtable and HashMap cannot guarantee the order of key value pairs.

The criteria for hashtable and HashMap to judge the two keys are the same: the two keys return true through the equals method, and the hashcode is also equal.

Hashtable and HashMap have the same criteria for judging two values: the two values return true through the equals method

5、 Treemap

When storing key value pairs, treemap needs to sort the key value pairs according to the key. Treemap can ensure that all key value pairs are in an orderly state.

Natural sorting: all keys in treemap must implement the comparable interface, and all keys should be objects of the same class, otherwise an exception will be thrown;

Custom sorting: when creating a treemap, a comparator object is passed in, which is responsible for sorting all keys of the treemap. At this time, the map key is not required to implement the comparable interface;

Output:

{1=a,2=b,3=c}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>