I came across a question where I have to store to pair in PriorityQueue in Java. But Java does not have anything like make_pair in C++. What can be the alternate used in Java for making a pair?
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3676 |
| 2 | Kevin114514 | 3659 |
| 3 | jiangly | 3640 |
| 4 | turmax | 3450 |
| 5 | tourist | 3408 |
| 6 | strapple | 3360 |
| 7 | Um_nik | 3354 |
| 8 | squareOf105 | 3348 |
| 9 | maroonrk | 3312 |
| 10 | idontwannadothis…e | 3191 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 154 |
| 2 | nik_exists | 151 |
| 3 | maspy | 149 |
| 4 | AmShZ | 146 |
| 5 | Um_nik | 143 |
| 6 | Errichto | 139 |
| 7 | BledDest | 136 |
| 8 | adamant | 135 |
| 9 | maroonrk | 133 |
| 10 | cry | 130 |
I came across a question where I have to store to pair in PriorityQueue in Java. But Java does not have anything like make_pair in C++. What can be the alternate used in Java for making a pair?
| Name |
|---|



The simplest way would probably be making your own pair class and defining a custom comparator.
Still not available in Java standard library. So all you need is to switch to Scala :)
That’s a weird way to spell C++.
Lol. If that was spelt C++, you would probably see another blog post titled "alternative for BigInteger in C++"
You need to create a java class for pair an implementation is given below :
class pair implements Comparable{ int a,b;
public pair(int a, int b) { this.a = a; this.b = b; } public int compareTo(pair o) { if(this.a==o.a) return this.b - o.b; return this.a - o.a; } }pair class implements comparable interface because priority queue will use this interface compareTo method to compare elements.
you can define priority queue as follows
PriorityQueue<pair> pq = new PriorityQueue<>(); pq.add(new pair(1,2));Got it!. Thanks.
Abit weird method but if the numbers in your pair are small enough, you insert then into your priority queue by inserting a*(constant)+b.