zoukankan      html  css  js  c++  java
  • java成神之——集合框架之队列,栈,集合并发

    集合

    队列和双端队列

    PriorityQueue

    此队列第一个元素永远是最小的,先进先出
    PriorityQueue<Integer> queue = new PriorityQueue<Integer>();
    

    Deque

    双端队列
    Deque<String> dequeA = new LinkedList<>();
    dequeA.add("element 1"); 
    dequeA.addFirst("element 2");
    dequeA.addLast("element 3");
    dequeA.element(); // 获取队头元素
    dequeA.getFirst();
    dequeA.getLast();
    dequeA.remove();
    dequeA.removeFirst();
    dequeA.removeLast();
    

    BlockingQueue

    线程阻塞队列
    当你出队队空或者进队队满,会造成当前线程阻塞,除非此时其他的线程进队或者出队,才会继续运行
    ArrayBlockingQueue
    LinkedBlockingQueue
    PriorityBlockingQueue
    
    BlockingQueue<String> bQueue = new ArrayBlockingQueue<String>(2);
    

    Queue

    Queue<String> queue = new LinkedList<String>();
    queue.offer( "first element" ); // 入队
    queue.offer( "second element" );
    while ( !queue.isEmpty() ) {
        System.out.println( queue.poll() ); // 出队
    }
    

    先进后出
    
    Stack st = new Stack();
    st.push(10);
    st.pop();
    

    集合并发

    线程锁

    List<String> threadSafeList = Collections.synchronizedList(new ArrayList<String>());
    Set<String> threadSafeSet = Collections.synchronizedSet(new HashSet<String>());
    Map<String, String> threadSafeMap = Collections.synchronizedMap(new HashMap<String, String>());
    

    线程安全集合

    List<String> threadSafeList = new CopyOnWriteArrayList<String>();
    Set<String> threadSafeSet = new ConcurrentHashSet<String>();
    
    Map<String, String> threadSafeMap = new ConcurrentHashMap<String, String>();
    String previousValue = threadSafeMap.putIfAbsent("a", "1");
    

    结语

    本文章是java成神的系列文章之一
    
    如果你想知道,但是本文没有的,请下方留言
    
    我会第一时间总结出来并发布填充到本文
    
  • 相关阅读:
    Apache Kylin v3.0.0-alpha 发布
    Apache Kylin在美团点评的应用
    Kylin 架构模块简介
    Kylin 1 背景、历史与使命
    谈MongoDB的应用场景
    Linux 内存Cache和Buffer理解
    Linux 下查看内存使用情况方法总结
    mongodb 集群配置文件
    MongoDB bindIp 与 bindIpAll
    MongoDB 权限认证
  • 原文地址:https://www.cnblogs.com/ye-hcj/p/9724543.html
Copyright © 2011-2022 走看看