예제 #1
0
 /**
  * Method to retain a Collection of elements (and remove all others).
  *
  * @param c The collection to retain
  * @return Whether they were retained successfully.
  */
 public synchronized boolean retainAll(java.util.Collection c) {
   boolean success = delegate.retainAll(c);
   if (success) {
     makeDirty();
     if (ownerOP != null && !ownerOP.getExecutionContext().getTransaction().isActive()) {
       ownerOP.getExecutionContext().processNontransactionalUpdate();
     }
   }
   return success;
 }
  public void methodsOfPQ() {
    PriorityQueue<Integer> pq = new PriorityQueue<Integer>(); // Default Constructor:
    //		Queue<Integer> pq = new PriorityQueue<Integer>();
    pq.add(10);
    pq.add(30);
    pq.add(50);
    pq.add(40);
    pq.add(20);
    System.out.println(pq); // [10, 20, 50, 40, 30]

    pq.offer(75); // add element randomly anywhere
    System.out.println(pq); // [10, 20, 50, 40, 30, 75]

    System.out.println(pq.peek()); // 10 | return head of queue and don't touch pq
    System.out.println(pq); // [10, 20, 50, 40, 30, 75]

    System.out.println(pq.poll()); // 10 | return head and also removes head from pq
    System.out.println(pq); // [20, 50, 40, 30, 75]

    System.out.println(pq.element()); // 20 | same as peek but throw exception if pq is empty
    System.out.println(pq); // [20, 50, 40, 30, 75]

    pq.remove(); // 20 | same as poll but throw exception when queue is empty
    System.out.println(pq); // [50, 40, 30, 75]

    List<Integer> ls = new ArrayList<Integer>();
    ls.add(1000);
    ls.add(2000);

    pq.addAll(ls);
    System.out.println(pq); // [30, 40, 50, 75, 1000, 2000]

    pq.removeAll(ls);
    System.out.println(pq); // [30, 40, 50, 75]

    ls.clear(); // empty ls
    ls.add(101);
    ls.add(102);
    pq.addAll(ls);
    pq.retainAll(ls); // all element will be removed except ls
    System.out.println(pq); // [101, 102]

    System.out.println(pq.containsAll(ls)); // true
    System.out.println("********************************************");

    //		Queue<String> q=new PriorityQueue<String>();
  }