Collection接口的子接口——Queue接口\Deque接口\Stack類

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"));  //1
        stack.push("B");
        System.out.println(stack.search("A"));  //2
        System.out.println(stack.toString());

        System.out.println(stack.size());       //2
        System.out.println(stack.capacity());   //10

        String B = stack.pop();
        System.out.println(B);
        System.out.println(stack.toString());
        System.out.println(stack.search("A"));  //1
        System.out.println(stack.search("X"));  //-1

        for (int i = 0; i < 9; i++) {
            stack.push(i+"");
        }
        System.out.println(stack.toString());   //[A, 0, 1, 2, 3, 4, 5, 6, 7, 8]
        System.out.println(stack.size());       //10
        System.out.println(stack.capacity());   //10

        stack.push("9");
        System.out.println(stack.toString());   //[A, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        System.out.println(stack.size());       //11
        System.out.println(stack.capacity());   //20

        stack.ensureCapacity(100);
        System.out.println(stack.size());       //11
        System.out.println(stack.capacity());   //100

        stack.trimToSize();
        System.out.println(stack.size());       //11
        System.out.println(stack.capacity());   //11

        stack.insertElementAt("B", 5);  //這是父類Vector的方法,stack不用纔好。
        System.out.println(stack.toString());   //[A, 0, 1, 2, 3, B, 4, 5, 6, 7, 8, 9]

//      stack.removeAllElements();
//      System.out.println(stack.toString());   //[]

        String[] anArray = new String[stack.size()];
        stack.copyInto(anArray);

        for (int i = 0; i < anArray.length; i++) {
            System.out.println("anArray["+i+"]"+anArray[i]);
        }

        stack.sort(null);   //我呢個天來
        System.out.println(stack.toString());       //[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B]

        Stream<String> a = stack.parallelStream();  //Collection方法(JDK1.8新加)
        System.out.println(a);      //java.util.stream.ReferencePipeline$Head@6d06d69c
        Stream<String> ccc = stack.stream();
        System.out.println(ccc);    //java.util.stream.ReferencePipeline$Head@7852e922

//      Predicate<String> filter = (s) -> s.length()<2;
        Predicate<String> filter = (s) -> Pattern.compile("[0-9]*").matcher(s).matches();
        stack.removeIf(filter);     //JDK1.8新加
        System.out.println(stack.toString());   //[A, B]
    }


    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");  //從last加
        System.out.println(deck);

        System.out.println("============Offer==============");
        deck.offer("offer");    //從last加
        System.out.println(deck);
        deck.offerFirst("offerFirst");
        System.out.println(deck);
        deck.offerLast("offerLast");
        System.out.println(deck);   //[offerFirst, addFirst, add, addLast, addNewOne, offer, offerLast]

        System.out.println("============get==============");
        System.out.println(deck.getFirst());//offerFirst
        System.out.println(deck.getLast()); //offerLast
        System.out.println("============peek==============");
        System.out.println(deck.peek());    //offerFirst
        System.out.println(deck.peekFirst());//offerFirst
        System.out.println(deck.peekLast());//offerLast

        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"));    //從隊頭開始,移除第一個出現的指定元素,返回true
        System.out.println(deck);
        System.out.println(deck.removeFirstOccurrence("?"));    //沒有,就不刪,返回false
        System.out.println(deck);

        System.out.println(deck.removeLast());
        System.out.println(deck);
        System.out.println(deck.removeLastOccurrence("add"));   //從隊尾開始,移除第一個出現的指定元素,返回true
        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));   //JDK1.8新加方法 
            /*
             * Removes all of the elements of this collection that satisfy the given predicate.
             */
        System.out.println(deck);
    }


    private static void myQueueLearn(){
        Queue<String> queue = new PriorityQueue<String>();  //會接排序[a1, a2, c1, b2, b1, c2]
        //Queue<String> queue = new ArrayBlockingQueue<String>(10); //正常插入順序[a2, b2, c2, a1, b1, c1]

        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());    
                /*
                 * 若隊列中沒有元素,則報異常
                 * Exception in thread "main" java.util.NoSuchElementException
                 */
        System.out.println(queue.peek());   //沒有元素時,返回null

//      queue.add("c2");
//      queue.add("d2");
//      queue.add("e2");
//      queue.add("f2");
//      queue.add("g2");
            /*
             * 隊列滿,報異常
             * Exception in thread "main" java.lang.IllegalStateException: Queue full
             */
        queue.offer("a3");      //若是PropertyQueue則會是如下順序:[a2, b1, a3, b2, c1, c2]
        System.out.println(queue.toString());
        System.out.println(queue.remove());
        System.out.println(queue.toString());
        System.out.println(queue.poll());   //隊列爲空,則返回null
        System.out.println(queue.toString());

        queue.offer(null);      //隊列不允許插入null值
        System.out.println(queue.toString());
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章