The charm of the Collection framework: stack!

xiaoxiao2021-03-06  51

Stack is a major mistake in Collection design in Java. This class is definitely garbage, and people who design Stack this class must be a brain. As everyone knows, the function of the stack in the data structure implemented by Stack is the first out, see the Stack's statement: public Class Stack Extends Vector

Vector is no longer a good thing, here still inherited it? The method in the stack is listed here, and it does not consider its implementation, because if you want to use this, then I can't say it. public Object push (Object item) public synchronized Object pop () public synchronized Object peek () public synchronized int search (Object o) public boolean empty () You may feel that these methods are nothing, but when it inherits Vector, means It doesn't have value, but this class is really simple, interested, you can look at the source code. We know that vector is a List interface, and Stack inherits vector naturally implements a list interface. In fact, Stack is not List, even COLLECTION is not. Is the designer designed to save the code, but we should design the interface to minimize the interface. If so, if you can use a class!

Stack should only have the above way, should not have other methods, and the existing design can use the Stack as a vector. List stack = new stack (); Do you see such a code? Don't you feel very awkward? I strongly recommend not using Stack!

转载请注明原文地址:https://www.9cbs.com/read-115591.html

New Post(0)