1.队列:

  1. 除啦一些并发队列,队列在集合中的实现有LinkedListPriorityQueue两种<br />
  1. public class QueueBehavior {
  2. private static int count=10;
  3. static <T> void test(Queue<T> queue, Gererator<T> gen) {
  4. for (int i = 0; i <count ; i++) {
  5. queue.offer(gen.next());
  6. }
  7. while(queue.peek()!=null){
  8. System.out.print(queue.remove()+" ");
  9. }
  10. System.out.println();
  11. }
  12. static class Gen implements Gererator<String>{
  13. String [] s=("one two three four five six seven "+"eight nine ten").split(" ");
  14. int i;
  15. @Override
  16. public String next() {
  17. return s[i++];
  18. }
  19. }
  20. public static void main(String[] args) {
  21. test(new LinkedList<String>(),new Gen());
  22. test(new PriorityQueue<String>(),new Gen());
  23. //以下是并发队列
  24. test(new ArrayBlockingQueue<String>(count),new Gen());
  25. test(new ConcurrentLinkedQueue<String>(),new Gen());
  26. test(new LinkedBlockingDeque<String>(),new Gen());
  27. test(new PriorityBlockingQueue<String>(),new Gen());
  28. }
  29. }

2.优先队列

优先队列中最有器的是todo列表,该列表的每个对象都包含一个字符串和一个主要的以及次要的优先值。该列表的顺序也是通过实现Comparable而进行控制

  1. public class TodList extends PriorityQueue<TodList.ToDoItem> {
  2. static class ToDoItem implements Comparable<ToDoItem> {
  3. private char primary;
  4. private int secondary;
  5. private String item;
  6. public ToDoItem(String td, char pri, int sec) {
  7. primary = pri;
  8. item = td;
  9. secondary = sec;
  10. }
  11. public int compareTo(ToDoItem arg) {
  12. if (primary > arg.primary)
  13. return +1;
  14. if (primary == arg.primary)
  15. if (secondary > arg.secondary)
  16. return +1;
  17. else if (secondary == arg.secondary)
  18. return 0;
  19. return -1;
  20. }
  21. @Override
  22. public String toString() {
  23. return Character.toString(primary)+secondary+":"+item;
  24. }
  25. }
  26. public void add(String td,char pri,int sec){
  27. super.add(new ToDoItem(td, pri, sec));
  28. }
  29. public static void main(String[] args) {
  30. final TodList toDoItems = new TodList();
  31. toDoItems.add("Empy trash",'C',4);
  32. while (!toDoItems.isEmpty()){
  33. System.out.println(toDoItems.remove());
  34. }
  35. }
  36. }
  1. <br />3.双向队列(Deque):<br /> 双向队列Deque,使你可以在队列的两端都可以添加或移除元素,Linkedist中包含支持双向队列的方法,因此可以使用LinkedList来组合来创建一个Deque类<br /> 示列,LinkedList组合Deque
  1. public class Deque<T> {
  2. private LinkedList<T> deque = new LinkedList<T>();
  3. public void addFirst(T e) { deque.addFirst(e); }
  4. public void addLast(T e) { deque.addLast(e); }
  5. public T getFirst() { return deque.getFirst(); }
  6. public T getLast() { return deque.getLast(); }
  7. public T removeFirst() { return deque.removeFirst(); }
  8. public T removeLast() { return deque.removeLast(); }
  9. public int size() { return deque.size(); }
  10. public String toString() { return deque.toString(); }

测试:

  1. public class DequeTest {
  2. static void fillTest(Deque<Integer> deque) {
  3. for (int i = 20; i < 27; i++)
  4. deque.addFirst(i);
  5. for (int i = 50; i < 55; i++)
  6. deque.addLast(i);
  7. }
  8. public static void main(String[] args) {
  9. Deque<Integer> di = new Deque<Integer>();
  10. ArrayDeque<Integer> d=new ArrayDeque<>();
  11. fillTest(di);
  12. System.out.println(di);
  13. while (di.size() != 0)
  14. System.out.println(di.removeFirst() + " ");
  15. System.out.println();
  16. fillTest(di);
  17. while (di.size() != 0)
  18. System.out.println(di.removeLast() + " ");
  19. }
  20. }