Skip to content

Java Related

hyjing edited this page May 26, 2019 · 2 revisions

Primitives vs. References

  1. primitive types are the basic types of data
  • byte, short, int, long, float, double, boolean, char
  • primitive variables store primitive values
  1. reference types are any instantiable class as well as arrays
  • String, Scanner, Random, Die, int[], String[], etc.
  • reference variables store addresses

PriorityQueue in Java

based on the priority heap, First-In-First-Out algorithm, lowest to highest

  • PriorityQueue doesn’t permit NULL pointers.
  • We can’t create PriorityQueue of Objects that are non-comparable
  • PriorityQueue are unbound queues.
  • The head of this queue is the least element with respect to the specified ordering. If multiple elements are tied for least value, the head is one of those elements — ties are broken arbitrarily.
  • The queue retrieval operations poll, remove, peek, and element access the element at the head of the queue.
  • It inherits methods from AbstractQueue, AbstractCollection, Collection and Object class.
PriorityQueue<ListNode> queue = new PriorityQueue<ListNode>(lists.length, (a, b) -> a.val - b.val);

or

PriorityQueue<ListNode> queue= new PriorityQueue<ListNode>(lists.size(),new Comparator<ListNode>(){
    @Override
    public int compare(ListNode o1,ListNode o2){
        if (o1.val<o2.val)
           return -1;
        else if (o1.val==o2.val)
           return 0;
        else 
           return 1;
        }
   });

Clone this wiki locally