Inconsistency in Java's compareTo method on Strings [duplicate] - java

I am having a problem with PriorityQueues, as I am lead to believe it orders on priority however I am not sure what the priority is (I mean what the value is and where it comes from). A priorityQueue can be made with a comparator in the constructor and I have tried this but it does not work.
Queue class:
public JavaPriorityFlightQueue() {
super();
flights = new PriorityQueue(5, new SortQueueViaPriority());
}
Comparator:
import java.util.Comparator;
public class SortQueueViaPriority implements Comparator {
public int compare(Object o1, Object o2){
Flight f1 = (Flight) o1;
Flight f2 = (Flight) o2;
if( f1 == null || f2 == null ){
if( f1 == f2 ) return 0;
else if( f2 == null) return +1;
else return -1;
}
Integer i1 = (Integer) f1.getPriority();
Integer i2 = (Integer) f2.getPriority();
return i2.compareTo(i1);
}
}
Priority is an int value which is part of the flight class. I test this.
JavaPriorityFlightQueue flightQueue = new JavaPriorityFlightQueue();
Flight flight1 = new Flight("0001",9);
Flight flight2 = new Flight("0002",7);
Flight flight3 = new Flight("0003",1);
Flight flight4 = new Flight("0004",2);
Flight flight5 = new Flight("0005",1);
However the PriorityQueue is not sorted, and when I check it the value 9 is never compared to anything and the result is nothing is sorted. the compare class SortQueueViaPriority is copy and pasted from another class where the class sorts perfectly.

I suggest you try the following example. If you use PriorityQueue as a queue, the entries are removed in order.
import java.util.Comparator;
import java.util.PriorityQueue;
public class Main {
public static void main(String... args) {
PriorityQueue<Flight> flights = new PriorityQueue<Flight>(5, new SortQueueViaPriority());
flights.add(new Flight("0001", 9));
flights.add(new Flight("0002", 7));
flights.add(new Flight("0003", 1));
flights.add(new Flight("0004", 2));
flights.add(new Flight("0005", 1));
while (!flights.isEmpty())
System.out.println(flights.remove());
}
}
class SortQueueViaPriority implements Comparator<Flight> {
#Override
public int compare(Flight f1, Flight f2) {
return Integer.compare(f2.getPriority(), f1.getPriority());
}
}
class Flight {
private final String name;
private final int priority;
Flight(String name, int priority) {
this.name = name;
this.priority = priority;
}
public int getPriority() {
return priority;
}
#Override
public String toString() {
return "Flight{" +
"name='" + name + '\'' +
", priority=" + priority +
'}';
}
}
prints
Flight{name='0001', priority=9}
Flight{name='0002', priority=7}
Flight{name='0004', priority=2}
Flight{name='0003', priority=1}
Flight{name='0005', priority=1}
Note: PriorityQueue sorts entries such that only the first element will be the smallest. If you iterate over the queue, you will see all the elements, but they may or may not be in order.

Issue is Iterator.As Documented in Java doc of PriorityQueue#iterator
Returns an iterator over the elements in this queue. The iterator does not return the elements in any particular order.
As toString uses iterator it will not get printed in order. Or if you use loop based on iterator then also it will be in order.
And in the Java doc of PriorityQueue
The queue retrieval operations poll, remove, peek, and element access the element at the head of the queue.
To get results in order you will have to use one of these methods.

Instead of Comparator just use Comparable interface.
Your Flight class should implement Comparable interface. Then you need to override the compareTo() method. In that method you can add your own logic for sorting based on the property you need.
Just like this way:
#Override
public int compareTo(Object obj) {
// TODO Auto-generated method stub
Flight f = (Flight)obj;
if(this.a <f.a){
return 1;
}else{
return -1;
}
}

Related

CompareTo incorrect return value

