在Java中,哪些集合类是线程安全的?
Stack
Java中的Stack类实现了基于LIFO原则的堆栈数据结构。因此,Stack类支持许多操作,如push、pop、peek、search、empty等。
示例
import java.util.*; public class StackTest { public static void main (String[] args) { Stack stack = new Stack(); stack.push(5); stack.push(7); stack.push(9); Integer num1 = (Integer)stack.pop(); System.out.println("The element popped is: " + num1); Integer num2 = (Integer)stack.peek(); System.out.println(" The element on stack top is: " + num2); } }登录后复制