Java 8 Stream concat method example.
The Stream.concat() method creates a lazily concatenated stream whose elements are all the elements of the
first stream followed by all the elements of the second stream. The resulting stream is ordered if both of the input streams
are ordered, and parallel if either of the input streams is parallel. When the resulting stream is closed, the close handlers
for both input streams are invoked.
package com.java2novice.streams;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import com.java2novice.lambda.Employee;
public class StreamConcatEx {
public static void main(String a[]) {
List<Employee> accEmpList = new ArrayList<>();
accEmpList.add(new Employee("Nataraja G", "Accounts", 8000));
accEmpList.add(new Employee("Nagesh Y", "Accounts", 15000));
List<Employee> adminEmpList = new ArrayList<>();
adminEmpList.add(new Employee("Vasu V", "Admin", 2500));
adminEmpList.add(new Employee("Amar", "Admin", 12500));
Stream.concat(accEmpList.stream(), adminEmpList.stream()).forEach(System.out::println);
}
}
|
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;
}
}
|
|