|
|
Java 8 Stream anyMatch(), allMatch() and noneMatch() example.
Stream.anyMatch() returns whether any elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty then false is returned and the predicate is not evaluated.
Stream.allMatch() returns whether all elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result.
Stream.noneMatch() returns whether no elements of this stream match the provided predicate.
package com.java2novice.streams;
import java.util.ArrayList;
import java.util.List;
import com.java2novice.lambda.Employee;
public class StreamMatchEx {
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", "Admin", 12500));
boolean result = empList.stream().anyMatch(emp->emp.getAccount().matches("Admin"));
System.out.println(result);
result = empList.stream().allMatch(emp->emp.getAccount().matches("Admin"));
System.out.println(result);
result = empList.stream().noneMatch(emp->emp.getAccount().matches("Admin"));
System.out.println(result);
}
}
|
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;
}
}
|
|
|
|
|
|
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.
|
|
Class, Constructor and Primitive data types
Class is a template for multiple objects with similar features and it is a blue print for objects. It defines a type of object
according to the data the object can hold and the operations the object can perform. Constructor is a special kind of method that
determines how an object is initialized when created. Primitive data types are 8 types and they are: byte, short, int, long, float,
double, boolean, char.
Never argue with a fool, onlookers may not be able to tell the difference.
-- Mark Twain
|