Natural Ordering and the Comparable Interface: Java Collections Framework Tutorial Part 8

preview_player
Показать описание
-------------------------------------------------------------------------------------------------------------------------------------------
This tutorial shows you how to give your objects a 'natural order' by implementing the Comparable interface. You can then sort your objects without using a comparator and add them to TreeSets and TreeMaps.
--------------------------------------------------------------------------------------------------------------------------------------------
Рекомендации по теме
Комментарии
Автор

The audio intro is dope, you need to bring it back in your recent videos

akramUpdateteam
Автор

Comparable is something objects implement themselves so that they can be sorted easily. Comparator is something a separate object implements, which can compare objects that don't implement Comparable. It's a question of where the information needed to compare the objects should be placed; in the objects themselves, or externally in a separate object. If you try typing examples of both, you'll certainly see how different they are.

caveofprogramming
Автор

Thanks for all these tutorials these are really helpful, you sounds like Bob Ross :)

AG-ohxw
Автор

The last part just saved me hours of trying to figure out why a Comparable would not work as expected on a set.

xteric
Автор

I don't know why he added Hashcode and equals method because List interface allows duplicate and SortedSet interface using TreeSet will get rid of duplicate object by itself. Unless he is using HashSet then it is ok. Thanks alot your are Great teacher Cheers.

AS-jypf
Автор

When you implement Comparable interface, you will override the compareTo() method that defines the way two objects should be compared. And there can be only one compareTo() method in your implementation, whereas if you use external comparators, you can define as many of them to sort objects based on varied and differing criteria. For ex: you can see John using the comparators for sorting the objects in regular/reverse/exotic orders.

smuralimohan
Автор

Taking Computer Science 2 currently. I wish you were my instructor! This material is SO HELPFUL!!!

MarkWilliams-eswc
Автор

When we make Object in Tree Set.. it will itself implement Comparable! 
like we dont have to do Collections.sort(treeSet); 
its working for me just wanted to know more.. 

xthmath
Автор

Wow, the Beethoven! :-) Goes well with your voice. Nice beginning!

biteset
Автор

I can't really grasp the difference between Comparator and Comparable interfaces. So far it seems that with Comparator we can compare two objects that doesn't have to be of similar type. And Comparable compares similar type objects? I am really confused about this.

skazis
Автор

John is our God bestowing knowledge upon us.

smuralimohan
Автор

Instead of the if - else cond. for comparing Persons on id, could we have used IF instead we used Integer rather that primitive int for the id? If i recall correctly, compareTo () can accept Strings as well as Integer as args

jritzeku
Автор

This is wonderful stuff. My initial feelings that "bah -- so much work just to sort" are replaced with "Wow! How flexible and customizable sorting can be without touching the algorithm." Just like C++, with fewer dangling pointers, almost.

jvsnyc
Автор

cmon cut the guy some slack, he has a great voice, its calm and collected, I don't want to have someone jumping off the walls. Just drink your coffee

Antekdzi
Автор

The code worked just fine without the hashCode() and equals() methods. Why do I have to add them when adding elements(objects) to a Set?

abolaynain
Автор

17:05 .  isn't it safer overall to override the compareTo method to something like this
public int compareTo(Person person){
       return name.compareTo(person.name)
}
thank you and keepup the good work, u rock!!!!

nataliekalajo
Автор

Why the sound is like GOD is speaking from the

JaikratSinghTariyal
Автор

Thank you John bogdashkin !!!!///@\\\!!!!

rodomod
Автор

I have a question ... I understand everything in this tutorial I mean I know how to build exactly the same program .. but I am missing something ... I know that the compareTo() method return either 1, -1, or 0;
but what method exactly take advantage from the returned int ?? .. and what is the compareTo() method has to do with the hashCode() and equals() methods the we generated ? ... I just don't get it ... Please Help ??! 


Big Like For Your Outstanding Tutorials .... 

WaseemSwaileh
Автор

I don't quite get how the comparable method works, as we only have 1 parameter?

medotata
welcome to shbcf.ru