Java PriorityQueue create custom PriorityQueue to ignore duplicate values

Description

Java PriorityQueue create custom PriorityQueue to ignore duplicate values

import java.util.PriorityQueue;

public class Main {
  public static void main(String args[]) {
    PriorityQueue<Integer> p = new MyQueue<Integer>();
    p.add(1);// ww  w .  ja v a2s .c om
    p.add(2);
    p.add(1);
    for (int i = 0; i <= 2; i++) {
      System.out.println(p.poll());
    }

  }
}

class MyQueue<E> extends PriorityQueue<E> {
  public boolean add(E e) {
    boolean isAdded = false;
    if (!super.contains(e)) {
      isAdded = super.add(e);
    }
    return isAdded;
  }

}



PreviousNext

Related