集合
队列和双端队列
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成神的系列文章之一
如果你想知道,但是本文没有的,请下方留言
我会第一时间总结出来并发布填充到本文