#95 Comparator vs Comparable in Java

  Рет қаралды 202,026

Telusko

Telusko

Күн бұрын

Пікірлер: 128
@naul2k74
@naul2k74 Жыл бұрын
You literally teach and explain everything about sorting with object in 15 mins that my professor wasn't able to do in 2 hours. Good job and keep doing these things because it's really helpful and valuable to a fresher like me
@jnslzr
@jnslzr Ай бұрын
same, our teacher here literally sent us to read javadocs =)
@Noah-zc6mv
@Noah-zc6mv Жыл бұрын
So glad you're updating your older videos. Helpful as always! 😁
@suhailsharieff155
@suhailsharieff155 10 ай бұрын
Navin sir.... Really fond of u..... I sat for 5 hrs to understand it.... Now im crystal clear
@sciencequotesfacts5543
@sciencequotesfacts5543 20 күн бұрын
can you give some techniques
@rhea1149
@rhea1149 11 ай бұрын
best java instructor i’ve ever come across
@varsheethtv2459
@varsheethtv2459 Жыл бұрын
The last 2 mins are very insightful!! 😍😍
@Democracy_Manifest
@Democracy_Manifest Жыл бұрын
Good video. Thanks for leaving in the errors so we can learn.
@shraddhashetty8659
@shraddhashetty8659 Жыл бұрын
Sorting the String based on the length: public class PractComparator { public static void main(String[] arg) { Comparator com = new Comparator() { @Override public int compare(String o1, String o2) { return o1.length() - o2.length(); } }; List fruit = new ArrayList(); fruit.add("Banana"); fruit.add("Mandarin"); fruit.add("PineApple"); fruit.add("Apple"); Collections.sort(fruit,com); System.out.println(fruit); } }
@AmeyChittar
@AmeyChittar Жыл бұрын
Hi Navin. as usual great video.. but most importantly comparator and comparable have always been things ive never understood.... until now.. the way you have explained is amazing and always wait for your amazing videos. and by the way the growth of your presentation and teaching skills have become better and better and we love it. Thanks a lot and will be waiting for lots more videos.
@dypsking3414
@dypsking3414 Жыл бұрын
Thank you Mr. You made it very simple and easy!
@tarekabushaheen7593
@tarekabushaheen7593 6 ай бұрын
10/10 video and demonstration, absolutely love the dynamic, following along and coding beside you helps to learn efficiently
@karanraut4417
@karanraut4417 10 ай бұрын
@telusko I loved your teaching style Naveen Reddy Sir, It is not necessary for a Comparator to return -1 or 1 it can actually return any negative or positive integer only the sign matters. even 0 is allowed i guess. this caused me confusion but you should have also mentioned it at the end for more nerdy people. package java4AdvancedCollections; import java.util.TreeSet; public class TreeSetExample { public static void main(String[] args) { // Creating a TreeSet of integers (natural ordering) TreeSet treeSet = new TreeSet(); // Adding elements to the TreeSet treeSet.add(10); treeSet.add(5); treeSet.add(20); treeSet.add(15); // Printing the TreeSet (elements will be in ascending order) System.out.println("TreeSet: " + treeSet); // Creating a TreeSet with a custom comparator for descending order TreeSet descendingTreeSet = new TreeSet((a, b) -> { if (a > b) { return -2; } else { return 2; } }); // Adding elements to the descending TreeSet descendingTreeSet.add(10); descendingTreeSet.add(5); descendingTreeSet.add(20); descendingTreeSet.add(15); // Printing the descending TreeSet (elements will be in descending order) System.out.println("Descending TreeSet: " + descendingTreeSet); // Performing operations specific to NavigableSet interface System.out.println("Ceiling of 12: " + treeSet.ceiling(12)); System.out.println("Floor of 12: " + treeSet.floor(12)); } }
@inspiredomkar1239
@inspiredomkar1239 Жыл бұрын
Logic to sort elements based on the length of the string import java.util.Comparator; import java.util.*; class Solution { public static void main(String[] args) { Comparator com = new Comparator(){ public int compare(String a , String b){ if(a.length() > b.length()) return 1; else return -1; } }; List nums = new ArrayList(); nums.add("Python"); nums.add("C++"); nums.add("Java"); nums.add("Django"); nums.add("SpringBoot"); nums.add("Spring"); Collections.sort(nums , com); System.out.println(nums); } }
@moviecraze6079
@moviecraze6079 4 ай бұрын
Heck of a video! I have been trying to understand the differences for a long time, finally you cleared all of my doubts.
@ekanshmishra4517
@ekanshmishra4517 Ай бұрын
I have been struggling to understand this for long and this video is what it was required to get it cleared. Thank you @NavinSir #Telusko
@vikrantsingh2804
@vikrantsingh2804 8 ай бұрын
I must say this was one of the best videos explaining comparator and comparable concept.
@gurrammahenderreddy4586
@gurrammahenderreddy4586 Жыл бұрын
TASK : import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; class Hello { public static void main(String a[]) { Comparator com = new Comparator() { public int compare(String i, String j) { if(i.length() < j.length()) return 1; else return -1; } }; List names = new ArrayList(); names.add("Navin"); names.add("Navin Reddy"); names.add("Telusko"); names.add("Navin Telusko"); Collections.sort(names, com); System.out.println(names); } }
@statuslover1510
@statuslover1510 Жыл бұрын
Correct 👍
@uLise4Life
@uLise4Life Жыл бұрын
Had to watch an entire tutorial just because I thought I don't undertand java. After this video. i'm like OH MY GOD !!!!!!!!!
@abhinavmishra9690
@abhinavmishra9690 Жыл бұрын
Understood today the real concept behind comparator and comparable
@beinghuman63393
@beinghuman63393 Жыл бұрын
Excellent ❤❤ , feeling enjoyed the functional usage at the end
@pratikkumarsingh8114
@pratikkumarsingh8114 Жыл бұрын
Great, got my concepts cleared . Thank you , sir!!!👍👍👍👍👍👍👍👍👍👍👍👍👍👍
@Sumanth_Siddareddy
@Sumanth_Siddareddy Жыл бұрын
thanks for your explanation, sir. It helps me to understand comparable and comparator.
@adityarao7249
@adityarao7249 Жыл бұрын
Love your videos, learn new thing every time.
@franciscomambo4120
@franciscomambo4120 7 ай бұрын
Just discovered this video and the explanation is awesome, it almost blew my mind
@rahuljmd
@rahuljmd 5 ай бұрын
Homework: import java.util.*; class Solution { public static void main(String[] args) { // Write your code here Comparator com=new Comparator(){ //jai mata di : Anonymous Inner class public int compare(String s1, String s2){ if(s1.length()>s2.length()){ return 1; } else return -1; } }; List al=new ArrayList(); al.add("Rahul"); al.add("DK"); al.add("J"); Collections.sort(al,com); System.out.println(al); } }
@yusrax3
@yusrax3 2 ай бұрын
finally understood thanks!
@deepthimalepatiii247
@deepthimalepatiii247 8 ай бұрын
Reddy ji! No words Thanks for your explanation
@priyanshukhullar-tc7ss
@priyanshukhullar-tc7ss Жыл бұрын
Awesome guruji shukriya
@emmaruhcheva3658
@emmaruhcheva3658 5 ай бұрын
amazing video, gave me the summary i needed!
@mikedelta658
@mikedelta658 9 ай бұрын
Now, I understood those concepts. Thanks
@ManojPatil-z8m
@ManojPatil-z8m 3 ай бұрын
Hats off to you, sir! 🙌
@gyanendramaurya1796
@gyanendramaurya1796 8 ай бұрын
When i was a student in 2015 then also i used to come and watch java videos here. I now have almost 5 years of experience and when some Java work comes I come here again to refresh my memory. Thank you Navin. It feels like nostalgia sometimes watching your videos
@francksgenlecroyant
@francksgenlecroyant Жыл бұрын
Navin Reddy never disappoints
@abrorallaberganov6293
@abrorallaberganov6293 4 ай бұрын
Great Explanation. Thanks
@student_03
@student_03 Жыл бұрын
thats a great video covered all that i needed thank you
@vinethasuresh3488
@vinethasuresh3488 Жыл бұрын
wonderful video, it helps me to understand the concept very clear . Example you are providing is just awesome sir. keep doing these kind of videos sir. thanks a lot.
@lovemishra8137
@lovemishra8137 5 ай бұрын
Nice explaination
@harshsuchak8370
@harshsuchak8370 Жыл бұрын
Awesome Explanation!
@rahuljmd
@rahuljmd 5 ай бұрын
Synopsis: If you want to give class the power to compare its object on its own then use Comparable otherwise use Comparator to define logic and pass its object in the Collections.sort() method....
@tej3679
@tej3679 Жыл бұрын
Thanks for the awesome explanation sir🎉
@shankil2975
@shankil2975 Жыл бұрын
Excellent video. Well explained. Can you add a comment here on how to sort based on more than one field? Say name and age assuming student names are duplicated. thanks
@lxy2670
@lxy2670 Жыл бұрын
i love you man. I finally got it!
@xacademia9646
@xacademia9646 Жыл бұрын
you know the person teaching you about java is legit when he is bald hehe. subscribed :)
@tsum3489
@tsum3489 Жыл бұрын
My teacher in University who's teaching us java is also bald🤣 and he even looks so much like this youtuber!!
@karanraut4417
@karanraut4417 10 ай бұрын
@telusko it is not necessary for a Comparator to return -1 or 1 it can actually return any negative or positive integer only the sign matters. {even 0 is allowed i guess}
@hmmgmm2841
@hmmgmm2841 Жыл бұрын
something meaningful to me
@nischalofchrist
@nischalofchrist Жыл бұрын
This is great explanation.
@sai-dx1vb
@sai-dx1vb 10 ай бұрын
How you intantiated the comparator because comparator is an interface and we cannot instantiate interface?
@soumyadeepchatterjee2189
@soumyadeepchatterjee2189 10 ай бұрын
Very helpful, thank you.
@Manjot_singh2002
@Manjot_singh2002 9 ай бұрын
7:28 import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class first { public static void main(String[] args) throws IOException { Comparator comp = new Comparator() { public int compare(String s, String t) { if (s.length() < t.length()) return 1; else return -1; // throw new UnsupportedOperationException("Unimplemented method 'compare'"); } }; List col=new ArrayList(); col.add("aaaaa"); col.add("aa"); col.add("aaaa"); col.add("a"); Collections.sort(col, comp); System.out.println(col); } }
@sumantagorai5880
@sumantagorai5880 Жыл бұрын
1 for swap & -1 for no swap
@akshaysilgari2097
@akshaysilgari2097 11 ай бұрын
Great explanation
@aashuk
@aashuk Жыл бұрын
Nice explanation. which IDE is that ? - got it. Visual Studio
@jeffro8807
@jeffro8807 9 ай бұрын
your videos are awesome man
@Sriram-ii8oo
@Sriram-ii8oo 8 күн бұрын
Thank you so much for this video ❤
@leonsvideos
@leonsvideos Жыл бұрын
Great explanation, thank you so much!
@shriharis.s3296
@shriharis.s3296 2 ай бұрын
Here i have a doubt. Comparable is a functional interface. So we can override onlt compareTo() method and it is fine. But Comparator method is not a functional interface. How can we just override only menthod which is compare() and Java dosen't gives an error?
@SphereofTime
@SphereofTime Ай бұрын
comparable and so no?1:00
@swacharahman5084
@swacharahman5084 Жыл бұрын
This video is tooo goood. THANK you
@macedoedit5797
@macedoedit5797 Жыл бұрын
Great video, thanks !
@zeldaking2429
@zeldaking2429 Жыл бұрын
Thank you. very clear and helpful!
@kidoo1567
@kidoo1567 7 ай бұрын
Thanx jhonny
@devabdul
@devabdul Жыл бұрын
Thank you for valuable video ❤️
@stargaming3103
@stargaming3103 Жыл бұрын
Sir what is meaning of 1 and -1 . I didn't understand that
@ketansharma6955
@ketansharma6955 Жыл бұрын
great video sir
@santoshchhetrithapa3573
@santoshchhetrithapa3573 Жыл бұрын
thank u sir very helpful . all clear now . Okay I have
@abhijeetkulkarni8246
@abhijeetkulkarni8246 Жыл бұрын
Sir please try to make videos with white screen
@SrinivasKumar-v3c
@SrinivasKumar-v3c 5 ай бұрын
sir can u upload generics concept
@awkward_adi
@awkward_adi Жыл бұрын
Thank you so much for this.
@sharabugnanesh3098
@sharabugnanesh3098 Жыл бұрын
7:20 import java.util.List; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class compa { public static void main(String[] args) { Comparator y = new Comparator() { public int compare(String i, String j) { if (i.length() > j.length()) return 1; else return -1; } }; List names = new ArrayList(); names.add("Gnani"); names.add("Gnan"); names.add("Chapathi"); names.add("Gnanesh"); names.add("navin mava"); Collections.sort(names, y); System.out.println(names); } }
@codework2434
@codework2434 Жыл бұрын
Line no: 8 --> You have to specify Comparator on right side as well, its compulsory 3:03 . But I dono why it works without it.
@sharabugnanesh3098
@sharabugnanesh3098 Жыл бұрын
​​@@codework2434 it's new update from official java it's unnecessary to mention on creating object of Comparator But in case of anonymous class we should! do you understand?
@codework2434
@codework2434 Жыл бұрын
@@sharabugnanesh3098 So, in your code above you left to mention the type, right ?
@sharabugnanesh3098
@sharabugnanesh3098 Жыл бұрын
@@codework2434 yes I need to mention at left
@codework2434
@codework2434 Жыл бұрын
@@sharabugnanesh3098 Thanks about the "official java update"
@hygef-v4
@hygef-v4 4 ай бұрын
thanks
@mrchaitu7
@mrchaitu7 3 ай бұрын
Cannot make a static reference to the non-static field compare some times i am getting this error in quick fix i got add static static Comparator com = new Comparator() { then it solved
@SugamSangeetwdSavita
@SugamSangeetwdSavita Жыл бұрын
Do we always need to use Array List for comparator or can it be used for array also??
@PurushothamreddyRamigani-x6q
@PurushothamreddyRamigani-x6q Жыл бұрын
I think no We can use any collection Correct me if I am wrong
@srikanthatthota2151
@srikanthatthota2151 7 ай бұрын
what if i try to implement Comparator on Student class, Navin reddy please explain this scenario
@hadse1234
@hadse1234 Жыл бұрын
Very good videoes!
@gautamkumarpandey9494
@gautamkumarpandey9494 Жыл бұрын
// Sort the list of strings on the basis of length of string. List list = new ArrayList(); list.add("Navin Reddy sir"); list.add("Harendra Pandey jee"); list.add("Gautam"); list.add("Nitesh"); Comparator com = new Comparator() { @Override public int compare(String o1, String o2) { if(o1.length()>o2.length())return 1; else return -1; } }; System.out.println("List before sorting"); System.out.println(list); System.out.println("List after sorting"); Collections.sort(list,com); System.out.println(list);
@prateekgoel7154
@prateekgoel7154 3 ай бұрын
Awesome video !!!
@AmitSingh-xc9qe
@AmitSingh-xc9qe 3 ай бұрын
Thankyou sir
@bharath3207
@bharath3207 Жыл бұрын
you are the best
@abhishekk3561
@abhishekk3561 Жыл бұрын
I have a doubt .... how many times this swapping happens??
@ESECYogeshS
@ESECYogeshS Жыл бұрын
Sir the collection is not a class it's an Interface
@Forty3Stories
@Forty3Stories Жыл бұрын
Collections is a class but Collection is an interface 😊
@priyanshukhullar-tc7ss
@priyanshukhullar-tc7ss Жыл бұрын
Thankyou Reddy Ji lots of love.
@GowthamE-l9r
@GowthamE-l9r Жыл бұрын
Nuvu king mava🤗
@-GANGUPALLIMAHAMMED
@-GANGUPALLIMAHAMMED Жыл бұрын
Comparator is an interface so how object will created??
@SharavananSrinivasan
@SharavananSrinivasan Жыл бұрын
we are implementing the logic with lambda expression
@nazeebahmed1050
@nazeebahmed1050 Жыл бұрын
I have a quetion, why by returning 1 , the swapping is happenning?It doesn't make sense to me,plz explain
@audiomac
@audiomac 9 ай бұрын
Hello! Returning 1 means that a is greater than b. Returning -1 means b is greater than a. Returning 0 means a equals b. I hope this helps!
@mazerati935
@mazerati935 Жыл бұрын
thanks
@aminesaber467
@aminesaber467 Жыл бұрын
import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; public class ChallengLQREA { public static void main(String[] args) { Comparator cmp = new Comparator() { public int compare(String o1, String o2) { if(o1.length() < o2.length()) return -1; else return 1 ; } }; ArrayList SortStrings = new ArrayList(); SortStrings.add("Amine"); SortStrings.add("Saberrr"); SortStrings.add("Aichaa"); SortStrings.add("Bahtiiii"); Collections.sort(SortStrings,cmp ); System.out.println(SortStrings); }
@ABHIJEETBHUJADE
@ABHIJEETBHUJADE 3 ай бұрын
Can anyone help me waht is (a:) this label oprator why we use it
@abdulwadoodh5757
@abdulwadoodh5757 Жыл бұрын
How did you replace nums to studs?
@kushagrasahni4597
@kushagrasahni4597 Жыл бұрын
its just find and replace
@Vaishnav-zk2tf
@Vaishnav-zk2tf Жыл бұрын
can any body tell me why return 1 and -1 is used .
@actandrepeat
@actandrepeat Жыл бұрын
Yes please..
@unemployedcse3514
@unemployedcse3514 Ай бұрын
awesome ❤
@sayantanhalder1056
@sayantanhalder1056 4 ай бұрын
Sorting string by length package Collection; import java.util.*; public class SortStringByLen { public static void main(String[] args) { ArrayList ob = new ArrayList(); ArrayList ob1 = new ArrayList(); Comparator c1 = new Comparator() { public int compare(String i ,String j){ if(i.length()>j.length()){ return 1; } else{ return -1; } } }; Scanner sc = new Scanner(System.in); System.out.println("Enter the names: "); String str; int len; for(int i=0;i
@Shri577
@Shri577 3 ай бұрын
package basics; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class demo { public static void main(String[] args) { Comparator com = new Comparator() { public int compare(String i, String j) { if(i.length()
@sudeepbansal5234
@sudeepbansal5234 Жыл бұрын
You are awesome 👏👏👏
@dominicdumenil861
@dominicdumenil861 9 ай бұрын
import java.util.List; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class Student { public static void main(String[] args) { Comparator com = ( i, j) -> s1.length()>s2.length()? 1 : -1; List names = new ArrayList(); names.add("Nathan"); names.add("Dominic"); names.add("Nikita"); names.add("Sharon"); names.add("Jessica"); Collections.sort(names, com); System.out.println(names); } }
@ooogabooga5111
@ooogabooga5111 6 ай бұрын
You are explaining saying sort() works using bubblesort in the background, which need not be the case, don't give wrong info. In case of java it uses, duel pivot quick sort sort alogithm. Why you pass in comparator, you are only letting the sort know how to compare the values, not the whole sorting.
@kashishswami3268
@kashishswami3268 11 ай бұрын
14:03
@WolnośćUmysłu
@WolnośćUmysłu Жыл бұрын
what is the IDE he is using?
@zakirdeshmukh9916
@zakirdeshmukh9916 Жыл бұрын
Vs code
@sruthigudmella3322
@sruthigudmella3322 Жыл бұрын
but IntelliJ IDEA works better
@ROSHAN-HARI-BALAJE
@ROSHAN-HARI-BALAJE 4 ай бұрын
Ternary Operator ❌ Tatari Operator ✔
@yashjoshi5709
@yashjoshi5709 Жыл бұрын
aaj mai alien banne vaala tha sir but aaj bhi merko aadhi cheeze samajh nahi aayi
@abhishekkumargupta9548
@abhishekkumargupta9548 Жыл бұрын
opiest explaination
@hiibuddy
@hiibuddy Жыл бұрын
Ohh bhupendra jogi 😂😂😂
#96 Need of Stream API in Java
7:04
Telusko
Рет қаралды 67 М.
Interview Question | Comparable vs Comparator in Java
12:58
Telusko
Рет қаралды 647 М.
这是自救的好办法 #路飞#海贼王
00:43
路飞与唐舞桐
Рет қаралды 136 МЛН
The IMPOSSIBLE Puzzle..
00:55
Stokes Twins
Рет қаралды 141 МЛН
PIZZA or CHICKEN // Left or Right Challenge
00:18
Hungry FAM
Рет қаралды 16 МЛН
Stream API in Java
26:04
Telusko
Рет қаралды 353 М.
#94 Map in Java
10:03
Telusko
Рет қаралды 95 М.
JWT авторизация. Основы JWT - механизма.
6:45
Хочу вАйти
Рет қаралды 16 М.
#26 Stack And Heap in Java
12:37
Telusko
Рет қаралды 278 М.
Stop Recommending Clean Code
27:05
ThePrimeTime
Рет қаралды 531 М.
Generics In Java - Full Simple Tutorial
17:34
Coding with John
Рет қаралды 1,1 МЛН
Differences between Comparable and Comparator
5:49
Durga Software Solutions
Рет қаралды 321 М.
Functional Interface | Lambda Expression in Java
13:56
Telusko
Рет қаралды 158 М.
Learn Java in 15 Minutes (seriously)
19:50
ForrestKnight
Рет қаралды 118 М.
这是自救的好办法 #路飞#海贼王
00:43
路飞与唐舞桐
Рет қаралды 136 МЛН