Good programmer Java tutorial data structure knowledge comb, Java is an object-oriented programming language, with powerful and easy to use two attributes. The Java toolkit provides powerful data structures, including interface classes for enumerations, bitsets, vectors, stacks, dictionaries, hash tables, properties, and more. Next, I will give you a simple knowledge comb.

There are several commonly used data structures in Java, which are mainly divided into Collection and Map interfaces (interfaces only provide methods, but do not provide implementation). The data structures ultimately used in programs are data structure classes inherited from these interfaces.

What is the difference between ArrayList, LinkedList, and Vector?

ArrayList

1) Only reference objects can be loaded (base types are converted to encapsulated classes);

2) The thread is not safe;

3) The bottom layer is implemented by the array (sequence table). Because it is implemented by the sequence table, it has the characteristics of the sequence table, such as: the length needs to be declared, the capacity needs to be expanded when the length exceeds, it is not suitable for frequent movement to delete elements, and the retrieval of elements is fast;

4) The default value of capacity is 10. When oldCapacity exceeds the value, capacity automatically increases by 0.5 times (oldCapacity >> 1).

Vector:

1) Only reference objects can be loaded (base types are converted to encapsulated classes);

2) Vector ensures thread safety through synchronized method;

3) The bottom layer is also implemented by arrays;

4) Capacity defaults to 10 and grows by capacityIncrement when capacityIncrement is greater than or equal to 0. When capacityIncrement is greater than or equal to 0, capacityIncrement is multiplied by 1. CapacityIncrement: oldCapacity).

LinkedList

1) Only reference objects can be loaded (base types are converted to encapsulated classes);

2) The thread is not safe;

3) The bottom implementation is linked list, which has the characteristics of linked list, such as: no declaration of length, poor retrieval performance, but fast insertion, movement and deletion.

4) Lists are implemented through Node objects.

The difference between linked lists and arrays

Arrays must be sized before they can be used, and not being able to do so dynamically can result in allocating too many units to the array and wasting precious resources. On the bad side, the program may need to process more data than the units of the array at runtime.

When you need to dynamically decrease or increase data items, you can use a linked list as a data structure.

Why hash tables?

With arrays and linked lists, if you want to find a particular element they store without knowing its location, you need to access the elements from scratch until you find a match. If the data structure contains too many elements, time is wasted. It’s a good idea to use a hash table to store the data you’re looking for.

The main implementation classes of the List interface include:

ArrayList () : Represents an array whose length can be changed. Elements can be randomly accessed, and inserts and deletions to ArrayList () are slow.

LinkedList () : adopt LinkedList data structure in implementation. Insert and delete fast, slow access.

In the case of a random access to a List, only elements are randomly retrieved at a particular location. The get (int index) method of List puts back the object in the collection at the index position specified by the parameter index, with subscripts starting at “0”. Two basic methods for retrieving all objects in a collection.

Use HashSet to determine whether a primary key exists

HashSet implements the Set interface, which is implemented by a hash table (actually a HashMap), but does not guarantee the order of iteration of the Set and allows null elements. The time complexity of a HashSet is the same as that of a HashMap. If there are no hash conflicts, the time complexity is O(1); if there are hash conflicts, the time complexity does not exceed O(n). So, in everyday coding, you can use a HashSet to determine whether a primary key exists.

All Java development engineers in the daily development work, can not do without Java common data structures, enterprises in the recruitment process will also examine candidates to grasp Java data structures, so you should pay high attention to data structures.