JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

Java 8 Stream sorted method example.


The Stream.sorted() method returns a sorted Stream in the natural order by default. You can provide your own Comparator for the desired sorting order. Incase if you are going with default method Stream.sorted() without Comparator, if the elements of this stream are not Comparable, a java.lang.ClassCastException may be thrown when the terminal operation is executed.

package com.java2novice.streams;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import com.java2novice.lambda.Employee;

public class StreamSortedEx {

	public static void main(String a[]) {

		Stream.of("bus", "car", "bicycle", "flight", "train")
				.sorted()
				.forEach(System.out::println);

		System.out.println("-------------------------");

		List<Employee> empList = new ArrayList<>();
		empList.add(new Employee("Nataraja G", "Accounts", 8000));
		empList.add(new Employee("Nagesh Y", "Admin", 15000));
		empList.add(new Employee("Vasu V", "Security", 2500));
		empList.add(new Employee("Amar", "Entertainment", 12500));

		empList.stream()
				.sorted((emp1, emp2)->emp1.getSalary().compareTo(emp2.getSalary()))
				.forEach(System.out::println);
	}
}

Employee class:

package com.java2novice.lambda;

public class Employee {

	private String name;
	private String account;
	private Integer salary;

	public Employee(String name, String account, Integer salary) {
		super();
		this.name = name;
		this.account = account;
		this.salary = salary;
	}

	@Override
	public String toString() {

		return "name: "+ this.name +" | account: "+ this.account +" | salary: "+this.salary;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public Integer getSalary() {
		return salary;
	}

	public void setSalary(Integer salary) {
		this.salary = salary;
	}
}

Output:
bicycle
bus
car
flight
train
-------------------------
name: Vasu V | account: Security | salary: 2500
name: Nataraja G | account: Accounts | salary: 8000
name: Amar | account: Entertainment | salary: 12500
name: Nagesh Y | account: Admin | salary: 15000
<< Previous Program | Next Program >>

Java 8 Streams Examples

  1. How Java 8 Streams work?
  2. Java 8 Streams parallelism introduction.
  3. Explain non-interference behavior of Java 8 Streams.
  4. Create Java 8 Stream using Stream.of() method example.
  5. Create Java 8 Stream using List example.
  6. Create Java 8 Stream using Stream.generate() method.
  7. Java 8 Stream.filter() example.
  8. Java 8 Stream.map() example.
  9. Java 8 Stream flatmap method example.
  10. Java 8 Stream peek method example.
  11. Java 8 Stream distinct method example.
  12. Java 8 Stream sorted method example.
  13. Java 8 Stream limit method example.
  14. Java 8 Stream forEach method example.
  15. Java 8 Stream toArray method example.
  16. Java 8 Stream reduce method example.
  17. Java 8 Stream collect method example.
  18. Java 8 Stream concat method example.
  19. Java 8 Stream anyMatch(), allMatch() and noneMatch() example.
  20. Java 8 Stream findFirst(), findAny() example.
  21. Primitive type Stream example.
Knowledge Centre
Java class can be private?
We can not declare top level class as private. Java allows only public and default modifier for top level classes in java. Inner classes can be private.
Famous Quotations
It’s not that I’m so smart, it’s just that I stay with problems longer.
-- Albert Einstein

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.