package mappingtutorial;
import java.util.Comparator;
import java.util.stream.Stream;
public class CountStream {
public static void main(String[] args) {
Stream<Integer> stream = Stream.of(1, 2, 3, 3, 4);
//Count the number
System.out.println(stream.count()); //Output = 5
//Get the minimum number
Integer min = Stream.of(1, 2, 3, 3, 4).min(Comparator.comparing
(Integer::valueOf)).get();
System.out.println(“Min => ” + min);// output Min => 1
//Get the maximum number
Integer max=Stream.of(1, 2, 3, 3, 4).max(Comparator.comparing(Integer::valueOf)).get();
System.out.println(“Max => ” + max);// output Min => 4
}
}
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class FindCharacters {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
//Find first
Optional<Integer> element = list.stream().findFirst();
if(element.isPresent()) {
System.out.println(element.get());
}else {
System.out.println(“It is empty”);//Output: 1
}
Optional<Integer> elements = list.stream().findAny();
if(elements.isPresent()) {
System.out.println(elements.get());
}else {
System.out.println(“It is empty”);//Output: 1
}
}
}
package length;
public class StringLength {
public static void main(String[] args) {
String str =”Jesus is my Lord”;
int count = 0;
for(int i=0; i<str.length(); i++) {
if(str.charAt(i)!= ‘ ‘) {
count++;
}
}
System.out.println(“No of characters: ” +count);
}
}
package mappingtutorial;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Java8 {
public static void main(String[] args) {
List<Student> students = new ArrayList<Student>();
students.add(new Student(1, “John”, “password”, “john@gmail.com”));
students.add(new Student(2, “Mich”, “password”, “mich@gmail.com”));
students.add(new Student(3, “Gush”, “password”, “gush@gmail.com”));
students.add(new Student(4, “Jack”, “password”, “jack@gmail.com”));
//Java 7
List<StudentDTO> studentsDTO = new ArrayList<StudentDTO>();
for(Student student : students) {
studentsDTO.add(new StudentDTO(student.getId(), student.getUserName(), student.getEmail()));
}
for(StudentDTO dto : studentsDTO) {
System.out.println(dto);
}
//Output
/* Student [id=1, userName=John, email=john@gmail.com]
Student [id=2, userName=Mich, email=mich@gmail.com]
Student [id=3, userName=Gush, email=gush@gmail.com]
Student [id=4, userName=Jack, email=jack@gmail.com]*/
// Java 8
/* students.stream().map(new Function<Student, StudentDTO>() {
@Override
public StudentDTO apply(Student student) {
return new StudentDTO(student.getId(), student.getUserName(), student.getEmail());
}*/
//Change the above code to lambda expression
students.stream().map((Student student)->
new StudentDTO(student.getId(), student.getUserName(),
student.getEmail())).forEach((x)->{
System.out.println(x);
//Change the above code to lambda expression Collection
List<StudentDTO> studentsDTOs = students.stream().map((Student student)->
new StudentDTO(student.getId(), student.getUserName(),
student.getEmail())).collect(Collectors.toList());
studentsDTOs.forEach(System.out::println);
});
}
}
//Output
/* Student [id=1, userName=John, email=john@gmail.com]
Student [id=2, userName=Mich, email=mich@gmail.com]
Student [id=3, userName=Gush, email=gush@gmail.com]
Student [id=4, userName=Jack, email=jack@gmail.com]*/
class StudentDTO{
private int id;
private String userName;
private String email;
public StudentDTO(int id, String userName, String email) {
super();
this.id = id;
this.userName = userName;
this.email = email;
}
public static void add(StudentDTO studentDTO) {
// TODO Auto-generated method stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return “Student [id=” + id + “, userName=” + userName + “, email=” + email + “]”;
}
}
class Student{
private int id;
private String userName;
private String password;
private String email;
public Student(int id, String userName, String password, String email) {
super();
this.id = id;
this.userName = userName;
this.password = password;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getpassword() {
return password;
}
public void setPassord(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return “Student [id=” + id + “, userName=” + userName + “, password=” + password + “, email=” + email + “]”;
}
}
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;
public class EmployeeProgram {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Employee> list = new ArrayList<>();
list.add(new Employee(1, "John Doe", 111, "inactive", 25000));
list.add(new Employee(2, "Mark Bruce", 221, "active", 55000));
list.add(new Employee(3, "Gary King", 221, "active", 85000));
list.add(new Employee(4, "Jerry Khan", 111, "active", 80000));
list.add(new Employee(5, "Congo Box", 111, "active", 66000));
//List all employees by department
list.stream().collect(Collectors.groupingBy(Employee::getDeptId, Collectors.toList()));
Map<Integer, List<Employee>>listBasedOnDept=list.stream().collect(Collectors.groupingBy(Employee::getDeptId, Collectors.toList()));
listBasedOnDept.entrySet().forEach(x -> {
System.out.println(x.getKey() + "--" + x.getValue()
);
});
/*System.out.println("List all employees by department ....");
221--[Employee [empid=2, empName=Mark Bruce, deptId=221, status=active, salary=55000], Employee [empid=3, empName=Gary King, deptId=221, status=active, salary=85000]]
111--[Employee [empid=1, empName=John Doe, deptId=111, status=inactive, salary=25000], Employee [empid=4, empName=Jerry Khan, deptId=111, status=active, salary=80000], Employee [empid=5, empName=Congo Box, deptId=111, status=active, salary=66000]]*/
//Counting no. of departments and employees in each department
Map<Integer, Long> empCountDept = list.stream().collect(Collectors.groupingBy(Employee::getDeptId, Collectors.counting()));
empCountDept.entrySet().forEach(x-> {
System.out.println(x.getKey() + "--" + x.getValue());
});
/*System.out.println("//Counting no. of departments and employees in each department ....");
221--2
111--3*/
//Count active and inactive employees
Long activeEmpCount = list.stream().filter(x -> "active".equals(x.getStatus())).count();
Long inactiveEmpCount = list.stream().filter(x -> "inactive".equals(x.getStatus())).count();
System.out.println("active emp count -- " + activeEmpCount);
System.out.println("inactive emp count -- " + inactiveEmpCount);
/*System.out.println("Count active and inactive employees ....");
active emp count -- 4
inactive emp count -- 1*/
System.out.println("Print maximum and minimum salaries ....");
//Print maximum and minimum salaries
Optional<Employee> emp1 =list.stream().max(Comparator.comparing(Employee::getSalary));
Optional<Employee> emp2 =list.stream().min(Comparator.comparing(Employee::getSalary));
System.out.println(emp1);
System.out.println(emp2);
/* Print maximum and minimum salaries ....
Optional[Employee [empid=3, empName=Gary King, deptId=221, status=active, salary=85000]]
Optional[Employee [empid=1, empName=John Doe, deptId=111, status=inactive, salary=25000]]*/
System.out.println("List department and top salary earners ....");
//List department and top salary earners
Map<Integer, Optional<Employee>> topSalaryEmpDept = list.stream().collect(Collectors.groupingBy(Employee::getDeptId,
Collectors.reducing(BinaryOperator.maxBy(Comparator.comparing(Employee::getSalary)))));
topSalaryEmpDept.entrySet().forEach(x-> {
System.out.println("Dept " + x.getKey() + " topEmp " + x.getValue());
});
/* List department and top salary earners ....
Dept 221 topEmp Optional[Employee [empid=3, empName=Gary King, deptId=221, status=active, salary=85000]]
Dept 111 topEmp Optional[Employee [empid=4, empName=Jerry Khan, deptId=111, status=active, salary=80000]]*/
}
}
Very good article. I definitely appreciate this website. Stick with it!