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

Sdílet
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

Komentáře • 89

  • @nameshd658
    @nameshd658 Před 3 lety +3

    Wow man... I'm doing your programming series one by one every day... This is day #7.. Thanks a lot bro. Awesome tutorial !!!

  • @baibhavghimire3827
    @baibhavghimire3827 Před 4 lety +2

    You deserve more subscription..I am doing that..Thanks man for going through the basis and connecting dots.

  • @BooSgu
    @BooSgu Před rokem +1

    Simple explanation and easy to understand. Thank you very much.

  • @neutral911
    @neutral911 Před 4 lety +1

    Excellent Bro... I'm very satisfied with this explanation.. Thanks a lot and keep posting videos...

  • @BookWormsOriginal
    @BookWormsOriginal Před 4 lety +4

    The most simplistic and to the point explanation, you made my day! Thanks a lot! You deserve to be subscribed to!

  • @vandana89100
    @vandana89100 Před 3 lety +3

    Hats off to you for making life easier👍🏻

  • @sureshsadanala4639
    @sureshsadanala4639 Před 5 lety +2

    nicely explained 👍.. waiting for more videos 😊

  • @mohammedshabbir1347
    @mohammedshabbir1347 Před 3 lety +2

    Awesome explanation. Thanks Basant.

  • @mohan4459
    @mohan4459 Před 5 lety +2

    Nice explanation ... Thank you so much

  • @Technoverse_solutions
    @Technoverse_solutions Před 3 měsíci +1

    Your explanation was simply superb.......................#CRISTAL CLEAR #take_a_bow

  • @siyedyoussef3202
    @siyedyoussef3202 Před 5 lety +2

    Great java 8 playlist video 👍👍👍👍

  • @debaprasadsahoo3707
    @debaprasadsahoo3707 Před 5 lety +2

    Really this is a very good explanation for me.

  • @manimarankrishnamoorthy8172

    Nicely explained..Great.

  • @subhashsahu9930
    @subhashsahu9930 Před 2 měsíci +1

    Thanks for sharing this valuable knowledge

  • @ramanshrestha3851
    @ramanshrestha3851 Před 3 lety

    Nice video, keep uploading useful videos related to Java. Thanks

  • @TheMihan123
    @TheMihan123 Před 4 lety +2

    Thanks ! nicely explained .

  • @fjkldhakljf
    @fjkldhakljf Před rokem +3

    Great work sir , I admire your ability to give motivation for examples and follow through steps of why this way is better than alternatives

  • @amitkharbade
    @amitkharbade Před 2 lety +2

    Thanks for another useful tutorial

  • @kishordige4654
    @kishordige4654 Před 2 lety +1

    Thank you! You are a great Teacher

  • @mydreamworld29
    @mydreamworld29 Před 3 lety +2

    Well explained. Thanks

  • @niranjanp8194
    @niranjanp8194 Před 2 lety +1

    Perfect example bro. keep it up.

  • @saurabh7489
    @saurabh7489 Před 2 lety +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  Před 2 lety

      It's always recommend to use stream in single pipeline rather than use collections utility method

    • @saurabh7489
      @saurabh7489 Před 2 lety

      @@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.

  • @taufiquealam4125
    @taufiquealam4125 Před 5 lety +4

    You are simply Wow. Nice explanation. Request to come up with new things in Java 8 that would be highly appreciated.

    • @Javatechie
      @Javatechie  Před 5 lety +1

      After finish this stream API we will start date API series

    • @taufiquealam4125
      @taufiquealam4125 Před 5 lety +1

      @@Javatechie Thank you...

  • @tussharsurya5829
    @tussharsurya5829 Před 5 lety +2

    Well explained 😇

  • @umaparvathi606
    @umaparvathi606 Před 3 lety +1

    All the topics very nice sir

  • @berkcanulubas1318
    @berkcanulubas1318 Před měsícem

    thank you for your great video

  • @ANKITCHOUDHARYankitkrchy
    @ANKITCHOUDHARYankitkrchy Před 2 lety +1

    Commendable job

  • @omsonschicago5872
    @omsonschicago5872 Před 4 lety +1

    Thank you so much

  • @sinurao1017
    @sinurao1017 Před 3 lety +1

    Super JT

  • @sameer838
    @sameer838 Před 2 lety +1

    Great

  • @milimishra6447
    @milimishra6447 Před měsícem

    Good one

  • @RaulSC9
    @RaulSC9 Před 4 lety +1

    Thanks!!

  • @ILuvBilli
    @ILuvBilli Před 5 lety +1

    Superbbbb

  • @rahulkaranjavkar7994
    @rahulkaranjavkar7994 Před 5 lety +1

    Thanks...

  • @sarojgupta8181
    @sarojgupta8181 Před rokem +1

    wow its superb :)

  • @vipinvenugopal3166
    @vipinvenugopal3166 Před 2 lety +2

    Thanks

  • @Proman.Offbeat.Traveller

    NYC video 17:18

  • @sowmyadara
    @sowmyadara Před 4 lety +2

    Great explanation.. at 2:58 why can't we use like sorted(Comparator::reveseOrder)

    • @Javatechie
      @Javatechie  Před 4 lety +1

      We can't represent this as Method reference

    • @sowmyadara
      @sowmyadara Před 4 lety +1

      @Java Techie ... Thank you

  • @maruthidanda2364
    @maruthidanda2364 Před 2 lety +1

    Super super

  • @naveenc4963
    @naveenc4963 Před 5 lety +2

    Nice explanation bro
    Can you share the Java 8 features example codes

    • @Javatechie
      @Javatechie  Před 5 lety +1

      Link mentioned in video description

  • @saswatidas6217
    @saswatidas6217 Před 4 lety

    Could you please explain more about method reference ?

  • @ashishchoudhary9824
    @ashishchoudhary9824 Před 3 lety

    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.

  • @Saravanan-lj9so
    @Saravanan-lj9so Před 4 lety

    Man, Why you did not put video 15 years back ?I would have been very good developer.You are great.

  • @oguzhan2393
    @oguzhan2393 Před rokem +1

    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  Před rokem

      Thanks buddy glad that i am delivering the content which people can understand. Keep learning

  • @hardikjotangia2642
    @hardikjotangia2642 Před 4 lety +2

    Sir, when I am doing shorting I want new shorted list, How to achieve this?

    • @Javatechie
      @Javatechie  Před 4 lety +2

      Yes we can , you need to do something like
      list.stream().sort(Comparator.comparing("ADD COMPARISON LOGIC")).collect(Collectors.toList());

  • @pankajkm884
    @pankajkm884 Před 2 lety +2

    getName method is not static method. It is instance method. So, how you can call it by Class reference method?

    • @Javatechie
      @Javatechie  Před 2 lety

      Method reference doesn't mean to access only static method all getter method you can access while performing mapping

  • @pradiptagure7492
    @pradiptagure7492 Před 3 lety +3

    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?

  • @raghunathhembram9962
    @raghunathhembram9962 Před 9 měsíci +1

    sir can we sort the list of number without using sorted method by using stream api in java

    • @Javatechie
      @Javatechie  Před 9 měsíci

      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);
      }
      }

  • @saurabhkailashchandrapuran8609

    how to sort on custom two fields like with name and salary both at a time

    • @Javatechie
      @Javatechie  Před 3 lety +1

      Inside lambda we need to go for if condition

  • @johngather4055
    @johngather4055 Před 4 lety +1

    thnks why u are overriding equals and hashcode for custom employee

    • @Javatechie
      @Javatechie  Před 4 lety

      If you won't override equals and hashcode then we can't compare object
      This is what also contract between these two method

  • @vamsikondaparthik9934
    @vamsikondaparthik9934 Před 2 lety +1

    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  Před 2 lety

      Do you have setter and getter method define in your employee class

    • @vamsikondaparthik9934
      @vamsikondaparthik9934 Před 2 lety

      @@Javatechie i think its just STS not showing the methods … when i wrote manually there is no error

  • @Ravinanik
    @Ravinanik Před 8 měsíci

    How to sort reverse using comparing method

  • @kishyash1
    @kishyash1 Před 2 lety +1

    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.

  • @RealSlimShady-um6gf
    @RealSlimShady-um6gf Před 9 měsíci

    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

  • @vishalpradhan9881793197
    @vishalpradhan9881793197 Před 2 lety +1

    writing comment from UI

  • @vishalpradhan9881793197
    @vishalpradhan9881793197 Před 2 lety +1

    writing comment from terminal

  • @sujithg5873
    @sujithg5873 Před 5 lety +1

    Can u detailed explanation for method reference

  • @Ankit-vp7nt
    @Ankit-vp7nt Před 4 lety

    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  Před 4 lety +1

      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 Před 4 lety

      @@Javatechie Collections.sort(employee,(o1,o2)-> o1.getName().compareToIgnoreCase(o2.getName())); like that working but in stream is not working

    • @Javatechie
      @Javatechie  Před 4 lety

      @@Ankit-vp7nt let you know once I will check

  • @sumitjain3526
    @sumitjain3526 Před 2 lety +1

    Thank you so much