First and Foremost, I must mention after reading lots of questions and tutorials and watching some videos, still the problem is not resolved.
I am an intermediate programmer in Java, and I have written some codes for comparing elements in a priority queue, in which elements are kept like [Comparable element, int priority]. Obviously, the higher priority would be popped.
the problem is:
I modified the code to have the first element with highest priority, and it does NOT work! I have narrowed it down that when comparing the first 2 elements, the compareTo method returns 0, which it should NOT, consequently the code fails!
this is the class that has the push of O(1) and Pop of O(n):
public class PriorityQueueU<T>
{
public class PriorityPair implements Comparable
{
public Comparable element;
public Comparable priority;
public PriorityPair(Comparable element, int priority){
this.element = element;
this.priority = priority;
}
public Comparable<T> getElemet(){
return this.element;
}
public Comparable<T> getPriority(){
return this.priority;
}
public int compareTo(Comparable a)
{
PriorityPair p2 = (PriorityPair)a;
return ((Comparable)priority).compareTo(p2.priority);
}
public String toString(){
String s1 = this.element.toString();
String s2 = this.priority.toString();
String res = "[ " + s1 + ", " + s2 + " ]" ;
return res;
}
public int compareTo(Object o)
{
// TODO Auto-generated method stub
return 0;
}
}
private LinkedList data;
public PriorityQueueU()
{
data = new LinkedList();
}
public void pushUnsorted(Comparable<T> o, int priority)
{
PriorityPair paired = new PriorityPair(o, priority);
data.addLast(paired);
}
public Comparable popUnsorted()
{
int index = 0;
for (int i = 0; i < this.data.size() - 1; i++)
{
if (((PriorityPair) this.data.get(i)).compareTo(this.data.get(i + 1)) < 0)
{
index = i + 1;
}
}
PriorityPair pp = (PriorityQueueU<T>.PriorityPair) this.data.get(index);
this.data.deleteIt(index);
return pp.getElemet();
}
public String toString(){
return this.data.toString();
}
}
}
and this is the code that would test the behavior:
PriorityQueueU<T> unSortedPQ = new PriorityQueueU<>();
unSortedPQ.pushUnsorted( (Comparable<T>) "a", 1000);
unSortedPQ.pushUnsorted((Comparable<T>) "b", 200);
unSortedPQ.pushUnsorted((Comparable<T>) "j", 900);
unSortedPQ.pushUnsorted((Comparable<T>) "r", 9);
unSortedPQ.pushUnsorted((Comparable<T>) "z", 6);
System.out.println("the UNsorted priority Q: ");
System.out.println(unSortedPQ);
System.out.println("*#------------------END OF PUSH-----------------#*");
System.out.println();
System.out.println("the priority Q: " + unSortedPQ);
System.out.println("popped item is: " + unSortedPQ.popUnsorted());
// System.out.println("top of the priority queue is: " + uPriorityQueueU.top());
System.out.println();
System.out.println("the UNsorted priority Q: ");
System.out.println(unSortedPQ);
System.out.println("*#------------------END OF POP-----------------#*");
System.out.println();
MANY thanks in advance.
P.S. bear in mind that maybe I was wrong and the problem was somewhere else!
current behavior: it pops j, which is the second highest element in the list. I already know that when popping, the element must be deleted as well from the priority queue, which is stored in a linked list. I made sure deletion in linked list is correctly working, but don't hesitate to ask for the code if necessary.
In order for compareTo to work, you should implement the method provided by the Comparable interface.
public interface Comparable<T> {
public int compareTo(T o);
}
As we see from the code above, Copmarable supports generics. If you do not specify what type of objects you want to compare (leave it as it is), it will default to Object, which it does (that's from your code):
public int compareTo(Object o)
{
// TODO Auto-generated method stub
return 0;
}
So you either use generics or write logic for compareTo(Object o).

Grouping a list of objects into a list of lists by a boolean function using Java 8 streams

Say I have a list of objects. I want to group them into a list of lists where each inner list contains elements for which a boolean comparison function returns true:
public class VO {
public VO(int age, int val) {
this.age = age;
this.val = val;
}
public int age;
public int val;
}
public void testGrouping() {
// Equal to vo2
VO vo1 = new VO(1, 100);
// Equal to vo1 and vo3
VO vo2 = new VO(3, 105);
// Equal to vo2 but not vo1 (age difference > 2),
// so it belongs into a new bucket
VO vo3 = new VO(5, 110);
// Equal to vo3, so it belongs into the same bucket as vo3
VO vo4 = new VO(7, 116);
List<VO> values = Arrays.asList(vo1, vo2, vo3, vo4);
//Group values using isEqual(VO, VO) into buckets somehow
//Any two values in a bucket must pass the check
//Expected without any specific order:
//[[vo1, vo2, [vo3, vo4]]
}
private boolean isEqual(VO a, VO b) {
return Math.abs(a.age - b.age) <= 2 && Math.abs(a.val - b.val) <= 10;
}
This is just a simplified example of the data that I have, in reality the comparison method is more complicated than that. Important is that each object must match each other object in its bucket regarding the check. The objects cannot be grouped/mapped by a specific value.
I already have code which does this but takes three levels of for-loops and which took me about a day to write. I'm curious if this can be achieved easier with streams.
Try mapping your items first, then use Collectors.groupingBy():
values.stream().map(name -> name.split("\\s+")).collect(groupingBy(a -> a[1]));
This does not return lists but arrays - but the concept is the same.
You can modify a bit the isEqual method so that you can use it as a comparator for the TreeMap. Then you can collect the TreeMap<VO, List<VO>> as follows. This code works in Java 7:
public static int isEqual(VO a, VO b) {
if (Math.abs(a.age - b.age) > 2 || Math.abs(a.val - b.val) > 10)
return 1;
else if (Math.abs(a.age - b.age) <= 2 && Math.abs(a.val - b.val) <= 10)
return 0;
else
return -1;
}
public static void main(String[] args) {
VO vo1 = new VO(1, 100);
VO vo2 = new VO(3, 105);
VO vo3 = new VO(5, 110);
VO vo4 = new VO(7, 116);
List<VO> values = Arrays.asList(vo1, vo2, vo3, vo4);
Map<VO, List<VO>> buckets = new TreeMap<>(new Comparator<VO>() {
#Override
public int compare(VO o1, VO o2) {
return isEqual(o1, o2);
}
});
for (VO vo : values) {
List<VO> list = buckets.get(vo);
if (list == null) {
list = new ArrayList<>();
}
list.add(vo);
buckets.put(vo, list);
}
// output
System.out.println(buckets);
//{VO{1=100}=[VO{1=100}, VO{3=105}], VO{5=110}=[VO{5=110}, VO{7=116}]}
}
public static class VO {
public int age, val;
public VO(int age, int val) {
this.age = age;
this.val = val;
}
#Override
public String toString() {
return "VO{" + age + "=" + val + "}";
}
}

Arranging by Alphabetical Order

I'm new to Java and I'm trying to arrange an arrayList of terms in alphabetical order. (A term is defined as a char and an int) (e.g. {Term('Z',4),Term('C',3),Term('Q',2) ...} )
My code is as follows:
public Term nextElement()
{
Term max = terms.get(0);
char maxtest = max.getElement();
for (int i = 1; i < terms.size(); i++){
Term tester = terms.get(i);
char maxtest2 = tester.getElement();
if (maxtest2 > maxtest) {
tester = max;
}
}
return max;
}
Why isn't this working? and how do I accomplish this?
My arrayList is called term filled with type Term
Your problem with this line of Code. Your class is not a Type of Comparable So, On which property or criteria compareTo() method will compare these two objects ???
res = maxtest.compareTo(maxtest2); //Your maxtest object is not Comparable Type.
You must need to make your class Term Comparable type. and , Override the method compareTo() as per your need.
You have not mentioning the variable's or structure of your class Term . So, I am assuming that your class have such kind of Structure .
public class Term implements Comparable<Term> {
private Character alpha;
private int number;
//getter and setters +Constructors as you specified
....
....
...
.....
// Now Set a criteria to sort is the Alphanumeric.
#Override
public int compareTo(Term prm_obj) {
if (prm_obj.getAlpha() > this.alpha) {
return 1;
} else if (prm_obj.getAlpha() < this.alpha) {
return -1;
} else {
return 0;
}
}
Now Your Class become a comparable Type. So you may apply Collections.sort(Collection obj) which automatically sort your ArrayList<Term>.
Here I write a demo for this.
public static void main(String... args){
List<Term> obj_listTerm = new ArrayList<>();
//add all the data you given in question
obj_listTerm .add(new Term('Z', 4));
obj_listTerm .add(new Term('Q', 2));
obj_listTerm .add(new Term('c', 3));
// print without Sorting your Term ArrayList.
System.out.println("This is the list unsorted: " + myTermList);
// Sort Using Collections.sort() Method.
Collections.sort(myTermList);
// After applying sort() you may see your Sorted ArrayList.
System.out.println("This is the list SORTED: " + myTermList);
}
You can use the Collection class and sort the list of term you have, you need only to make the class Term comparable
Example:
public class Term implements Comparable<Term> {
.....
// .....
// criteria to sort is the char
#Override
public int compareTo(Term o) {
if (o.getLetter()> this.letter) {
return 1;
} else if (o.getLetter() < this.letter) {
return -1;
} else {
return 0;
}
}
public static void main(String[] args) {
// test
List<Term> myTermList = new ArrayList<>();
myTermList.add(new Term('Z', 4));
myTermList.add(new Term('Q', 2));
myTermList.add(new Term('c', 3));
// check how the look like
System.out.println("This is the list unsorted: " + myTermList);
// now sort them
Collections.sort(myTermList);
// check how the look like
System.out.println("This is the list SORTED: " + myTermList);
}
Edit>
if you dont want to implement comparable then modify this:
res = maxtest.compareTo(maxtest2);
because this is not valid since maxtest and maxtest2 are primitives and not objects...
use instead
res = Character.compare(maxtest, maxtest2);
and then use the result to verify your logic and make decisions:
if (res >1) {
System.out.println("bigger");
}else if (res<1) {
System.out.println("smaller");
}else {
System.out.println("same");
}

Removing Duplicate Entries in Array - Java

For Java practice, I am trying to create a method inside my EmployeesDirectory Class that:
Removes Duplicate entries from the array
The array should be the same length after removing duplicates
Non-Empty entries should be making a contiguous sequence at the beginning of the array - and the actualNum should keep a record of the entries
Duplicate Means: Same Name, Position and Salary
Here is my Current Code:
I am unsure on how to implement this - any help would be appreciated
class EmployeeDirectory {
private Employee dir[];
private int size;
private int actualNum;
public EmployeeDirectory(int n) {
this.size = n;
dir = new Employee[size];
}
public boolean add(String name, String position, double salary) {
if (dir[size-1] != null) {
dir[actualNum] = new Employee(name, position, salary);
actualNum++;
return true;
} else {
return false;
}
}
}
I'd rather you did not write a distinct method for removing duplicates. If I were you, I would search for duplicates in add method and then instantly decide whether I need to add Employee.
Also, why don't you use Sets (link for HashSet) instead of arrays for your purpose? Sets by their own definition disallow adding duplicates, so they seem to be appropriate as a solution
First of all, Override equals and hashCode methods in Employee class as follow
#Override
public boolean equals(Object other) {
if(this == other) return true;
if(other == null || (this.getClass() != other.getClass())){
return false;
}
Employee guest = (Employee) other;
return Objects.equals(guest.name, name)
&& Objects.equals(guest.position, position)
&& Objects.equals(guest.salary, salary);
}
#Override
public int hashCode() {
return Arrays.hashCode(new Object[] {
name,
position,
salary
});
}
Then you can use Stream API distinct method to remove duplicates
Returns a stream consisting of the distinct elements (according to
Object.equals(Object)) of this stream.
You can do it like so
Employee e1 = new Employee("John", "developer", 2000);
Employee e2 = new Employee("John", "developer", 2000);
Employee e3 = new Employee("Fres", "designer", 1500);
Employee[] allEmployees = new Employee[100];
allEmployees[0] = e1;
allEmployees[1] = e2;
allEmployees[2] = e3;
allEmployees = Arrays.asList(allEmployees).stream().distinct()
.toArray(Employee[]::new);
Arrays.asList(allEmployees).forEach(System.out::println);
Output: (keeping both empty and non-empty entries)
John developer 2000.0
Fres designer 1500.0
null
Unfortunately, I have not got the Employee class to verify my code, but try this:
void removeDuplicates() {
int length = dir.length;
HashSet set = new HashSet(Arrays.asList(dir));
dir = new Employee[length];
Employee[] temp = (Employee[]) set.toArray();
for (int index = 0; index < temp.length; index++)
dir[index] = temp[index];
}
The code must remain the size of array after deletion the duplicates. At the beginning of array there must be valid Employees, at the end - nulls.
And don't forget to add this at the beginning of your .java file
import java.util.Arrays;
import java.util.HashSet;
If your task states as "remove duplicates from array" (i. e. you cannot use ArrayList or control when adding items), you can use the following approach:
public void removeDuplicates() {
Set<Employee> d = new HashSet<>(); // here to store distinct items
int shift = 0;
for (int i = 0; i > dir.length; i++) {
if (d.contains(dir[i])) { // duplicate, shift += 1
shift++;
} else { // distinct
d.add(dir[i]); // copy to `d` set
dir[i - shift] = dir[i]; // move item left
}
}
for (int i = d.size(); i < dir.length; i++)
dir[i] = null; // fill rest of array with nulls
actualNum = d.size();
}
Here, shift variable stores number of duplicates found in the array so far. Every distinct item is moved to shift positions left in order to make sequence continuous while keeping initial ordering. Then remaining items are altered to nulls.
To make hash-based collections work with Employee instances correctly, you also need to override hashCode() and equals() methods as follows:
public class Employee {
//...
#Override
public int hashCode() {
return Objects.hash(name, position, salary);
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
if (!o.getType().equals(this.getType()) return false;
Employee e = (Employee) o;
return Objects.equals(e.name, name)
&& Objects.equals(e.position, position)
&& Objects.equals(e.salary, salary); // or e.salary == salary, if it primitive type
}
}

Move an element up in the list using Comparator

I have an ArrayList in Java :
{"PatMic", "PatientDoc", "Phram", "Patnet", "PatientA"}
All the elements have a number assigned : PatMic = 20, PatientDoc = 30, Phram = 40, Patnet = 50, PatientA = 60.
And my current Comparator :
Comparator<String> comparator = new Comparator<String>() {
#Override
public int compare(final String o1, final String o2) {
final int numbr1 = getElementNumber(); //Returns element's number in a list
final int numbr2 = getElementNumber();
if (numbr1 > numbr2 ) {
return 1;
} else if (numbr1 < numbr2 ) {
return -1;
}
return 0;
}
};
Collections.sort(strings, comparator);
I do not want to change the assigned numbers to each element but would want to move the element PatientA in between PatMic and PatientDoc so the modified list should look like :
{"PatMic", "PatientA" "PatientDoc", "Phram", "Patnet"}
Could someone please suggest how to achieve this? I tried many ways to modify the existing Comparator logic but in vain. Thank you.
You are trying to sort based on some inherent value associated with a String. Therefore, sorting on a String itself is probably not correct. What you probably want to use is either a custom object (implement equals, hashCode and the interface Comparable), or an enum type. This will allow you to change the internal state of these objects explicitly, which will manifest itself naturally when using a Comparator. For example, using a class:
class MyClass implements Comparable
{
private String name;
private int value;
//Constructor
public MyClass(String s, int v)
{
name = s;
value = v;
}
//Getters and setters
//Implement comparing method
}
Then you can use these objects in place of your Strings:
//...
MyClass patMic = new MyClass("PatMic", 20);
// So on..
First, you should give you comparator sufficient knowledge about what it should do. I mean you should have some data available to comparator that says something like "okay, sort them all by associated number except this one - place it right here". "Right here" could be anything that points exact position, I gonna choose "before that element".
So here we go
public void sortWithException(List<String> data, final Map<String, Integer> numbers, final String element, final String next) {
Collections.sort(data, new Comparator<String>() {
#Override
public int compare(String first, String second) {
if (first.equals(element) || second.equals(element)) { //the exception
Integer nextNumber = numbers.get(next);
Integer firstNumber = numbers.get(first);
Integer secondNumber = numbers.get(second);
if (first.equals(element)) {
if (next == null) // placing the exception after ANY element
return 1;
return secondNumber >= nextNumber ? -1 : 1; //placing the element before next and after all next's predecessors
} else { // second.equals(element)
if (next == null)
return -1;
return firstNumber >= nextNumber ? 1 : -1;
}
} else { //normal sort
return numbers.get(first) - numbers.get(second);
}
}
});
}
and call it like sortWithException(data, numbers, "PatientA", "PatientDoc")
Note that i used Map for associated numbers, you should probably use your own method to get those numbers.

Categories