Queue队列
- 队列是FIFO(先进先出)结构,队尾入队首出。(队伍长度问题)。
- 3对方法:增加(add、offer)、移出(remove、poll)、查看(peek、element)。
- 方法“报异常”和“返回null”,是不同的情况。要区分。
- 不能插入null值。
- 其它方法继承Collection接口。
Deque双端(Double End Queue)队列
- 双端队列可用作FIFO(先进先出)队列,也可用作 LIFO(后进先出)堆栈。要作“堆栈”用,应优先使用此接口!(JDK原话)
堆栈方法 | 等效 Deque 方法 |
---|
push(e) | addFirst(e) |
pop() | removeFirst() |
peek() | peekFirst() |
- 12个方法 = 3种操作(增加、移出、查看)* 2端操作(队首、队尾) * 2种返回(异常、null)
| 第一个元素(头部) | 最后一个元素(尾部) |
---|
| 抛出异常\特殊值 | 抛出异常\ 特殊值 |
插入 | addFirst(e)\offerFirst(e) | addLast(e)\offerLast(e) |
移除 | removeFirst()\pollFirst() | removeLast()\pollLast() |
检查 | getFirst()\peekFirst() | getLast()\peekLast() |
- 不建议插入null值,但没做强制限制。
- 其它方法继承Queue、Collection接口。
Stack类
Stack 类表示后进先出(LIFO)的对象堆栈。
- 继承Vector类
- 五个特殊方法:peek、pop、push(O)、serch(O)。分别是查看堆栈顶对象,移除栈顶对象,压入栈顶一个对象,查看某对象到栈顶的距离。
反正,我以后是不用Stack类做堆栈了
package collection
import java.util.ArrayDeque
import java.util.Deque
import java.util.PriorityQueue
import java.util.Queue
import java.util.Stack
//import java.util.concurrent.ArrayBlockingQueue
import java.util.function.Predicate
import java.util.regex.Pattern
import java.util.stream.Stream
public class LearnQueue {
public static void main(String[] args) {
//myQueueLearn()
//myDequeLearn()
Stack<String> stack = new Stack<String>()
System.out.println(stack.empty())
stack.push("A")
System.out.println(stack.toString())
System.out.println(stack.empty())
System.out.println(stack.search("A"))
stack.push("B")
System.out.println(stack.search("A"))
System.out.println(stack.toString())
System.out.println(stack.size())
System.out.println(stack.capacity())
String B = stack.pop()
System.out.println(B)
System.out.println(stack.toString())
System.out.println(stack.search("A"))
System.out.println(stack.search("X"))
for (int i = 0
stack.push(i+"")
}
System.out.println(stack.toString())
System.out.println(stack.size())
System.out.println(stack.capacity())
stack.push("9")
System.out.println(stack.toString())
System.out.println(stack.size())
System.out.println(stack.capacity())
stack.ensureCapacity(100)
System.out.println(stack.size())
System.out.println(stack.capacity())
stack.trimToSize()
System.out.println(stack.size())
System.out.println(stack.capacity())
stack.insertElementAt("B", 5)
System.out.println(stack.toString())
// stack.removeAllElements()
// System.out.println(stack.toString())
String[] anArray = new String[stack.size()]
stack.copyInto(anArray)
for (int i = 0
System.out.println("anArray["+i+"]"+anArray[i])
}
stack.sort(null)
System.out.println(stack.toString())
Stream<String> a = stack.parallelStream()
System.out.println(a)
Stream<String> ccc = stack.stream()
System.out.println(ccc)
// Predicate<String> filter = (s) -> s.length()<2
Predicate<String> filter = (s) -> Pattern.compile("[0-9]*").matcher(s).matches()
stack.removeIf(filter)
System.out.println(stack.toString())
}
private static void myDequeLearn() {
Deque<String> deck = new ArrayDeque<String>()
System.out.println("============Add==============")
deck.add("add")
System.out.println(deck)
deck.addFirst("addFirst")
System.out.println(deck)
deck.addLast("addLast")
System.out.println(deck)
deck.add("addNewOne")
System.out.println(deck)
System.out.println("============Offer==============")
deck.offer("offer")
System.out.println(deck)
deck.offerFirst("offerFirst")
System.out.println(deck)
deck.offerLast("offerLast")
System.out.println(deck)
System.out.println("============get==============")
System.out.println(deck.getFirst())
System.out.println(deck.getLast())
System.out.println("============peek==============")
System.out.println(deck.peek())
System.out.println(deck.peekFirst())
System.out.println(deck.peekLast())
System.out.println("============remove==============")
System.out.println(deck.remove())
System.out.println(deck)
System.out.println(deck.removeFirst())
System.out.println(deck)
System.out.println(deck.removeFirstOccurrence("offer"))
System.out.println(deck)
System.out.println(deck.removeFirstOccurrence("?"))
System.out.println(deck)
System.out.println(deck.removeLast())
System.out.println(deck)
System.out.println(deck.removeLastOccurrence("add"))
System.out.println(deck)
System.out.println(deck.removeLastOccurrence("?"))
System.out.println(deck)
//JDK1.8新类:java.util.function.Predicate
Predicate<String> p = (s) -> s.length() > 0
System.out.println("Predicate === "+p.test("foo"))
System.out.println("Predicate === "+p.test(""))
System.out.println(deck.removeIf(p))
System.out.println(deck)
}
private static void myQueueLearn(){
Queue<String> queue = new PriorityQueue<String>()
//Queue<String> queue = new ArrayBlockingQueue<String>(10)
queue.add("a2")
queue.add("b2")
queue.add("c2")
queue.add("a1")
queue.add("b1")
queue.add("c1")
// queue.offer("天王1")
// queue.offer("天地2")
// queue.offer("地动3")
// queue.offer("地面4")
// queue.offer("地林5")
// queue.offer("天空6")
// queue.offer("天狼7")
// queue.offer("天狗8")
// queue.offer("阿弥9")
// queue.offer("自由0")
//[地动, 地林, 天地, 天狗, 地面, 天空, 天狼, 天王, 阿弥陀, 自由]
//[地动3, 地林5, 天地2, 天狗8, 地面4, 天空6, 天狼7, 天王1, 阿弥陀9, 自由0]
//[地动3, 地林5, 天地2, 天狗8, 地面4, 天空6, 天狼7, 天王1, 阿弥9, 自由0]
//中文方面看不出排序方法,横竖撇捺折?
// queue.offer("一")
// queue.offer("丨")
// queue.offer("丿")
// queue.offer("丶")
// queue.offer("乙")
//[一, 丨, 丿, 丶, 乙]
// queue.offer("乙")
// queue.offer("丨")
// queue.offer("一")
// queue.offer("丶")
// queue.offer("丿")
//[一, 丶, 丨, 乙, 丿]
//完全看不懂,哪位研究一下?
System.out.println(queue.toString())
System.out.println(queue.element())
System.out.println(queue.peek())
// queue.add("c2")
// queue.add("d2")
// queue.add("e2")
// queue.add("f2")
// queue.add("g2")
queue.offer("a3")
System.out.println(queue.toString())
System.out.println(queue.remove())
System.out.println(queue.toString())
System.out.println(queue.poll())
System.out.println(queue.toString())
queue.offer(null)
System.out.println(queue.toString())
}
}