Sorting Using Stream API sorted() method in java : copy below code and study , to understand different sorting techniques using java Stream API :
I know , this program is very big , but its enough to practice whole sorting using Stream API in java ,due to time limitation , i am skipping theory part we will see theory in depth in upcoming articles , till that time use this program and try to understand sorting using stream in java :
Step 1: write Student class as below :
Happy Learning , Practice above program and feel free to ask any questions.
Great Day Ahead..
package com.crtr4u.www;
public class Student {
int id;
String name;
String department;
public Student() {
super();
}
public Student(int id, String name, String department) {
super();
this.id = id;
this.name = name;
this.department = department;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", department=" + department + "]";
}
}
Step 2: Write main class as below :
package com.crtr4u.www;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class SortingDemo {
public static void main(String[] args) {
List<Integer> numList=Arrays.asList(1,4,2,6,5,3);
System.out.println("original numerical list : "+numList);
System.out.println("Sorted numbers : ");
numList.stream().sorted().forEach(System.out::print);
System.out.println("\nReversed numbers :");
numList.stream().sorted(Comparator.reverseOrder()).forEach(System.out::print);
List<String> stringList=Arrays.asList("A","D","C","B","Z","W");
System.out.println("\noriginal String list : "+stringList);
System.out.println("Sorted String : ");
stringList.stream().sorted().forEach(System.out::print);
System.out.println("\nReversed String :");
stringList.stream().sorted(Comparator.reverseOrder()).forEach(System.out::print);
List<Student> studentList = Arrays.asList(new Student(1, "A", "MCA"), new Student(4, "B", "Accounting"),
new Student(3, "C", "Mechanical"), new Student(2, "D", "MCA"), new Student(6, "a", "Pharmacy"),
new Student(5, "d", "Chemical"), new Student(7, "E", "accounting"));
System.out.println("\nStudent list ");
for (Student s : studentList) {
System.out.println(s);
}
List<Student> sortedListByName1 = studentList.stream().sorted((s1, s2) -> s1.getName().compareTo(s2.getName()))
.collect(Collectors.toList());
System.out.println("Sorted by name : ");
for (Student s : sortedListByName1) {
System.out.println(s);
}
List<Student> sortedListById1 = studentList.stream().sorted((s1, s2) -> {
if (s1.getId() == s2.getId()) {
return 0;
} else if (s1.getId() > s2.getId()) {
return 1;
} else {
return -1;
}
}).collect(Collectors.toList());
System.out.println("Sorted by id : ");
for (Student s : sortedListById1) {
System.out.println(s);
}
List<Student> sortedListByName2 = studentList.stream().sorted(Comparator.comparing(Student::getName))
.collect(Collectors.toList());
System.out.println("Sorted by name :");
for (Student s : sortedListByName2) {
System.out.println(s);
}
List<Student> sortedListByName3 = studentList.stream().sorted(Comparator.comparing(Student::getName).reversed())
.collect(Collectors.toList());
System.out.println("Sorted by name reversed :");
for (Student s : sortedListByName3) {
System.out.println(s);
}
List<Student> sortedById2 = studentList.stream().sorted(Comparator.comparingInt(Student::getId))
.collect(Collectors.toList());
System.out.println("Sorted by id : ");
for (Student s : sortedById2) {
System.out.println(s);
}
List<Student> sortedById3 = studentList.stream().sorted(Comparator.comparingInt(Student::getId).reversed())
.collect(Collectors.toList());
System.out.println("Sorted by id reversed: ");
for (Student s : sortedById3) {
System.out.println(s);
}
}
}
Output :
original numerical list : [1, 4, 2, 6, 5, 3] Sorted numbers : 123456 Reversed numbers : 654321 original String list : [A, D, C, B, Z, W] Sorted String : ABCDWZ Reversed String : ZWDCBA Student list Student [id=1, name=A, department=MCA] Student [id=4, name=B, department=Accounting] Student [id=3, name=C, department=Mechanical] Student [id=2, name=D, department=MCA] Student [id=6, name=a, department=Pharmacy] Student [id=5, name=d, department=Chemical] Student [id=7, name=E, department=accounting] Sorted by name : Student [id=1, name=A, department=MCA] Student [id=4, name=B, department=Accounting] Student [id=3, name=C, department=Mechanical] Student [id=2, name=D, department=MCA] Student [id=7, name=E, department=accounting] Student [id=6, name=a, department=Pharmacy] Student [id=5, name=d, department=Chemical] Sorted by id : Student [id=1, name=A, department=MCA] Student [id=2, name=D, department=MCA] Student [id=3, name=C, department=Mechanical] Student [id=4, name=B, department=Accounting] Student [id=5, name=d, department=Chemical] Student [id=6, name=a, department=Pharmacy] Student [id=7, name=E, department=accounting] Sorted by name : Student [id=1, name=A, department=MCA] Student [id=4, name=B, department=Accounting] Student [id=3, name=C, department=Mechanical] Student [id=2, name=D, department=MCA] Student [id=7, name=E, department=accounting] Student [id=6, name=a, department=Pharmacy] Student [id=5, name=d, department=Chemical] Sorted by name reversed : Student [id=5, name=d, department=Chemical] Student [id=6, name=a, department=Pharmacy] Student [id=7, name=E, department=accounting] Student [id=2, name=D, department=MCA] Student [id=3, name=C, department=Mechanical] Student [id=4, name=B, department=Accounting] Student [id=1, name=A, department=MCA] Sorted by id : Student [id=1, name=A, department=MCA] Student [id=2, name=D, department=MCA] Student [id=3, name=C, department=Mechanical] Student [id=4, name=B, department=Accounting] Student [id=5, name=d, department=Chemical] Student [id=6, name=a, department=Pharmacy] Student [id=7, name=E, department=accounting] Sorted by id reversed: Student [id=7, name=E, department=accounting] Student [id=6, name=a, department=Pharmacy] Student [id=5, name=d, department=Chemical] Student [id=4, name=B, department=Accounting] Student [id=3, name=C, department=Mechanical] Student [id=2, name=D, department=MCA] Student [id=1, name=A, department=MCA]