Java 8 Stream - How to Sort a List using lambda | Example | Java Techie

  Рет қаралды 60,419

Java Techie

Java Techie

Күн бұрын

This video explain you How to sort a list in java 8 using lambda expression and stream API even we use Comparator for custom sorting order with stream api - real time example
GitHub:
github.com/Jav...
Blogs:
javagyanmantra...
Facebook:
/ 919464521471923
guys if you like this video please do subscribe now and press the bell icon to not miss any update from Java Techie

Пікірлер: 90
@nameshd658
@nameshd658 3 жыл бұрын
Wow man... I'm doing your programming series one by one every day... This is day #7.. Thanks a lot bro. Awesome tutorial !!!
@pankajkm884
@pankajkm884 3 жыл бұрын
getName method is not static method. It is instance method. So, how you can call it by Class reference method?
@Javatechie
@Javatechie 3 жыл бұрын
Method reference doesn't mean to access only static method all getter method you can access while performing mapping
@sinurao1017
@sinurao1017 3 жыл бұрын
Super JT
@vamsikondaparthik9934
@vamsikondaparthik9934 2 жыл бұрын
Thanks for the Tutorial. In Java 11 i am unable to call getter and setter methods in comparing method like below: listEmployee.stream().sorted(Comparator.comparing(emp->emp.getName) Could you please help me with this.
@Javatechie
@Javatechie 2 жыл бұрын
Do you have setter and getter method define in your employee class
@vamsikondaparthik9934
@vamsikondaparthik9934 2 жыл бұрын
@@Javatechie i think its just STS not showing the methods … when i wrote manually there is no error
@vishalpradhan9881793197
@vishalpradhan9881793197 3 жыл бұрын
writing comment from UI
@sowmyadara
@sowmyadara 4 жыл бұрын
Great explanation.. at 2:58 why can't we use like sorted(Comparator::reveseOrder)
@Javatechie
@Javatechie 4 жыл бұрын
We can't represent this as Method reference
@sowmyadara
@sowmyadara 4 жыл бұрын
@Java Techie ... Thank you
@hardikjotangia2642
@hardikjotangia2642 4 жыл бұрын
Sir, when I am doing shorting I want new shorted list, How to achieve this?
@Javatechie
@Javatechie 4 жыл бұрын
Yes we can , you need to do something like list.stream().sort(Comparator.comparing("ADD COMPARISON LOGIC")).collect(Collectors.toList());
@taufiquealam4125
@taufiquealam4125 5 жыл бұрын
You are simply Wow. Nice explanation. Request to come up with new things in Java 8 that would be highly appreciated.
@Javatechie
@Javatechie 5 жыл бұрын
After finish this stream API we will start date API series
@taufiquealam4125
@taufiquealam4125 5 жыл бұрын
@@Javatechie Thank you...
@vipinvenugopal3166
@vipinvenugopal3166 2 жыл бұрын
Thanks
@raghunathhembram9962
@raghunathhembram9962 11 ай бұрын
sir can we sort the list of number without using sorted method by using stream api in java
@Javatechie
@Javatechie 11 ай бұрын
import java.util.List; import java.util.ArrayList; import java.util.stream.Collectors; import java.util.Comparator; public class Main { public static void main(String[] args) { List numbers = List.of(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5); List sortedNumbers = new ArrayList(); while (!numbers.isEmpty()) { Integer min = numbers.stream() .min(Comparator.naturalOrder()) .orElse(null); if (min != null) { sortedNumbers.add(min); numbers.remove(min); } } System.out.println(sortedNumbers); } }
@naveenc4963
@naveenc4963 5 жыл бұрын
Nice explanation bro Can you share the Java 8 features example codes
@Javatechie
@Javatechie 5 жыл бұрын
Link mentioned in video description
@mydreamworld29
@mydreamworld29 3 жыл бұрын
Well explained. Thanks
@vishalpradhan9881793197
@vishalpradhan9881793197 3 жыл бұрын
writing comment from terminal
@amitkharbade
@amitkharbade 3 жыл бұрын
Thanks for another useful tutorial
@fjkldhakljf
@fjkldhakljf Жыл бұрын
Great work sir , I admire your ability to give motivation for examples and follow through steps of why this way is better than alternatives
@Javatechie
@Javatechie Жыл бұрын
Keep learning 😃
@BookWormsOriginal
@BookWormsOriginal 4 жыл бұрын
The most simplistic and to the point explanation, you made my day! Thanks a lot! You deserve to be subscribed to!
@Javatechie
@Javatechie 4 жыл бұрын
Thank you so much subham
@saurabh7489
@saurabh7489 2 жыл бұрын
Great video. Thank you very much. Here is what I do for a list of objects: employees.sort(Comparator.comparing(Employee::getSalary)); employees.sort(Comparator.comparing(Employee::getSalary).reversed());
@Javatechie
@Javatechie 2 жыл бұрын
It's always recommend to use stream in single pipeline rather than use collections utility method
@saurabh7489
@saurabh7489 2 жыл бұрын
@@Javatechie Thanks for the information 🙂 I just felt that if all I want to do is sort the stream and not worry about modifying the original list, and not perform any other operation like 'filtering' or 'mapping' then creating a stream will incur extra overhead.
@manimarankrishnamoorthy8172
@manimarankrishnamoorthy8172 4 жыл бұрын
Nicely explained..Great.
@oguzhan2393
@oguzhan2393 2 жыл бұрын
you are really doing great things. I was watching a guy who was really idiot. called amigo. something. But mate you are really doing good things thanks.
@Javatechie
@Javatechie 2 жыл бұрын
Thanks buddy glad that i am delivering the content which people can understand. Keep learning
@vandana89100
@vandana89100 3 жыл бұрын
Hats off to you for making life easier👍🏻
@baibhavghimire3827
@baibhavghimire3827 4 жыл бұрын
You deserve more subscription..I am doing that..Thanks man for going through the basis and connecting dots.
@sanketprajakta
@sanketprajakta Ай бұрын
nice explanation👌
@sureshsadanala4639
@sureshsadanala4639 5 жыл бұрын
nicely explained 👍.. waiting for more videos 😊
@mohammedshabbir1347
@mohammedshabbir1347 4 жыл бұрын
Awesome explanation. Thanks Basant.
@siyedyoussef3202
@siyedyoussef3202 5 жыл бұрын
Great java 8 playlist video 👍👍👍👍
@debaprasadsahoo3707
@debaprasadsahoo3707 5 жыл бұрын
Really this is a very good explanation for me.
@mohan4459
@mohan4459 5 жыл бұрын
Nice explanation ... Thank you so much
@sumitjain3526
@sumitjain3526 2 жыл бұрын
Thank you so much
@kishyash1
@kishyash1 2 жыл бұрын
Hii vasant, how will we do customized sorting in descending order using method reference ? (Comparator.comparing(Employee::getName)). This will give me sorting in ascending order but i want in descending order.
@Javatechie
@Javatechie 2 жыл бұрын
Just use reversed () method
@kishyash1
@kishyash1 2 жыл бұрын
@@Javatechie Thanks alot and response also so fast
@Javatechie
@Javatechie 2 жыл бұрын
Is it works?
@kishyash1
@kishyash1 2 жыл бұрын
@@Javatechie yes
@neutral911
@neutral911 4 жыл бұрын
Excellent Bro... I'm very satisfied with this explanation.. Thanks a lot and keep posting videos...
@subhashsahu9930
@subhashsahu9930 4 ай бұрын
Thanks for sharing this valuable knowledge
@Sp_Travel_Vlogs07
@Sp_Travel_Vlogs07 Жыл бұрын
NYC video 17:18 ❤
@Technoverse_software_solutions
@Technoverse_software_solutions 6 ай бұрын
Your explanation was simply superb.......................#CRISTAL CLEAR #take_a_bow
@RealSlimShady-um6gf
@RealSlimShady-um6gf 11 ай бұрын
While sorting why do you want to downcast Long to int? Either while declaring the field you should have made it as int or proceed with logic for Long which is a much better real time example
@maruthidanda2364
@maruthidanda2364 3 жыл бұрын
Super super
@TheMihan123
@TheMihan123 5 жыл бұрын
Thanks ! nicely explained .
@BooSgu
@BooSgu 2 жыл бұрын
Simple explanation and easy to understand. Thank you very much.
@Ravinanik
@Ravinanik 10 ай бұрын
How to sort reverse using comparing method
@johngather4055
@johngather4055 4 жыл бұрын
thnks why u are overriding equals and hashcode for custom employee
@Javatechie
@Javatechie 4 жыл бұрын
If you won't override equals and hashcode then we can't compare object This is what also contract between these two method
@ILuvBilli
@ILuvBilli 5 жыл бұрын
Superbbbb
@niranjanp8194
@niranjanp8194 2 жыл бұрын
Perfect example bro. keep it up.
@tussharsurya5829
@tussharsurya5829 5 жыл бұрын
Well explained 😇
@sarojgupta8181
@sarojgupta8181 2 жыл бұрын
wow its superb :)
@Javatechie
@Javatechie 2 жыл бұрын
Thanks buddy 😊
@umaparvathi606
@umaparvathi606 3 жыл бұрын
All the topics very nice sir
@kishordige4654
@kishordige4654 2 жыл бұрын
Thank you! You are a great Teacher
@berkcanulubas1318
@berkcanulubas1318 4 ай бұрын
thank you for your great video
@saurabhkailashchandrapuran8609
@saurabhkailashchandrapuran8609 3 жыл бұрын
how to sort on custom two fields like with name and salary both at a time
@Javatechie
@Javatechie 3 жыл бұрын
Inside lambda we need to go for if condition
@ANKITCHOUDHARYankitkrchy
@ANKITCHOUDHARYankitkrchy 2 жыл бұрын
Commendable job
@ashishchoudhary9824
@ashishchoudhary9824 3 жыл бұрын
Hii vasant, how will we do customized sorting in descending order using method reference ? (Comparator.comparing(Employee::getName)). This will give me sorting in ascending order but i want in descending order. Or i want sorting based on length of employee name, how will do using stream api ? Thanks.
@omsonschicago5872
@omsonschicago5872 4 жыл бұрын
Thank you so much
@ramanshrestha3851
@ramanshrestha3851 3 жыл бұрын
Nice video, keep uploading useful videos related to Java. Thanks
@sameer838
@sameer838 2 жыл бұрын
Great
@pradiptagure7492
@pradiptagure7492 4 жыл бұрын
Very good tutorial. It helped me. But one question, at the comparing example, it is sorting ascending by default. If I need to do, descending as well, how to achieve that?
@Javatechie
@Javatechie 4 жыл бұрын
You can call reverseOrder ()
@pradiptagure7492
@pradiptagure7492 4 жыл бұрын
@@Javatechie Thanks
@saswatidas6217
@saswatidas6217 5 жыл бұрын
Could you please explain more about method reference ?
@sujithg5873
@sujithg5873 5 жыл бұрын
Can u detailed explanation for method reference
@Javatechie
@Javatechie 5 жыл бұрын
Ok Will do
@sujithg5873
@sujithg5873 5 жыл бұрын
@@Javatechie thanks ☺️☺️
@rahulkaranjavkar7994
@rahulkaranjavkar7994 5 жыл бұрын
Thanks...
@RaulSC9
@RaulSC9 4 жыл бұрын
Thanks!!
@milimishra6447
@milimishra6447 3 ай бұрын
Good one
@Saravanan-lj9so
@Saravanan-lj9so 4 жыл бұрын
Man, Why you did not put video 15 years back ?I would have been very good developer.You are great.
@Javatechie
@Javatechie 4 жыл бұрын
Thank you 😀
@Ankit-vp7nt
@Ankit-vp7nt 4 жыл бұрын
Bro search by name and name has casesensitive means some name start with upper case then how to sort in video 15:15 min
@Javatechie
@Javatechie 4 жыл бұрын
Why doing stream you should forcefully either make them lowercase or uppercase something like employees.stream().sorted(Comparator.comparing(emp->emp.getName().toLowerCase())).forEach(System.out::println);
@Ankit-vp7nt
@Ankit-vp7nt 4 жыл бұрын
@@Javatechie Collections.sort(employee,(o1,o2)-> o1.getName().compareToIgnoreCase(o2.getName())); like that working but in stream is not working
@Javatechie
@Javatechie 4 жыл бұрын
@@Ankit-vp7nt let you know once I will check
Win This Dodgeball Game or DIE…
00:36
Alan Chikin Chow
Рет қаралды 37 МЛН
The Joker wanted to stand at the front, but unexpectedly was beaten up by Officer Rabbit
00:12
#95 Comparator vs Comparable in Java
15:43
Telusko
Рет қаралды 184 М.
Stream API in Java
26:04
Telusko
Рет қаралды 331 М.
Java 8 Stream API | forEach & filter Method example | Java Techie
21:56
Stream API in Java : Foundation |  Java 8
55:31
Selenium Express
Рет қаралды 15 М.
28. Streams in Java8 | Collections in Java - Part7
1:15:10
Concept && Coding - by Shrayansh
Рет қаралды 48 М.
Sort HashMap By Value Java8
6:17
CloudTech
Рет қаралды 6 М.
Java 8 Streams | Optional Usage and Best Practices | JavaTechie
22:22