Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does tie-breaking mean?

I'm reading about PriorityQueues in javadocs and it mentions the term tie-breaking. I couldn't undersand what does the term mean. I hope someone could explain.

like image 964
Jack Twain Avatar asked Sep 03 '26 21:09

Jack Twain


1 Answers

In Java, comparison is done using a compare(a,b) method (for comparators) or a.compareTo(b) method (for class instances that can be compared). This method is supposed to return a negative number whenever a < b, a positive number when a > b, and 0 when a = b.

However sometimes people just use return value 0 to mean a and b are incomparable (some orderings aren't total). In this case, the PriorityQueue has to decide which element goes first. This is tie-breaking. Specifically some priority queues preserve the order in which zero-comparing elements were inserted, so in that case insertion time is the tie-breaker. Then for a collection of elements where compareTo() always returns 0, the priority queue would act just like a normal queue.

like image 199
dspyz Avatar answered Sep 05 '26 10:09

dspyz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!