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());
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老陈头7

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值