A
A
Atom6562021-04-28 18:06:48
Java
Atom656, 2021-04-28 18:06:48

I wrote the code and I need to add the PriorityQueue class, but I don't know if I wrote the class correctly?

Please help, I wrote the code and I need to add the PriorityQueue class, but I don’t know if I wrote the class correctly - please tell me.
Here is my code that I wrote.

package com.company;

import java.util.*;

public class Main {

    public static class PriorityQueueExample {
        public static void main(String[] args) {
            PriorityQueue<Integer> PQueue = new PriorityQueue<>();
            PriorityQueue<String> PQueue2 = new PriorityQueue<>();
            PQueue2.add("BMW");
            PQueue.add(25);
            PQueue2.add("Mazda");
            PQueue.add(35);
            PQueue2.add("Tesla");
            PQueue.remove(10);
            PQueue2.remove("Tesla");
            Iterator iterator = PQueue.iterator();
            while (iterator.hasNext()) {
                System.out.println(iterator.next() + " ");
            }
            Iterator iterator2 = PQueue2.iterator();
            while (iterator2.hasNext()) {
                System.out.println(iterator2.next() + " ");
            }
            System.out.println("Повертає - " + PQueue + PQueue2);
            System.out.println("Видаляэ - " + PQueue.poll() + "," + PQueue2.poll());
            System.out.println("Фінальна стадія" + PQueue + PQueue2);
            Integer element = PQueue.peek();
            String element2 = PQueue2.peek();
            System.out.println("Доступні елементи- " + element + element2);
            System.out.println("Колекція-" + PQueue + PQueue2);
            System.out.println(" ");
            System.out.println("Чи колекція є пуста? " + PQueue.isEmpty() + "," + PQueue2.isEmpty());
            System.out.println("Розмір черги- " + PQueue.size() + " && " + PQueue2.size());
            System.out.println(" ");
            System.out.println("Чи є констаната Mazda і 35? " + PQueue.contains(35) + "," + PQueue2.contains("Mazda"));
            System.out.println("Чи є констаната Nissan і 55? " + PQueue.contains(55) + "," + PQueue2.contains("Nissan"));
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-04-28
@Atom656

Good afternoon!

but I don't know if I wrote the class correctly - tell me please.

Well, basically the code is working. Another thing is that the behavior of the code may differ from what you expect. I won't tell you here, because I don't know your ultimate goal.
As for the code:
public class Main {
    public static class PriorityQueueExample {

This is certainly not an error, but I don’t see the point in this case in a static class either.
You can remove the Main class
public class PriorityQueueExample {
PriorityQueue<Integer> PQueue = new PriorityQueue<>();

Names of variables according to the naming convention with a small letter. For example, pQueue, and even better if they are conscious (readable). For example, instead of PQueue2, use carQueue or carModelQueue, etc.
PQueue2.add("BMW");
            PQueue.add(25);
            PQueue2.add("Mazda");
            PQueue.add(35);
            PQueue2.add("Tesla");

            PQueue.remove(10);
            PQueue2.remove("Tesla");

You have added 2 Mazda & Tesla items, 25 & 35, but you are removing an item that is not in the queue (10, Tesla). More precisely, you added Tesla, but you didn’t add 10. Perhaps this is just an oversight.
It is possible that if I see a task (task), I can say for sure how correctly you implemented it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question