Java common data structures

Java common data structures

 

 

The overall framework of java collections

 

      Java collections are toolkits provided by java, including common data structures: collections, linked lists, queues, stacks, arrays, maps, etc. The Java Collection Toolkit location is java.util.*

 

      Java collections can be mainly divided into 4 parts: List list, Set collection, Map mapping, tool class (Iterator iterator, Enumeration enumeration class, Arrays and Collections).

 

Java collection toolkit framework diagram (below):



Commonly used collections

 

There are several commonly used data structures in Java, which are mainly divided into two main interfaces, Collection and Map (the interface only provides methods, not implementations), and the final data structure used in the program is the data structure class that inherits from these interfaces. Its main relationships (inheritance relationships) are: 

 

Collection---->Collections                                                                                                          

Collection---->List----->(Vector \ ArryList \ LinkedList)                                                          

Collection---->Set------>(HashSet \ LinkedHashSet \ SortedSet)

 

Map----->SortedMap------>TreeMap

Map------>HashMap

 



 

 Collections 

 

is a wrapper class. It contains various static polymorphic methods for collection operations. This class cannot be instantiated, just like a utility class that serves Java's Collection framework. (e.g. sorting, synchronization)

 

 

 

List

 

List is an ordered Collection, using this interface can precisely control the insertion position of each element. The user can use the index (the position of the element in the List, similar to the array subscript >) to access the elements in the List, which is similar to the Java array.

 

 

 

Vector

 

Array-based List actually encapsulates some functions that arrays do not have for us to use, so it is difficult to avoid the limitations of arrays, and the performance cannot exceed arrays. So, where possible, we want to use arrays more. Another very important point is that Vector is thread synchronized (synchronized), which is also an important difference between Vector and ArrayList.

 

 

 

ArrayList

 

Like Vector, it is a linked list based on an array, but the difference is that ArrayList is not synchronized. So it is better than Vector in performance, but when running in a multi-threaded environment, you need to manage the synchronization of threads yourself.

 

 

 

LinkedList

 

Unlike the previous two Lists, LinkedList is not based on arrays, so it is not limited by the performance of arrays. 

Each of its nodes (Node) contains two aspects: 

1. The data of the node itself (data); 

2. The information of the next node (nextNode). 

Therefore, when adding and deleting actions to LinkedList, there is no need to do a lot of data movement like an array-based ArrayList. It can be achieved as long as the relevant information of nextNode is changed, which is the advantage of LinkedList.

 

 

 

ListSummary

 

  • All Lists can only accommodate a single table of different types of objects, not Key-Value key-value pairs. For example: [tom,1,c]
  • All Lists can have the same elements, for example, Vector can have [tom,koo,too,koo]
  • All Lists can have null elements, such as [tom,null,1]
  • Array based List (Vector, ArrayList) is suitable for query, while LinkedList is suitable for add, delete operations

 

 

Set

 

 

A Set is an unordered Collection that does not contain duplicate elements. 

 

 

 

HashSet

 

 

Although both Set and List implement the Collection interface, their implementation methods are quite different. List is basically based on Array. But Set is implemented on the basis of HashMap, which is the fundamental difference between Set and List. The storage method of HashSet is to use the Key in the HashMap as the corresponding storage item of the Set. Take a look at the implementation of the add(Object obj) method of HashSet to see at a glance. 

 

public boolean add(Object obj) {   
   return map.put(obj, PRESENT) == null;   
}  

 This is also the fundamental reason why there cannot be duplicate items in Set like in List, because the keys of HashMap cannot have duplicates. 

 

 

 

LinkedHashSet

 

A subclass of HashSet, a linked list. 

 

 

 

TreeSet

 

A subclass of SortedSet, which is different from HashSet in that TreeSet is ordered. It is implemented through SortedMap. 

 

 

 

SetSummary

 

  • The basis of Set implementation is Map (HashMap)
  • The elements in the Set cannot be repeated. If you use the add(Object obj) method to add an existing object, the previous object will be overwritten

 

 

Map

 

Map is a container that associates key objects with value objects, and a value object can be a Map, and so on, so that a multi-level map can be formed. For key objects, like Set, the key objects in a Map container are not allowed to be repeated, this is to maintain the consistency of the search results; if there are two key objects the same, then you want to get the value object corresponding to that key object When there is a problem, maybe what you get is not the value object you think, and the result will be confusing, so the uniqueness of the key is very important, and it is also in line with the nature of the set. Of course, in the process of use, the value object corresponding to a key may change, and then the value object corresponding to the key will be corresponding to the last modified value. There is no unique requirement for value objects, you can map any number of keys to a value object without any problems.

 

 

 

HashMap

 

A hash code algorithm is used to quickly find a key.

 

 

 

TreeMap

 

The keys are stored in order, so it has some extended methods, such as firstKey(), lastKey(), etc. You can also specify a range from the TreeMap to get its child Map. 

 

 

 

HashTable

 

A thread-safe HashMap, but not null.

 

 

 

Iterator

 

It is a tool for traversing collections, that is, we usually traverse collections through Iterator iterators. We say that Collection depends on Iterator because the implementation classes of Collection must implement the iterator() function and return an Iterator object.

ListIterator exists specifically for traversing Lists.

 

 

 

Enumeration

 

It is an abstract class introduced in JDK 1.0. The function is the same as the Iterator, and it also traverses the collection; but the function of Enumeration is less than that of Iterator. Enumeration can only be used in Hashtable, Vector, Stack.

 

 

 

The difference between several commonly used classes 

 

  • ArrayList: single element, high efficiency, mostly used for query 
  • Vector: single element, thread-safe, mostly used for query 
  • LinkedList: single element, mostly used for insertion and deletion 
  • HashMap: elements are paired, elements can be empty 
  • HashTable: Elements are paired, thread-safe, elements cannot be null 

 

 

Vector, ArrayList and LinkedList and arrays

 

  • If you can use an array (the element type is fixed, the length of the array is fixed), please try to use an array instead of List; 
  • If there are no frequent deletion and insertion operations, and without considering the multi-threading problem, ArrayList is preferred; 
  • If used in multi-threaded conditions, you can consider Vector; 
  • LinkedList is useful if you need to delete and insert frequently; 
  • If you don't know anything, use ArrayList. 

 

 

The difference between Collection and Collections

 

Collection

    An interface under Java.util that is the parent interface for various collection structures. Inheritance and his interface mainly include Set and List.

 

Collections

    A dedicated static class under java.util that contains various static methods for collection operations.

    Provides a series of static methods to implement operations such as searching, sorting, and thread safety for various collections.

 

 

 

Difference between Array and Arrays

 

Array class Array

    One of the most basic storage structures in Java.

    Provides methods for dynamically creating and accessing Java arrays. The elements in it must be of the same type.

    High efficiency, but the capacity is fixed and cannot be changed dynamically.

    It cannot tell how many elements are actually stored in it, length just tells us the capacity of the array.

 

static class Arrays

    This static class is dedicated to manipulating arrays, providing static methods such as searching, sorting, and copying.

    equals(): Compares two arrays for equality. The array has the same number of elements, and all corresponding elements are equal in pairs.

    sort(): used to sort the array.

    binarySearch(): Search for elements in sorted array.

 

 

 

http://zhangshixi.iteye.com/category/101360

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327056331&siteId=291194637