首页 > 其他分享 >集合5 - Queue

集合5 - Queue

时间:2023-01-03 17:48:32浏览次数:35  
标签:queue System Queue println 集合 poll out

Queue - ArrayBlockingQueue

底层实现 -- 数组

Array(数组) - Blocking(创建对象时的容量,多了就堵塞) - Queue(先进先出)

public class _Queue {
    public static void main(String[] args) throws Exception{

        //TODO Collection Queue
        ArrayBlockingQueue queue = new ArrayBlockingQueue(3);

        System.out.println("第一个数据");
        queue.add("hehe");
        System.out.println("第二个数据");
        queue.add("haha");
        System.out.println("第三个数据");
        queue.add("hoho");
        //queue.add("hoho");  add方法超过容量并不会触发Blocking,而是直接报错

        System.out.println("第四个数据");
        //queue.put("oooo");
        //.put()方法 超过容量后会堵塞,程序会持续等待空闲容量

        System.out.println(queue.offer("oooo")); //false
        //.offer()方法 超过容量后返回false

        //TODO .poll() .take() 取数据
        System.out.println(queue.poll()); //最先入队的数据被删除
        System.out.println(queue.poll());
        //System.out.println(queue.poll());
        //System.out.println(queue.poll()); //空队列返回null

        System.out.println(queue.take());
        System.out.println(queue.take());  //空队列进入等待状态

        System.out.println(queue);
    }
}

标签:queue,System,Queue,println,集合,poll,out
From: https://www.cnblogs.com/Ashen-/p/17022943.html

相关文章

  • 集合4 HashSet
    HashSet底层实现是数组+链表数据存放时的索引通过内部Hash算法确定--增加数据是无序的Hash算法--索引相同时根据内容是否相同决定是否丢弃,内容不相同则采用链地址......
  • java集合Collection操作
    CollectionallCollections=newArrayList();//集合里添加数据allCollections.add("testName");//判断即合理是否包含某特定的数据if(al......
  • 集合3 LinkedList
    LinkedList底层双向链表基础增删改查publicclass_LinkedList{publicstaticvoidmain(String[]args){//TODO集合-Collection-LinkedList=......
  • 集合1 Collection
    集合Collection数据的一种容器,用于容纳数据Java提供了完整的集合框架数组也是容器,为什么要使用集合?数组使用不方便数组索引范围由长度确定不能动态修改容量什......
  • 集合2 ArrayList
    ArrayListpublicclassList{publicstaticvoidmain(String[]args){//TODOArrayList:Array+List//List:列表,清单--按照数据插入......
  • L2-005 集合相似度
    L2-005 集合相似度 (25 分)给定两个整数集合,它们的相似度定义为:Nc/Nt×100%。其中Nc是两个集合都有的不相等整数的个数,Nt是两个集合一共有的不相等整数的个数。你的任务就......
  • 数据结构 玩转数据结构 7-9 Leetcode上更多集合和映射的问题
    0课程地址https://coding.imooc.com/lesson/207.html#mid=13711 1重点关注1.1结论3.1和3.2对比3.2空间复杂度优于3.1 3.3和3.4对比......
  • 当下适合男人用的智能手机集合-转
    哪些智能手机适合男人使用呢?哪些安卓手机具有男人气息呢?从低价位的1000到中高价位的4000均有推荐,有兴趣的朋友不妨考虑一下。 HTC7Trophy,也叫奖杯,WP7系统,1000......
  • 第十三章《集合》第5节:Map集合
    ​List、Set和Queue都是Collection接口的子接口,因此从更高层次来说它们属于统一类型的集合。Map接口也代表一种集合,但它不是Collection子接口,因此它属于另一种类型的集合。M......
  • 第十三章《集合》第6节:使用Collections类操作集合
    ​Java提供了一个操作集合的工具类Collections,这个类中提供了大量方法对集合元素进行排序、查询和修改等操作,此外还提供了将集合对象设置为不可变、对集合对象实现同步控制......