Java Priority queue Considerations __ Humanoid robot

Source: Internet
Author: User

In the C + + language, a lambda expression is constructed directly using a precedence queue, using an anonymous function pointer. The return value of the Java comparison function is not a bool type, only an integral type.

Internal corresponding C + + anonymous function:

Anonymous Comparator implementation

Auto Comparemax = [] (const cell &a, const cell &b) {return A.max < B.max;};

Corresponding Java functions:

Import Java.util.Queue;
Import Java.util.Comparator;
Import Java.util.PriorityQueue;

    Anonymous Comparator implementation public
   static comparator<cell> Comparemax = new comparator<cell> () {
        @Override public
        int Compare (cell C1, cell C2) {
            if (C1.max < C2.max) return
                1;
            else
                return-1;
        }
    ;

Anonymous comparison function implementation, Java uses int, the return value is 1 and -1,c++ can use the Boolean type. Should actually write a lambda expression

Use of Java precedence queues:

queue<cell> cellqueue = new Priorityqueue (Comparemax);

Corresponds to the use of C + + priority queues:
Using Queue = std::p riority_queue< cell<t>, std::vector<cell<t>, Decltype (Comparemax) >;
queue<cell> cellqueue = new Priorityqueue (Comparemax);


Implements a function that, when inserted, can be used to sort inserts using C.max.

Reference: Lambda expressions for Java functional programming

Anonymous classes implement anonymous functions:

public void Testanonymousclass () {
    integer[] nums = {2, 5, 1, 6};
    Arrays.sort (Nums, New comparator<integer> () {
        @Override public
        int compare (integer o1, integer o2) {
            if (O1 < O2)
                return-1;
            return 0;
        }
    });

    for (Integer n:nums) {
        System.out.println (n);
    }
}

Lambda expression:

public void Testanonymousclass () {
    integer[] nums = {2, 5, 1, 6};
    Arrays.sort (Nums, (O1, O2)-> {
        if (O1 < O2)
            return-1;
        return 0;
    });
    for (Integer n:nums) {
        System.out.println (n);
    }
}

Reference: Use of queue in Java

The queue interface, which is the same level as list and set, inherits the collection interface. LinkedList implements the queue interface. The queue interface narrows access to LinkedList methods (that is, when the parameter type in the method is a queue, you can only access the method defined by the queue interface, not directly to the LinkedList method). So that only the right method can be used. Blockingqueue inherits the queue interface.

AddAdd a dollar cable if the queue is full, throw a Iiiegaislabeepeplian exception
RemoveRemove and return elements of the queue header if the queue is empty, throw a nosuchelementexception exception
elementElements that return the header of the queue if the queue is empty, a Nosuchelementexception exception is thrown
OfferAdd an element and return true if the queue is full, return false
PollRemove and return the elements of the queue header if the queue is empty, return null
PeekReturns the element of the queue's head returns null if the queue is empty
putAdd an element block if the queue is full
TakeThe element that removes and returns the header of the queue if the queue is empty, the block

Remove, element, offer, poll, Peek actually belong to the queue interface.

Reference: Java8 teach you how to write lambda expressions



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.