Java 8 Stream Programming Interview Questions and Answers (2024)

In Java 8, Stream api is most import part because it perform any type of operations.So Stream coding concept should be clear to 

clear interview. Interviewer may be ask you different topic questions, like as: Java Streams Coding Questions and Answers or Java 8 Interview Questions (2024) or Coding Based Java 8 Coding Interview Question or Top Java 8 Interview Questions & Answers or Top 10 Java 8 Coding and Programming Interview or java 8 stream programming questions and answers


Below there are different questions related to Stream api..  

1). Find the second highest salary using Java8 streams or How to find second largest salary in Java 8? or How to get highest salary of employee in Java 8? or Find second Salary of Employee in java 8 strems api ? or To find out the second highest salary using java 8


  

import java.util.Arrays;

import java.util.Comparator;

import java.util.List;

import java.util.Optional;

import java.util.stream.Collectors;


class Employee {

int id;

String empName;

float salary;

public Employee(int id, String empName, float salary) {

this.id = id;

this.empName = empName;

this.salary = salary;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getEmpName() {

return empName;

}

public void setEmpName(String empName) {

this.empName = empName;

}

public float getSalary() {

return salary;

}

public void setSalary(float salary) {

this.salary = salary;

}

@Override

public String toString() {

return “Employee [id=” + id + “, empName=” + empName + “, salary=” + salary + “]”;

}

}


public class Second_highest_salaried_Employee {

public static void main(String[] args) {

List<Employee> employeeList = Arrays.asList(new Employee(100, “Shekher”, 500000),

new Employee(101, “Trilok”, 300000), new Employee(102, “Aryan”, 200000),

new Employee(104, “Sanjay”, 100000));


// Second Highest Salary of Employee

Optional<Employee> secondHisghestSalary = employeeList.stream()

.sorted(Comparator.comparing(Employee::getSalary).reversed()).skip(1).findFirst();

System.out.println(“Second Highest Salary of Employee” + secondHisghestSalary.get());

}

}



2). To find the highest salary of employee in java 8 or How to find highest salary in Java 8? or What is maxBy function in Java 8?

or how to get max salary from employee list in java or second highest salary in java 8 ?


     import java.util.Arrays;

import java.util.Comparator;

import java.util.List;

import java.util.Optional;

import java.util.stream.Collectors;


class Employee {

int id;

String empName;

float salary;

public Employee(int id, String empName, float salary) {

this.id = id;

this.empName = empName;

this.salary = salary;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getEmpName() {

return empName;

}

public void setEmpName(String empName) {

this.empName = empName;

}

public float getSalary() {

return salary;

}

public void setSalary(float salary) {

this.salary = salary;

}

@Override

public String toString() {

return “Employee [id=” + id + “, empName=” + empName + “, salary=” + salary + “]”;

}

}


public class Second_highest_salaried_Employee {

public static void main(String[] args) {

List<Employee> employeeList = Arrays.asList(new Employee(100, “Shekher”, 500000),

new Employee(101, “Trilok”, 300000), new Employee(102, “Aryan”, 200000),

new Employee(104, “Sanjay”, 100000));


// Maximum or max salary of Employee

Optional<Employee> maxSalary = employeeList.stream()

.collect(Collectors.maxBy(Comparator.comparing(Employee::getSalary)));

System.out.println(“Max Salary of Employee : “ + maxSalary.get());

}

}


3). To find the minimum salary of employee in java 8 ? or What is minBy function in Java 8?


     import java.util.Arrays;

import java.util.Comparator;

import java.util.List;

import java.util.Optional;

import java.util.stream.Collectors;


class Employee {

int id;

String empName;

float salary;

public Employee(int id, String empName, float salary) {

this.id = id;

this.empName = empName;

this.salary = salary;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getEmpName() {

return empName;

}

public void setEmpName(String empName) {

this.empName = empName;

}

public float getSalary() {

return salary;

}

public void setSalary(float salary) {

this.salary = salary;

}

@Override

public String toString() {

return “Employee [id=” + id + “, empName=” + empName + “, salary=” + salary + “]”;

}

}


public class Second_highest_salaried_Employee {

public static void main(String[] args) {

List<Employee> employeeList = Arrays.asList(new Employee(100, “Shekher”, 500000),

new Employee(101, “Trilok”, 300000), new Employee(102, “Aryan”, 200000),

new Employee(104, “Sanjay”, 100000));


// Minimum or min salary of Employee

Optional<Employee> minSalary = employeeList.stream()

.collect(Collectors.minBy(Comparator.comparing(Employee::getSalary)));

System.out.println(“Max Salary of Employee : “ + minSalary.get());

}

}