Hey guys in this post let’s discuss aboutsorted()
introduced in Java 8 java.util.stream.Steam
interface. On the streams()
we can perform some aggregate operations such as filter()
, sorted()
and map()
. In this post let’s discuss about sorted()
.
Let’s look at the first example on how to sort the array of strings in ascending and descending order.
Example1.java
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) throws IOException {
List<String> fruits = Arrays.asList("Mango", "Apple", "Orange", "Grapes", "Pineapple", "Banana");
List<String> sortedList = new ArrayList<String>();
//sort in ascending order
sortedList = fruits.stream().sorted(Comparator.naturalOrder()).collect(Collectors.toList());
sortedList.forEach(System.out::println);
System.out.println("********************************");
//sort in descending order
sortedList = fruits.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
sortedList.forEach(System.out::println);
}
}
We can use Comparator
provided methods to sort the items, naturalOrder()
will sort in ascending order and reverseOrder()
, will sort in desceding order.
Person.java
class Person {
private int id;
private String name;
private int age;
private double salary;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
public Person(int id, String name, int age, double salary) {
super();
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
}
Example2.java
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<Person> list = Arrays.asList(
new Person(1, "Ramesh", 45, 35000),
new Person(2, "Bushan", 29, 22000),
new Person(3, "Shankar", 33, 29000),
new Person(4, "Chaitra", 32, 50000)
);
list.stream().sorted(Comparator.comparingInt(Person::getAge)).forEach(System.out::println);
System.out.println("****************************");
list.stream().sorted(Comparator.comparingInt(Person::getAge).reversed()).forEach(System.out::println);
}
}
To sort the integer values, Comparator
provides comparingInt()
, using which we can sort the items. reversed()
will sort the items in descending order.
Example3.java
public class Test {
public static void main(String[] args) {
List<Person> list = Arrays.asList(
new Person(1, "Ramesh", 45, 35000),
new Person(2, "Bushan", 29, 22000),
new Person(3, "Shankar", 33, 29000),
new Person(4, "Chaitra", 32, 50000)
);
list.stream().sorted(Comparator.comparing(Person::getName)).forEach(System.out::println);
System.out.println("****************************");
list.stream().sorted(Comparator.comparing(Person::getName).reversed()).forEach(System.out::println);
}
}
we will use comparing()
to sort the String
type which is provided by Comparator
.
Example4.java
Let’s look at the soring using lambdas
public class Test {
public static void main(String[] args) {
List<Person> list = Arrays.asList(
new Person(1, "Ramesh", 45, 35000),
new Person(2, "Bushan", 29, 22000),
new Person(3, "Shankar", 33, 29000),
new Person(4, "Chaitra", 32, 50000)
);
list.stream().sorted((o1, o2) -> o1.getName().compareTo(o2.getName())).forEach(System.out::println);
System.out.println("****************************");
list.stream().sorted((o1, o2) -> o2.getName().compareTo(o1.getName())).forEach(System.out::println);
}
}
Now let’s look at how to sort person’s salary using lambda’s
Example5.java
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<Person> list = Arrays.asList(
new Person(1, "Ramesh", 45, 35000),
new Person(2, "Bushan", 29, 22000),
new Person(3, "Shankar", 33, 29000),
new Person(4, "Chaitra", 32, 50000)
);
list.stream().sorted((e1, e2) -> (int)(e1.getSalary() - e2.getSalary())).forEach(System.out::println);
System.out.println("****************************");
list.stream().sorted((e1, e2) -> (int)(e2.getSalary() - e1.getSalary())).forEach(System.out::println);
}
}
Similarly, we can use Comparator
provided methods comparingLong()
, comparingDouble()
and comparingInt()
to sort long
, double
and int
values.
Hi bushan
Which version java used ?
am using java 8 but getting syntax errors here—–
(Comparator.comparing(Person::getName))
I am using Java 8 only.. please copy and paste code and try 🙂