If a program contains only a fixed number of objects,And know how long they exist,Then this procedure is quite simple.


There are many ways to accommodate objects,Arrays are one of the most common.It mainly has the following two characteristics:efficiency and type.For Java, the most efficient way to save and access a series of objects (actually object handles) is an array.The array is actually a simple linear sequence,So the access speed is very fast,But it also has some limitations,If the size of the array is fixed,And it must not change within the "time of existence."

For an array of primitive data types,Its operation type is similar to the object array,The difference is that the former does not hold a handle to the object,It's the actual value.


When programming,It is usually not known exactly how many objects need to be saved,Sometimes I even want to save objects in more complex ways,To solve such problems,Java provides several "set classes":vector, bitset, stack, and hashtable.

Disadvantage of collections:Type information is lost.It actually holds a handle to an object of type object.

Enumerator (iterator)

Save the object with a collection and then visit,You need to know the exact type of the objects in the collection in advance,Otherwise, an exception will occur during use.And iterators can solve this problem.An iterator is an object,Its role is to traverse a series of objects,And select every object in that sequence,Also keep client programmers from knowing or paying attention to the underlying structure of that sequence.

Enumeration in Java is a typical iterator,It is mainly used to do the following things:

(1) A method called elements is required for the collection to provide us with an enumeration. When its nextelements are called for the first time, this enumeration returns the first element in the sequence.

(2) Use nextelements to get the next object.

(3) Use hasmoreelements to check if there are more objects.

Collection type:


The vector class implements a growable array of objects.As with arrays,It contains components that can be accessed using integer indexes.However, the size of the vector can be increased or decreased as needed,To accommodate adding or removing items after the vector is created.


A bitset is actually a vector of "binary bits". If i want to efficiently save a lot of "on-off" information, you should use a bitset. Each component of the bit set has a boolean value. Index the bits of the bitset with a non-negative integer.Each indexed bit can be tested, set, or cleared.By logical AND, logical OR and logical XOR operation,You can use one bitset to modify the contents of another bitset. by default,All bits in the set have an initial value of false.

3. stack

A stack is also called a "last in, first out" collection. The stack class in java inherits from the vector class, which extends the class vector with five operations, allowing the vector to be treated as a stack.It provides the usual push and pop operations, as well as the peek method to take the apex of the stack, the empty method to test if the stack is empty, the search method to find items in the stack and determine the distance to the top of the stack.


Hash tables are an important storage method.It is also a common retrieval method.The basic idea is to use the value of the relation code as an independent variable.Calculate the corresponding function value through a certain functional relationship,Interpret this value as the storage address of the node,The node is stored in a storage unit corresponding to the calculated storage address.The retrieval method is used to retrieve the key code.Hash tables now have a complete set of algorithms for inserting, deleting, and resolving conflicts.Hash tables are used to store objects in java.Achieve fast retrieval.

to sum up

  • Previous net core how to use Redis publish and subscribe
  • Next inner join Inline and left join Example code for left join