public void test1(){
// 堆
PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2) -> {
return o1 - o2;//小根堆
});
PriorityQueue<Integer> pq2 = new PriorityQueue<>((o1, o2) -> {
return o2 - o1;//大根堆
});
// 队列 双端队列 的创建 主要在于前面的接口 Queue还是Deque
Queue<Integer> queueSingle = new ArrayDeque<>();
Deque<Integer> queueDouble = new ArrayDeque<>();
queueSingle.add(1);
queueDouble.add(2);
queueSingle.offer(3);
queueDouble.offerFirst(2);
// 链表
List<Integer> linkedlist = new LinkedList<>();
// 栈
Stack<Integer> stack = new Stack<>();
// A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class. For example:
// Deque<Integer> stack = new ArrayDeque<Integer>();
// Since: JDK1.0
// Author: Jonathan Payne
// stack的源码中 推荐使用双端队列 来代替栈
Deque<Integer> s = new ArrayDeque<>();
s.addLast(1);// 代替 push
s.pollLast();// 代替 pop
Integer last = s.peekLast();// 代替 peek
}
Java中堆 链表 队列 栈的实现汇总
最新推荐文章于 2022-08-31 14:21:09 发布