Comparable Interface in Java - Tutorial for Beginners | Learn Comparable in 5 minutes

preview_player
Показать описание
Comparable Interface Tutorial in Java - for beginners. Explained simply and elegantly in under 5 minutes.

Today I am going to show you how to use the Comparable interface in Java to sort Custom objects using the sort() method. When I was studying it in college it was a bit confusing, but don't worry, it is actually very simple and we will go over every detail. So lets get started!

Comparable Interface is a Java Interface used to Compare Objects on a single element: like year, name, price and so on. As every interface, it must be implemented, and when you implement it you must override its only method – compareTo.

Рекомендации по теме
Комментарии
Автор

New video - Comparable Interface vs Comparator Interface

javacodingcommunity
Автор

Your explanation was simple and clear. What I was trying to figure out in 1 hour, you did in 5 minutes. Mind Blowing!

vxxxnni
Автор

Busy practicing for a big practical test I have coming up and this video has been a lifesaver. Straight to the point, clear, and concise. Thank you so much for sharing!

JoshuaLawrence-wcyr
Автор

thanks mate, finally understand what this is.
i watched several tutorials and my teacher talked about it and finally you made it clear why we need and how we can use it.

vanamutt
Автор

Super easy to understand because of this video. Thank you!

altayali
Автор

Nice clear, concise and quick explanation!

draganjonceski
Автор

Awesome video, easy to follow and understand :)

ogdbalsuzf
Автор

Thanks for the help, it was really useful!

kinrev
Автор

Hello, thank you for this video it is a great help for me. I do have one question : how happens if they have same id and you want the "compare method" to compare on something else instead of setting them equal to each other? like if they have same Id number, you want to compare the name then

hans-henrikmadsen
Автор

Alternatively for line 51, you can call the method arr.sort(null) and you'll accomplish the same.

gringotico
Автор

Excellent video, tysm. But, what to do in case of maps / treemaps as sort function can only be used with lists and not with maps?

sagarmoyganguly
Автор

Why when you change the return value for each if statement, the sort also change from largest to smallest?

TranTran-wcmo
Автор

But how can I compare two Strings? I made an object name where the forenames and surnames are and want to sort them in main first by the surname and then by forename. Comparing two integers are rather simple but the strings... I dont understand that

vicvancen
Автор

good video but you forgot to add the equals() method

akna
Автор

I have a question, you didn't declare or used the overridden compareT() but it sorted it regardless, how did that happen?

noirRozelle
Автор

Great. Awesome. So the Collections.sort(car) method will go and look inside compareTo() method, and see that which values are being compared. here we have id compared and the value 0, 1, -1 is returned. and based upon these returned values the collections.sort(car) methods works.

thetruth
Автор

Hi, I need ur help, when I use Array.sort it doesn´t work, can you teach me how to resolve ?

otaldepeewee
Автор

hey i just wanna say that GOD loved the world so much he sent his only begotten
son Jesus to die a brutal death for us so that we can have eternal life
and we can all accept this amazing gift this by simply believing in him (Jesus) asking for the forgiveness of your sins
and forming a relationship with heavenly father.

zstar