public class Employer {
private Integer id;
private Integer age;
private String gender;
private String firstName;
private String lastName;
public static Predicate<Employer> ageGreaterThan70 = x -> x.getAge() >70;
public static Predicate<Employer> genderM = x -> x.getGender().equals("M");
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Employer(Integer id, Integer age, String gender, String firstName, String lastName) {
this.id = id;
this.age = age;
this.gender = gender;
this.firstName = firstName;
this.lastName = lastName;
}
public Employer() {
}
@Override
public String toString() {
return "Employer{" +
"id=" + id +
", age=" + age +
", gender='" + gender + '\'' +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}
public static void main(String[] args) {
Employer e1 = new Employer(1,23,"M","Rick","Beethovan");
Employer e2 = new Employer(2,13,"F","Martina","Hengis");
Employer e3 = new Employer(3,43,"M","Ricky","Martin");
Employer e4 = new Employer(4,26,"M","Jon","Lowman");
Employer e5 = new Employer(5,19,"F","Cristine","Maria");
Employer e6 = new Employer(6,15,"M","David","Feezor");
Employer e7 = new Employer(7,68,"F","Melissa","Roy");
Employer e8 = new Employer(8,79,"M","Alex","Gussin");
Employer e9 = new Employer(9,15,"F","Neetu","Singh");
Employer e10 = new Employer(10,45,"M","Naveen","Jain");
List<Employer> employees = Arrays.asList(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10);
List<Employer> filterAnd = employees.stream().filter(Employer.ageGreaterThan70.and(Employer.genderM)).collect(Collectors.toList());
System.out.println(filterAnd);
List<Employer> filterOr = employees.stream().filter(Employer.ageGreaterThan70.or(Employer.genderM)).collect(Collectors.toList());
System.out.println(filterOr);
List<Employer> filterNegate = employees.stream().filter(Employer.ageGreaterThan70.or(Employer.genderM).negate()).collect(Collectors.toList());
System.out.println(filterNegate);
System.out.println("------------------");
List<Employer> map = employees.stream().map(e -> {
e.setAge(e.getAge() + 1);
e.setGender(e.getGender().equals("M") ? "F" : "M");
return e;
}).collect(Collectors.toList());
System.out.println(map);
}
Integer total = employees.stream().map(Employer::getAge).reduce(0, Integer::sum);
System.out.println(total);
Integer total2 = employees.stream().reduce(0, (totalAge, emp) -> totalAge + emp.getAge(), Integer::sum);
System.out.println(total2);
Integer total3 = employees.parallelStream().map(Employer::getAge).reduce(0, Integer::sum,Integer::sum);
System.out.println(total3);