Transfer 2 generics in priority queue

I want to keep two things in the priority queue ... one is the number and the other is the cost. that is, I want to do the following:

PriorityQueue<Integer, Cost> q=new PriorityQueue<Integer, Cost>(); 

Cost is another class that I hav:

 class Cost implements Comparable<Cost> { String name; double cost; @Override public int compareTo(Cost s) { return Double.compare(cost, s.cost); } } 

Also, I want to perform comparisons only on the basis of value ... but I also want some integer identifier to be transmitted along with the value ... is there any way to achieve this?

I need to get the value based on id..so I use a hash map for it. When using the id field in value ... I want to get the entire cost instance based on this identifier field ... is it possible ... yes, then how?

I am new to Java programming. Can anyone suggest some way out?

+4
source share
4 answers

Change Cost class

 public class Cost implements Comparable<Cost> { String name; double cost; int id; public Cost(int id, String name, double cost) { this.id = id; this.name = name; this.cost = cost; } @Override public int compareTo(Cost s) { return Double.compare(cost, s.cost); } public int getId() { return this.id; } @Override public String toString() { return new StringBuilder().append("id : ").append(id).append( " name: ").append(name).append(" cost :").append(cost) .toString(); } } 

Then you can simply declare PriorityQueue of Const

 PriorityQueue<Cost> q=new PriorityQueue<Cost>(); 

Now that you want to find id based Cost , you can do below

 PriorityQueue<Cost> queue = new PriorityQueue<Cost>(); queue.add(new Cost(1, "one", 1)); queue.add(new Cost(2, "two", 2)); int id = 2;// Id to be found for (Cost cost : queue) { if (cost.getId() == 2) { System.out.println(cost); } } 
+1
source

The Cost object is a good start. Create an object containing both an integer and Cost , and put them in the priority queue. Or add an integer field to the Cost class.

0
source

You might want to wrap your integer and value in Map/HashMap , as shown below:

  PriorityQueue<Map<Integer, Cost>> q = new PriorityQueue<Map<Integer, Cost>>(); 

Now you can create a HashMap object and put two objects in it before inserting them into the queue.

In addition, you want to create a custom wrapper class , for example. CostNumber , which will have Integer and Cost as two member variables. After that, you can use this new object in the queue.

0
source

Since PriorityQueue stores one object, you need to do one of the following:

  • create a class containing both an integer and a cost object if the integer and cost are not related.
  • click the integer attribute as another member of the cost class, if they are related.

Also, I want to perform comparisons only on the basis of value ... but I also want some integer identifier to be transmitted along with the value ... is there any way to achieve this?

Why do you want to pass something for comparison? What you are not going to use during comparison? In any case, the signature of this method cannot be changed if you want to use the Comparator structure. You can add this integer identifier to your cost class as another member and thereby make it available during the execution of the compareTo method.

0
source

Source: https://habr.com/ru/post/1442441/


All Articles