Navigate Select ESC Close

#95 Comparator vs Comparable in Java

2023-01-19 Science & Technology
344.2k
5.8k
168
Telusko
Telusko
2.8m subscribers

Unlock all features

FREE: Get instant access to 10 AI summaries, chats, or transcripts per day.

Description

Check out our courses: AI Powered DevOps with AWS - Live Course :- https://go.telusko.com/AIDevOps-AWS Coupon: TELUSKO10 (10% Discount) Complete Java Developer Course Batch-4: https://go.telusko.com/Complete4 Coupon: TELUSKO10 (10% Discount) Master Java Spring Development : https://go.telusko.com/masterjava Coupon: TELUSKO20 (20% Discount) For More Queries WhatsApp or Call on : +919008963671 Udemy Courses: Spring: https://go.telusko.com/udemyteluskospring Java:- https://go.telusko.com/udemyteluskojava Java Spring:- https://go.telusko.com/Udemyjavaspring Java For Programmers:- https://go.telusko.com/javaProgrammers Python : https://go.telusko.com/udemyteluskopython Git : https://go.telusko.com/udemyteluskogit Docker : https://go.telusko.com/udemyteluskodocker website : https://courses.telusko.com/ In this lecture we will learn: - What is Comparator in Java? - How to give your implementation for sorting? - What is Comparable in Java? - Difference between Comparable & Comparator #1 - From the Java 1.7 version, it is not compulsory to mention the generic type on the right-hand side if you have already mentioned it on the left-hand side. - A collection class has lots of methods. The collection class belongs to the util package. - We can sort a list or an ArrayList by using the method sort of collection class Collections.sort(); - If we want to apply our own logic in sorting, then we have to use a comparator with sorting in collections. - Comparator is also an interface. - We have a method called compare() in the comparator interface. - We can use an interface by implementing a class or through an anonymous class. - Compare method works on an algorithm where it compares two values and then swaps them. Comparator Integer com= new Comparator Integer() { public int compare(Integer i, Integer j) { statements; } }; - So, a comparator is an interface through which you can specify your own concept of sorting. #2 - Integer class implements a Comparable interface. So by default, sort works for Integer. - If you want to do natural sorting on any other non-defined class, you can implement something called the Comparable. - Comparable is present in the lang package. - Comparable has a method known as compareTo(). - You have to define the method comapreTo() in a class, that is implementing Comparable. class Student implements Comparable Student { public int compareTo( Student that){ statements; } } Here, that is a variable. - We can also override the logic by using Comparator even if we have implements the Comparable interface. - Lambda expression can also be used with Comparator as it is a functional interface. #3 Difference between Comparable & Comparator: - Comparable provides a single sorting sequence while the Comparator provides multiple sorting sequences. - In Comparable, actual gets modified while in Comparator, the original class does not get affected. - Comparable gives the compareTo() method for sorting while Comparator gives the compare() method to sort elements. Github repo : https://github.com/navinreddy20/Javacode.git Instagram : https://www.instagram.com/navinreddyofficial/ Linkedin : https://in.linkedin.com/in/navinreddy20 Discord : https://discord.gg/UrYda98D Java:- https://bit.ly/JavaUdemyTelusko Spring:- https://bit.ly/SpringUdemyTelusko More Learning : Java :- https://bit.ly/3x6rr0N Python :- https://bit.ly/3GRc7JX Django :- https://bit.ly/3MmoJK6 JavaScript :- https://bit.ly/3tiAlHo Node JS :- https://bit.ly/3GT4liq Rest Api :-https://bit.ly/3MjhZwt Servlet :- https://bit.ly/3Q7eA7k Spring Framework :- https://bit.ly/3xi7buh Design Patterns in Java :- https://bit.ly/3MocXiq Docker :- https://bit.ly/3xjWzLA Blockchain Tutorial :- https://bit.ly/3NSbOkc Corda Tutorial:- https://bit.ly/3thbUKa Hyperledger Fabric :- https://bit.ly/38RZCRB NoSQL Tutorial :- https://bit.ly/3aJpRuc Mysql Tutorial :- https://bit.ly/3thpr4L Data Structures using Java :- https://bit.ly/3MuJa7S Git Tutorial :- https://bit.ly/3NXyCPu Donation: PayPal Id : navinreddy20 https://www.telusko.com

Top Comments (10)

@Noah-zc6mv 2023-02-22

So glad you're updating your older videos. Helpful as always! 😁

43
@restrostrain 2023-09-04

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<String> com = new Comparator<>(){ public int compare(String a , String b){ if(a.length() > b.length()) return 1; else return -1; } }; List<String> 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); } }

13
@varsheethtv 2023-06-18

The last 2 mins are very insightful!! 😍😍

9
@Democracy_Manifest 2023-02-28

Good video. Thanks for leaving in the errors so we can learn.

22
@vikrantsingh2804 2024-03-15

I must say this was one of the best videos explaining comparator and comparable concept.

3
@naul2k74 2023-07-10

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 <3

98 2 replies
@suhailsharieff155 2023-12-24

Navin sir.... Really fond of u..... I sat for 5 hrs to understand it.... Now im crystal clear

16 1 replies
@rhea1149 2023-12-09

best java instructor i’ve ever come across

9
@deepthimalepatiii247 2024-02-29

Reddy ji! No words Thanks for your explanation

0
@poojazare-0008 2025-03-24

You are truly amazing! I have never met anyone like you. Your teaching is outstanding—amazing, effective, and excellent. I have gained deep knowledge of OOPs in Java, and it's all because of your course. Thanks to this, I cracked my aptitude test and interview today! I just want to say a special thank you to you. Because of your deep knowledge, we gain deep knowledge too. Thank you!

0

Unlock the Data Inside
Turn Videos into Knowledge

  • Get FREE 10/day: transcripts, summaries, chats
  • Chat with videos, export text & PDF
  • $1 free API credit for RAG, chatbots & research

Free forever plan • All features unlocked

App screenshot