Java 8 Stream - How to Sort a List using lambda | Example | Java Techie
Vložit
- čas přidán 26. 03. 2019
- 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/Java-Techie-jt/java8
Blogs:
javagyanmantra.wixsite.com/we...
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 - Věda a technologie
Wow man... I'm doing your programming series one by one every day... This is day #7.. Thanks a lot bro. Awesome tutorial !!!
You deserve more subscription..I am doing that..Thanks man for going through the basis and connecting dots.
Simple explanation and easy to understand. Thank you very much.
Excellent Bro... I'm very satisfied with this explanation.. Thanks a lot and keep posting videos...
The most simplistic and to the point explanation, you made my day! Thanks a lot! You deserve to be subscribed to!
Thank you so much subham
Hats off to you for making life easier👍🏻
nicely explained 👍.. waiting for more videos 😊
Awesome explanation. Thanks Basant.
Nice explanation ... Thank you so much
Your explanation was simply superb.......................#CRISTAL CLEAR #take_a_bow
Great java 8 playlist video 👍👍👍👍
Really this is a very good explanation for me.
Nicely explained..Great.
Thanks for sharing this valuable knowledge
Nice video, keep uploading useful videos related to Java. Thanks
Thanks ! nicely explained .
Great work sir , I admire your ability to give motivation for examples and follow through steps of why this way is better than alternatives
Keep learning 😃
Thanks for another useful tutorial
Thank you! You are a great Teacher
Well explained. Thanks
Perfect example bro. keep it up.
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());
It's always recommend to use stream in single pipeline rather than use collections utility method
@@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.
You are simply Wow. Nice explanation. Request to come up with new things in Java 8 that would be highly appreciated.
After finish this stream API we will start date API series
@@Javatechie Thank you...
Well explained 😇
All the topics very nice sir
thank you for your great video
Commendable job
Thank you so much
Super JT
Great
Good one
Thanks!!
Superbbbb
Thanks...
wow its superb :)
Thanks buddy 😊
Thanks
NYC video 17:18 ❤
Great explanation.. at 2:58 why can't we use like sorted(Comparator::reveseOrder)
We can't represent this as Method reference
@Java Techie ... Thank you
Super super
Nice explanation bro
Can you share the Java 8 features example codes
Link mentioned in video description
Could you please explain more about method reference ?
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.
Man, Why you did not put video 15 years back ?I would have been very good developer.You are great.
Thank you 😀
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.
Thanks buddy glad that i am delivering the content which people can understand. Keep learning
Sir, when I am doing shorting I want new shorted list, How to achieve this?
Yes we can , you need to do something like
list.stream().sort(Comparator.comparing("ADD COMPARISON LOGIC")).collect(Collectors.toList());
getName method is not static method. It is instance method. So, how you can call it by Class reference method?
Method reference doesn't mean to access only static method all getter method you can access while performing mapping
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?
You can call reverseOrder ()
@@Javatechie Thanks
sir can we sort the list of number without using sorted method by using stream api in java
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);
}
}
how to sort on custom two fields like with name and salary both at a time
Inside lambda we need to go for if condition
thnks why u are overriding equals and hashcode for custom employee
If you won't override equals and hashcode then we can't compare object
This is what also contract between these two method
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.
Do you have setter and getter method define in your employee class
@@Javatechie i think its just STS not showing the methods … when i wrote manually there is no error
How to sort reverse using comparing method
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.
Just use reversed () method
@@Javatechie Thanks alot and response also so fast
Is it works?
@@Javatechie yes
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
writing comment from UI
writing comment from terminal
Can u detailed explanation for method reference
Ok Will do
@@Javatechie thanks ☺️☺️
Bro search by name and name has casesensitive means some name start with upper case then how to sort in video 15:15 min
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);
@@Javatechie Collections.sort(employee,(o1,o2)-> o1.getName().compareToIgnoreCase(o2.getName())); like that working but in stream is not working
@@Ankit-vp7nt let you know once I will check
Thank you so much