|
|
Java 8 Stream.filter() example.
Stream.filter() method returns a stream consisting of the elements of this stream that match the given predicate.
The given below example, filters all the employees with salary above 10000 rupees.
package com.java2novice.streams;
import java.util.ArrayList;
import java.util.List;
import com.java2novice.lambda.Employee;
public class StreamFilterEx {
public static void main(String a[]) {
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", "Entertinment", 12500));
// find employees whose salaries are above 10000
empList.stream().filter(emp->emp.getSalary() > 10000).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: |
name: Nagesh Y | account: Admin | salary: 15000
name: Amar | account: Entertinment | salary: 12500
|
|
|
|
|
Java 8 Streams Examples
- How Java 8 Streams work?
- Java 8 Streams parallelism introduction.
- Explain non-interference behavior of Java 8 Streams.
- Create Java 8 Stream using Stream.of() method example.
- Create Java 8 Stream using List example.
- Create Java 8 Stream using Stream.generate() method.
- Java 8 Stream.filter() example.
- Java 8 Stream.map() example.
- Java 8 Stream flatmap method example.
- Java 8 Stream peek method example.
- Java 8 Stream distinct method example.
- Java 8 Stream sorted method example.
- Java 8 Stream limit method example.
- Java 8 Stream forEach method example.
- Java 8 Stream toArray method example.
- Java 8 Stream reduce method example.
- Java 8 Stream collect method example.
- Java 8 Stream concat method example.
- Java 8 Stream anyMatch(), allMatch() and noneMatch() example.
- Java 8 Stream findFirst(), findAny() example.
- Primitive type Stream example.
|
|
What is fail-fast in java?
A fail-fast system is nothing but immediately report any failure that
is likely to lead to failure. When a problem occurs, a fail-fast system
fails immediately. In Java, we can find this behavior with iterators.
Incase, you have called iterator on a collection object, and another
thread tries to modify the collection object, then concurrent modification
exception will be thrown. This is called fail-fast.
When I do good, I feel good; when I do bad, I feel bad, and that is my religion.
-- Abraham Lincoln
|