JDK 8 Stream Sorted () example __jdk8 sort

Source: Internet
Author: User
Tags comparable java 8 stream

Original link: http://www.concretepage.com/java/jdk-8/java-8-stream-sorted-example
Foreign to Java8 a series of summary of Good, translation come to share
This article will explain the Java 8 stream sorted () example, where we can sort a stream either in natural order or with a collation defined by the comparator interface. Comparator can be initialized with a Lambada expression, we can also reverse an already sorted stream.
Next we'll use Java 8 's streaming sorted to sort list, Map, Set
1, sorted () By default, the Order of natural order, which elements must implement comparable interface
2, Sorted (comparator< super t> Comparator): We can use Lambada to create a Comparator instance. You can sort the elements in ascending or descending order.
The following code sorts a list in natural order

Natural order reverse elements, using the Reverseorder () method provided by comparator

Use comparator to sort a list

To reverse the elements above

Stream sorted () with List

We sort a list collection of objects that assemble the student class. First we use the natural order, and then we use comparator to do ascending and descending, respectively:
Sortlist.java

Package com.concretepage;
Import java.util.ArrayList;
Import Java.util.Comparator;
Import java.util.List;
Import java.util.stream.Collectors; public class Sortlist {public static void main (string[] args) {list<student> List = new Arraylist<s
        Tudent> ();
        List.add (New Student (1, "Mahesh", 12));
        List.add (New Student (2, "Suresh", 15));

        List.add (New Student (3, "Nilesh", 10));
        SYSTEM.OUT.PRINTLN ("---Natural sorting by Name---");
        List<student> slist = List.stream (). Sorted (). Collect (Collectors.tolist ());

        Slist.foreach (E-> System.out.println ("Id:" + e.getid () + ", Name:" +e.getname () + ", Age:" +e.getage ()));
        SYSTEM.OUT.PRINTLN ("---Natural sorting by Name in reverse order---");
        Slist = List.stream (). Sorted (Comparator.reverseorder ()). Collect (Collectors.tolist ());        

        Slist.foreach (E-> System.out.println ("Id:" + e.getid () + ", Name:" +e.getname () + ", Age:" +e.getage ())); System.out.prINTLN ("---sorting using Comparator by age---");
        Slist = List.stream (). Sorted (comparator.comparing (student::getage)). Collect (Collectors.tolist ());

        Slist.foreach (E-> System.out.println ("Id:" + e.getid () + ", Name:" +e.getname () + ", Age:" +e.getage ()));
        SYSTEM.OUT.PRINTLN ("---sorting using Comparator by age and reverse order---");
        Slist = List.stream (). Sorted (Comparator.comparing (student::getage). Reversed ()). Collect (Collectors.tolist ());
    Slist.foreach (E-> System.out.println ("Id:" + e.getid () + ", Name:" +e.getname () + ", Age:" +e.getage ()));  }
}

* Student.java *

Package com.concretepage;
    public class Student implements comparable<student> {private int id;
    private String name;
    private int age;
        public Student (int ID, String name, int age) {this.id = ID;
        THIS.name = name;
    This.age = age;
    public int getId () {return id;
    Public String GetName () {return name;
    public int getage () {return age;
    @Override public int compareTo (Student ob) {return Name.compareto (Ob.getname ()); @Override public boolean equals (final Object obj) {if (obj = = null) {return FAL
          Se
          Final Student std = (Student) obj;
          if (this = = std) {return true;
          else {return (This.name.equals (std.name) && (this.age = = std.age));
          @Override public int hashcode () {int hashno = 7; Hashno = 13 * Hashno + (name = = null 0:name.hashcode ());
        return Hashno;  }   
}

* Output *

---Natural sorting by Name---
id:1, Name:mahesh, Age:12 id:3,
Name:nilesh, Age:10 id:2
, Name:suresh, age:15
---Natural sorting by Name in reverse order---
Id:2, Name:suresh, age:15
id:3, Name:nilesh, Age:10
id:1, Name:mahesh, age:12
---sorting using Comparator b Y Age---
id:3, Name:nilesh, Age:10
id:1, Name:mahesh, Age:12
id:2, Name:suresh, age:15
---sorting us ing Comparator by age with reverse order---
id:2, Name:suresh, age:15
id:1, Name:mahesh, Age:12

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.