This question already has answers here:
Java Hashset.contains() produces mysterious result
(3 answers)
Closed 4 years ago.
I have a doctor object and each object has a unique attribute "docMobile"(primary key). I made two different LinkedHashSets (doctorsByLoc & doctorsByAvail) of doctors. Now when i do a
doctorsByLoc.retainAll(doctorsByAvail)
on the two sets it deletes all the elements even though both have the same doctors.
I have implemented hashCode() Method in my doctor class. I also printed the sets individually to check in the sets have same elements.
public class Doctor{
String docName;
long docMobile;
String docLocation;
int docDays;
#Override
public int hashCode() {
return Long.hashCode(docMobile);
}
}
Then somewhere in a servlet something like this happens
public static void main(String[] args){
Set<Doctor> doctorsByLoc = new LinkedHashSet<>();
Set<Doctor> doctorsByAvail = new LinkedHashSet<>();
doctorsByLoc.add(d1);
doctorsByLoc.add(d2);
doctorsByLoc.add(d3);
doctorsByAvail.add(d1);
doctorsByAvail.add(d2);
doctorsByAvail.add(d3);
System.out.println("Before retain All "+doctorsByLoc.size());
for(Doctor d:doctorsByLoc){
System.out.println(d.getdocName());
}
doctorsByLoc.retainAll(doctorsByAvail);
System.out.println("After retain All"+doctorsByLoc.size());
for(Doctor d:doctorsByLoc){
System.out.println(d.getdocName());
}
}
Actual output:
Before retain All 3
d1's name
d2's name
d3's name
After retain All 0
How can i fix my hashcode method so that the doctors remain.
I have tried printing the hashcode before returning it and I got pairs of similar hashcode as output.
You did not override equals correctly. You should be overriding it as follows:
#Override
public boolean equals (Object other) // Not "Doctor other"
{
// implementation here
}
This question already has answers here:
cannot be cast to java.lang.Comparable
(2 answers)
Closed 5 years ago.
I have the following class.
private static class Node {
public int id; // 0 indexed
public int distFromS;
Node(int id, int distFromS) {
this.id = id;
this.distFromS = distFromS;
}
}
I'm storing instances of Node in a PriorityQueue and manipulating them...
PriorityQueue<Node> procQueue = new PriorityQueue<Node>();
int[] distsFromS = new int[n];
Arrays.fill(distsFromS, -1);
// Read in s, add to processing queue
int s = (in.nextInt()) - 1; // 0 indexed
procQueue.add(new Node(s, 0));
// While queue isn't empty
while (procQueue.size() > 0) {
// deque "curr". If we haven't already reached curr from s
Node curr = procQueue.remove();
if (distsFromS[curr.id] == -1) {
// record distance.
distsFromS[curr.id] = curr.distFromS;
// enqueue all children of curr. distFromS = curr.distFromS + 6
Iterator<Integer> itr = edges[curr.id].iterator();
while(itr.hasNext()) {
procQueue.add(new Node(itr.next(), curr.distFromS + EDGE_WEIGHT)); // ***Exception is here***
}
}
}
But I'm getting the following Exception:
Exception in thread "main" java.lang.ClassCastException: Solution$Node cannot be cast to java.lang.Comparable
at java.util.PriorityQueue.siftUpComparable(PriorityQueue.java:652)
at java.util.PriorityQueue.siftUp(PriorityQueue.java:647)
at java.util.PriorityQueue.offer(PriorityQueue.java:344)
at java.util.PriorityQueue.add(PriorityQueue.java:321)
at Solution.main(Solution.java:52)
Do I need to implement compareTo for Node? Why? As far as I can tell I'm not doing any comparisons.
you need to make your class Node implement Comparable
private static class Node implements Comparable<Node> {
public int id; // 0 indexed
public int distFromS;
Node(int id, int distFromS) {
this.id = id;
this.distFromS = distFromS;
}
#Override
public int compareTo(Node another) {
// your codes here
}
}
or give a Comparator when constructing the PriorityQueue
PriorityQueue<Node> queue = new PriorityQueue<>(new Comparator<Node>() {
#Override
public int compare(Node o1, Node o2) {
// your codes here
}
});
From the docs:
An unbounded priority queue based on a priority heap. The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used. A priority queue does not permit null elements. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException).
You need to either specify a comparator or you class needs to be comparable.
Otherwise the PriorityQueue has no way of knowing which objects take priority.
Yeah you need to implement comparable.
https://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html#add(E)
throws
ClassCastException - if the specified element cannot be compared with
elements currently in this priority queue according to the priority
queue's ordering
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
Im trying to use the override comparator method of priorityqueue and i want to achieve the following:
i have the current list:
RG3
PR1
PR2
RG4
RG1
RG2
the RG refers to a regular person and the PR refers to a person with priority, the numbers represent the turns.
what i want is to get a First in first out order except when is a priority person wich will go to the top of the queue in his turn. so in the list i want the following result
PR1
PR2
RG1
RG2
RG3
RG4
heres what ive done until now:
Queue<Ficha> cola = new PriorityQueue<>(6, idComparator);
while (!list.isEmpty()) //this list is the unsorted list.
{
aux = list.remove(0);
cola.add(aux); // adds it to the priority queue
}
while(!cola.isEmpty())
{
aux = cola.poll();
System.out.println(aux.getCod_priority()+aux.getTurn()); // this shows me the order of the queue
}
}
public static Comparator<Ficha> idComparator = new Comparator<Ficha>()
{
#Override
public int compare(Ficha f1, Ficha f2) {
return (int) ((f1.getTurn()+prioridad(f1.getCod_priority())) - (f2.getTurn()+prioridad(f2.getCod_priority())));
}
};
private static long prioridad(String cod_priority) // this method i use it to give the cod_priority a int value to compare
{
if(cod_tipo_ficha=="PR")
{
return 10000;
}
else
{
return 1;
}
}
and when i run it i get the following order:
PR1
RG1
RG2
PR2
RG3
RG4
i know my problem is in the comparator method but i dont know how to achieve the queue that i want.
i know theres a lot of question related of how to compare but the only answers i see is when you compare strings. and this one i need to compare the priority string and the int.
Just change cod_tipo_ficha=="PR" to
if("PR".equals(cod_tipo_ficha)) {
...
}
Should work
This question already has answers here:
sort and group a java collection
(5 answers)
Closed 8 years ago.
Good morning. I have class, which contain data in this format: [int][Object]. I never worked with classes, i usually use Lists, Maps, now i don't understand how to implement this in common classes. How to sort by field in classes?
Be careful , my toString only showing field code without field number.
I would like to sort this class by size of value[Object]
( "1 1234", "2 35881", ... "7 22" --> "7 22", "1 1234", "2 35881")
public class Record {
private int number;
private Object code;
... Getters/Setters
#Override
public String toString() {
return (String)(this.code);
}
}
public class Storage
{
List<Record> record;
public Storage(){
this.record = new ArrayList<Record>();
}
public void addRecord(Record record) {
this.record.add(record);
}
public Record getRecord(int number){
return this.record.get(number);
}
public void delRecord(int number){
this.record.remove(number);
}
public Integer sizeStorage(){
return record.size();
}
}
public class Start {
public static void main(String[] args) {
System.out.println("Start reading from Xls");
ReaderXls read = new ReaderXls();
Storage storageRZS = readrzs.ReadXls("Text1obj",2,12);
.....
System.out.println(storageRZS.getRecord(5));
System.out.println(storageRZS.getRecord(7));
System.out.println(storageRZS.getRecord(10));
2122
189266
244
The result should be this:
244
2122
189266
If you wissh to access records in an ordered way you will need to use a comparator or implement comparable in your Record class.
Sample:
Collections.sort(listOfRecords, new Comparator<Record>() {
#Override
public int compare(Record o1, Record o2) {
//null checks
/*Compare the object field according to your custom logic.
Here it is assumed that getObjectCodeAsInt() will return an integer equivalent of the objectCode.*/
if(o1.getObjectCodeAsInt() > o2.getObjectCodeAsInt())
return 1;
else if(o1.getObjectCodeAsInt() < o2.getObjectCodeAsInt())
return -1;
return 0;
}
});
You will need to make a customer Comparator for the same. Read how to sort user defined objects
I am little unsure about the exactness of the question.
However if you wish to sort the collection such as list by different class attributes (fields as you say), you may look into Collections.sort
You will need to implement the Comparator interface for each of sorting attribute.
Regards
VJ
How do I get a PriorityQueue to sort on what I want it to sort on?
Also, is there a difference between the offer and add methods?
Use the constructor overload which takes a Comparator<? super E> comparator and pass in a comparator which compares in the appropriate way for your sort order. If you give an example of how you want to sort, we can provide some sample code to implement the comparator if you're not sure. (It's pretty straightforward though.)
As has been said elsewhere: offer and add are just different interface method implementations. In the JDK source I've got, add calls offer. Although add and offer have potentially different behaviour in general due to the ability for offer to indicate that the value can't be added due to size limitations, this difference is irrelevant in PriorityQueue which is unbounded.
Here's an example of a priority queue sorting by string length:
// Test.java
import java.util.Comparator;
import java.util.PriorityQueue;
public class Test {
public static void main(String[] args) {
Comparator<String> comparator = new StringLengthComparator();
PriorityQueue<String> queue = new PriorityQueue<String>(10, comparator);
queue.add("short");
queue.add("very long indeed");
queue.add("medium");
while (queue.size() != 0) {
System.out.println(queue.remove());
}
}
}
// StringLengthComparator.java
import java.util.Comparator;
public class StringLengthComparator implements Comparator<String> {
#Override
public int compare(String x, String y) {
// Assume neither string is null. Real code should
// probably be more robust
// You could also just return x.length() - y.length(),
// which would be more efficient.
if (x.length() < y.length()) {
return -1;
}
if (x.length() > y.length()) {
return 1;
}
return 0;
}
}
Here is the output:
short
medium
very long indeed
Java 8 solution
We can use lambda expression or method reference introduced in Java 8. In case we have some String values stored in the Priority Queue (having capacity 5) we can provide inline comparator (based on length of String) :
Using lambda expression
PriorityQueue<String> pq=
new PriorityQueue<String>(5,(a,b) -> a.length() - b.length());
Using Method reference
PriorityQueue<String> pq=
new PriorityQueue<String>(5, Comparator.comparing(String::length));
Then we can use any of them as:
public static void main(String[] args) {
PriorityQueue<String> pq=
new PriorityQueue<String>(5, (a,b) -> a.length() - b.length());
// or pq = new PriorityQueue<String>(5, Comparator.comparing(String::length));
pq.add("Apple");
pq.add("PineApple");
pq.add("Custard Apple");
while (pq.size() != 0)
{
System.out.println(pq.remove());
}
}
This will print:
Apple
PineApple
Custard Apple
To reverse the order (to change it to max-priority queue) simply change the order in inline comparator or use reversed as:
PriorityQueue<String> pq = new PriorityQueue<String>(5,
Comparator.comparing(String::length).reversed());
We can also use Collections.reverseOrder:
PriorityQueue<Integer> pqInt = new PriorityQueue<>(10, Collections.reverseOrder());
PriorityQueue<String> pq = new PriorityQueue<String>(5,
Collections.reverseOrder(Comparator.comparing(String::length))
So we can see that Collections.reverseOrder is overloaded to take comparator which can be useful for custom objects. The reversed actually uses Collections.reverseOrder:
default Comparator<T> reversed() {
return Collections.reverseOrder(this);
}
offer() vs add()
As per the doc
The offer method inserts an element if possible, otherwise returning
false. This differs from the Collection.add method, which can fail to
add an element only by throwing an unchecked exception. The offer
method is designed for use when failure is a normal, rather than
exceptional occurrence, for example, in fixed-capacity (or "bounded")
queues.
When using a capacity-restricted queue, offer() is generally preferable to add(), which can fail to insert an element only by throwing an exception. And PriorityQueue is an unbounded priority queue based on a priority heap.
Just pass appropriate Comparator to the constructor:
PriorityQueue(int initialCapacity, Comparator<? super E> comparator)
The only difference between offer and add is the interface they belong to. offer belongs to Queue<E>, whereas add is originally seen in Collection<E> interface. Apart from that both methods do exactly the same thing - insert the specified element into priority queue.
from Queue API:
The offer method inserts an element if possible, otherwise returning false. This differs from the Collection.add method, which can fail to add an element only by throwing an unchecked exception. The offer method is designed for use when failure is a normal, rather than exceptional occurrence, for example, in fixed-capacity (or "bounded") queues.
no different, as declare in javadoc:
public boolean add(E e) {
return offer(e);
}
Pass it a Comparator. Fill in your desired type in place of T
Using lambdas (Java 8+):
int initialCapacity = 10;
PriorityQueue<T> pq = new PriorityQueue<>(initialCapacity, (e1, e2) -> { return e1.compareTo(e2); });
Classic way, using anonymous class:
int initialCapacity = 10;
PriorityQueue<T> pq = new PriorityQueue<>(initialCapacity, new Comparator<T> () {
#Override
public int compare(T e1, T e2) {
return e1.compareTo(e2);
}
});
To sort in reverse order, simply swap e1, e2.
Just to answer the add() vs offer() question (since the other one is perfectly answered imo, and this might not be):
According to JavaDoc on interface Queue, "The offer method inserts an element if possible, otherwise returning false. This differs from the Collection.add method, which can fail to add an element only by throwing an unchecked exception. The offer method is designed for use when failure is a normal, rather than exceptional occurrence, for example, in fixed-capacity (or "bounded") queues."
That means if you can add the element (which should always be the case in a PriorityQueue), they work exactly the same. But if you can't add the element, offer() will give you a nice and pretty false return, while add() throws a nasty unchecked exception that you don't want in your code. If failure to add means code is working as intended and/or it is something you'll check normally, use offer(). If failure to add means something is broken, use add() and handle the resulting exception thrown according to the Collection interface's specifications.
They are both implemented this way to fullfill the contract on the Queue interface that specifies offer() fails by returning a false (method preferred in capacity-restricted queues) and also maintain the contract on the Collection interface that specifies add() always fails by throwing an exception.
Anyway, hope that clarifies at least that part of the question.
In here, We can define user defined comparator:
Below code :
import java.util.*;
import java.util.Collections;
import java.util.Comparator;
class Checker implements Comparator<String>
{
public int compare(String str1, String str2)
{
if (str1.length() < str2.length()) return -1;
else return 1;
}
}
class Main
{
public static void main(String args[])
{
PriorityQueue<String> queue=new PriorityQueue<String>(5, new Checker());
queue.add("india");
queue.add("bangladesh");
queue.add("pakistan");
while (queue.size() != 0)
{
System.out.printf("%s\n",queue.remove());
}
}
}
Output :
india
pakistan
bangladesh
Difference between the offer and add methods : link
As an alternative to using Comparator, you can also have the class you're using in your PriorityQueue implement Comparable (and correspondingly override the compareTo method).
Note that it's generally best to only use Comparable instead of Comparator if that ordering is the intuitive ordering of the object - if, for example, you have a use case to sort Person objects by age, it's probably best to just use Comparator instead.
import java.lang.Comparable;
import java.util.PriorityQueue;
class Test
{
public static void main(String[] args)
{
PriorityQueue<MyClass> queue = new PriorityQueue<MyClass>();
queue.add(new MyClass(2, "short"));
queue.add(new MyClass(2, "very long indeed"));
queue.add(new MyClass(1, "medium"));
queue.add(new MyClass(1, "very long indeed"));
queue.add(new MyClass(2, "medium"));
queue.add(new MyClass(1, "short"));
while (queue.size() != 0)
System.out.println(queue.remove());
}
}
class MyClass implements Comparable<MyClass>
{
int sortFirst;
String sortByLength;
public MyClass(int sortFirst, String sortByLength)
{
this.sortFirst = sortFirst;
this.sortByLength = sortByLength;
}
#Override
public int compareTo(MyClass other)
{
if (sortFirst != other.sortFirst)
return Integer.compare(sortFirst, other.sortFirst);
else
return Integer.compare(sortByLength.length(), other.sortByLength.length());
}
public String toString()
{
return sortFirst + ", " + sortByLength;
}
}
Output:
1, short
1, medium
1, very long indeed
2, short
2, medium
2, very long indeed
I was also wondering about print order. Consider this case, for example:
For a priority queue:
PriorityQueue<String> pq3 = new PriorityQueue<String>();
This code:
pq3.offer("a");
pq3.offer("A");
may print differently than:
String[] sa = {"a", "A"};
for(String s : sa)
pq3.offer(s);
I found the answer from a discussion on another forum, where a user said, "the offer()/add() methods only insert the element into the queue. If you want a predictable order you should use peek/poll which return the head of the queue."
Priority Queue has some priority assigned to each element, The element with Highest priority appears at the Top Of Queue. Now, It depends on you how you want priority assigned to each of the elements. If you don't, the Java will do it the default way. The element with the least value is assigned the highest priority and thus is removed from the queue first. If there are several elements with the same highest priority, the tie is broken arbitrarily. You can also specify an ordering using Comparator in the constructor PriorityQueue(initialCapacity, comparator)
Example Code:
PriorityQueue<String> queue1 = new PriorityQueue<>();
queue1.offer("Oklahoma");
queue1.offer("Indiana");
queue1.offer("Georgia");
queue1.offer("Texas");
System.out.println("Priority queue using Comparable:");
while (queue1.size() > 0) {
System.out.print(queue1.remove() + " ");
}
PriorityQueue<String> queue2 = new PriorityQueue(4, Collections.reverseOrder());
queue2.offer("Oklahoma");
queue2.offer("Indiana");
queue2.offer("Georgia");
queue2.offer("Texas");
System.out.println("\nPriority queue using Comparator:");
while (queue2.size() > 0) {
System.out.print(queue2.remove() + " ");
}
Output:
Priority queue using Comparable:
Georgia Indiana Oklahoma Texas
Priority queue using Comparator:
Texas Oklahoma Indiana Georgia
Else, You can also define Custom Comparator:
import java.util.Comparator;
public class StringLengthComparator implements Comparator<String>
{
#Override
public int compare(String x, String y)
{
//Your Own Logic
}
}
Here is the simple example which you can use for initial learning:
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
public class PQExample {
public static void main(String[] args) {
//PriorityQueue with Comparator
Queue<Customer> cpq = new PriorityQueue<>(7, idComp);
addToQueue(cpq);
pollFromQueue(cpq);
}
public static Comparator<Customer> idComp = new Comparator<Customer>(){
#Override
public int compare(Customer o1, Customer o2) {
return (int) (o1.getId() - o2.getId());
}
};
//utility method to add random data to Queue
private static void addToQueue(Queue<Customer> cq){
Random rand = new Random();
for(int i=0;i<7;i++){
int id = rand.nextInt(100);
cq.add(new Customer(id, "KV"+id));
}
}
private static void pollFromQueue(Queue<Customer> cq){
while(true){
Customer c = cq.poll();
if(c == null) break;
System.out.println("Customer Polled : "+c.getId() + " "+ c.getName());
}
}
}