Java 8 Stream API allow us to process data in a declarative way, something similar to SQL.

Here are some examples:

Loop through a collection

List<String> students = new ArrayList<>();
 
students.add("Amit");
students.add("Udit");
students.add("Vishal");
students.add("Sanjay");
students.add("Shiva");

students.stream().forEach(name -> System.out.println(name));

Modifying values while reading collection

List<String> students = new ArrayList<>();
 
students.add("Amit");
students.add("Udit");
students.add("Vishal");
students.add("Sanjay");
students.add("Shiva");

students.stream()
    .map(name -> "Hello " + name.toUpperCase())
    .forEach(name -> System.out.println(name));

Filtering a collection

List<String> students = new ArrayList<>();
 
students.add("Amit");
students.add("Udit");
students.add("Vishal");
students.add("Sanjay");
students.add("Shiva");

students.stream()
    .filter(name -> name.toLowerCase().startsWith("s"))
    .forEach(name -> System.out.println(name));

Sorting a collection

List<String> students = new ArrayList<>();
 
students.add("Amit");
students.add("Udit");
students.add("Vishal");
students.add("Sanjay");
students.add("Shiva");

students.stream()
    .sorted((name1,name2) -> name1.compareTo(name2))
    .forEach(name -> System.out.println(name));

Filtering objects and getting selected fields

We want to get sorted names of students that have been rewarded

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test {

    public static void main(String[] args) {

       /****** initialize some students *********/
       List<Student> students = new ArrayList<>();
       students.add(new Student("Amit", true));
       students.add(new Student("Udit", true));
       students.add(new Student("Vishal", false));
       students.add(new Student("Sanjay", true));
       students.add(new Student("Shiva", false));
    
       /******** filter students that have been rewarded **********/
       List<Student> rewardedStudents = new ArrayList<>();
       for(Student student: students)
          if(student.isRewarded())
             rewardedStudents.add(student);
    
       /********* sort filtered students ***********/
       Collections.sort(rewardedStudents, new Comparator<Student>() {
          public int compare(Student t1, Student t2){
             return t1.getName().compareTo(t2.getName());
          }
       });
    
       /******** get names of filtered/sorted students **********/
       List<String> studentNames = new ArrayList<>();
       for(Student t: rewardedStudents)
          studentNames.add(t.getName());
       
       for(String name : studentNames)
          System.out.println(name);
    }
}
class Student{
     private String name;
     private boolean isRewarded;

     public Student(String name, boolean isRewarded) {
         this.setName(name);
         this.isRewarded = isRewarded;
     }
     public boolean isRewarded() {
         return isRewarded;
     }

     public void setRewarded(boolean isRewarded) {
         this.isRewarded = isRewarded;
     }

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

The same program using stream

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class Test {
   public static void main(String[] args) {

       List<Student> students = new ArrayList<>();
 
       students.add(new Student("Amit", true));
       students.add(new Student("Udit", true));
       students.add(new Student("Vishal", false));
       students.add(new Student("Sanjay", true));
       students.add(new Student("Shiva", false));
 
       List<String> studentNames = students.stream()
            .filter(student -> student.isRewarded())
            .sorted()
            .map(Student::getName)
            .collect(Collectors.toList());
 
       for(String name : studentNames)
           System.out.println(name);
 
    }
}

class Student implements Comparable<Student>{

   private String name; 
   private boolean isRewarded;

   public Student(String name, boolean isRewarded) {
      this.setName(name);
      this.isRewarded = isRewarded;
   }

   public boolean isRewarded() {
      return isRewarded;
   }

   public void setRewarded(boolean isRewarded) {
      this.isRewarded = isRewarded;
   }
  
   public String getName() {
      return name;
   }

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

   @Override
   public int compareTo(Student o) {
      return this.getName().compareTo(o.getName());
   }
}