Queue接口

  队列是一个典型的先进先出(FIFO)的容器。即从容器的一段放入事物,从另一端取出,并且事物放入容器的顺序与取出的顺序是相同的。
  LinkedList实现了Queue接口,因此LinkedList可用作Queue的一种实现。Queue接口限制了LinkedList里的一些方法,只有队列所需的方法才允许被使用(接口的作用嘛)。

import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;

public class QueueTest {
    public static void main(String[] args){
        Queue<Integer> integers = new LinkedList<Integer>();
        Queue<Character> characters = new LinkedList<Character>();
        Random rand = new Random();

        for (int i = 0; i < 10; i++) {
            integers.offer(rand.nextInt(100));
        }

        while (integers.peek()!=null) System.out.print(integers.poll());

        System.out.println();

        for (char c : "No Country for Old Men".toCharArray()) {
            characters.offer(c);
        }
        while (characters.peek()!=null) System.out.print(characters.poll()+" ");
    }
}
//173198386858439895
//N o   C o u n t r y   f o r   O l d   M e n 

PriorityQueue<>

  优先级队列声明下一个弹出的元素为优先级最高的元素。
  方法iterator() 中提供的迭代器不 保证以任何特定的顺序遍历优先级队列中的元素。

import java.util.PriorityQueue;
import java.util.Random;

public class PriorityQueueTest {
    public static void main(String[] args){
        Random rand = new Random();
        PriorityQueue<Integer> queue = new PriorityQueue<Integer>();

        for (int i = 0; i < 10; i++) {
            queue.offer(rand.nextInt(1000));
        }

        for (int i : queue) {
            System.out.print(i+" ");
        }
    }
}

// 292 457 395 563 526 624 639 603 804 852 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值