What's wrong with the Stack class in the Java language?

When creating a new stack, it is not recommended to write:

Stack<Integer> stack=new Stack<>()

But:

Deque<Integer> stack=new ArrayDeque<>()

 

 

Let's talk about the Stack class in the Java language, what's the problem?

The Stack class in Java inherits the Vector class. Vector is a dynamic array

In this way, Stack inherits all the public methods of Vector

As a dynamic array, Vector has the ability to add or delete elements at any position in the array, and Stack also has this ability

stack.add(1,666);

For the stack, it is specified to insert the element 666 at the position of index 1, which destroys the encapsulation of the data structure of the stack

Users may call these operations intentionally or unintentionally, which will become the source of bugs

 

 

The reasons for this problem are:

The relationship between Stack and Vector should not be an inheritance relationship, but a composition relationship (composition)

继承: Is-a is one, for example: cat is an animal, cat class inherits animal class

组合: Has-a has one, for example: there is an engine in the car, and the engine class and the car class are combined (the member variable of the engine class object in the car)

In the real world, there are few true inheritance relationships, and composition relationships are more commonly used ( combination is used more often , inheritance is less used )

 

 

The reason why Java official does not change the Stack class:

If modified, programs using the old version of Java will not be able to execute in the new Java environment

 

 

Deque interface:

Deque actually means a double-ended queue, which can be inserted and deleted at both ends, and then the real stack can only be inserted and deleted at the same end.

As mentioned earlier, the problem with the Stack class is that it inherits several unwanted methods of the Vector class, which breaks the encapsulation. The stack of the Deque interface still has unneeded methods. This is a problem left over from the history of Java, and it has no solution now.

Summary: Although Java officially recommends using the Deque interface to implement stack, such a stack also destroys encapsulation and is not safe .

 

 

The difference between LinkedList and ArrayDeque:

Using ArrayDeque dynamic array, if the expansion operation is triggered, the world complexity is O(n)

Using LinkedList linked list will not involve the expansion problem, so every addition operation, the time is complicated and European O(1)

But in fact, when the amount of data reaches a certain level, the performance of the linked list is much lower than that of the dynamic array , because every time an element is added to the linked list, a Node class object must be recreated, that is, a new memory operation is performed. Memory operation is very slow

Summary: In practice, especially when facing large-scale data, you should not use linked lists !

 

For ArrayList, if your application scenario does not require thread-safe features, then for dynamic arrays, you should use ArrayList

 

 

The difference between Stack and ArrayDeque methods:

Stack:

stack.push()

stack.pop()

stack.peek()

 

ArrayDeque:

stack.addLast()

stack.removeLast()

stack.peekLast()

 

 

Guess you like

Origin blog.csdn.net/di_ko/article/details/115004162