avatarAmar Balu

Summary

The provided web content is a tutorial on Java 8 Stream API, offering insights into common interview questions and their solutions using Stream operations.

Abstract

The article titled "Most Commonly asked Java8 Stream based Interview Question — Part 1" is designed to prepare candidates for Java Stream API interview questions. It introduces an EmployeeObject class and populates a list with instances representing employees. The tutorial then demonstrates how to use Java 8 Streams to solve common problems such as counting employees by gender, listing department names, calculating average age, finding the highest-paid employee, and listing employees who joined after a certain year. The examples provided in the article are practical and aimed at enhancing the reader's understanding of Stream operations in Java.

Opinions

  • The author believes that understanding Stream-based questions is crucial for Java interviews.
  • The article implies that mastery of the Stream API is an important skill for Java developers.
  • The use of real-world examples (e.g., employee data) suggests that the author values practical application of Java Streams.
  • By providing a link to a publication, the author encourages readers to follow for more content, indicating a commitment to continuous learning and community engagement.
  • The inclusion of a YouTube reference at the end of the article suggests that the author endorses multimedia resources as complementary learning tools.

Most Commonly asked Java8 Stream based Interview Question — Part 1

Hello there, I hope everyone is doing well.

This article will help you to get some idea about how Stream based interview questions will be asked.

Image Source : https://javadeveloperzone.com/spring-boot/spring-boot-tutorial/

I will be sharing here some common questions asked along with its answers. In most of the questions, you will be asked to create a List of Custom Object.

Example Employee Class with Employee Object having data about Employee like emp_id, salary, name, Date_of_Joining, Designation. Student Class with student Object having data about Student like student_id, name, class, Stream, Date_of_birth.

The Questions will be then based on this List of Objects.

Implementation Example

Here I am creating an Employee class.

public class EmployeeObject {
    int emp_id;
    String emp_name;
    int emp_age;
    String emp_gender;
    String emp_dep;
    int year_of_joining;
    double emp_salary;

    
    public EmployeeObject(int emp_id, String name, int age, String gender, String department, int year_of_joining,
    double salary) {
        this.emp_id = emp_id;
        this.emp_name = name;
        this.emp_age = age;
        this.emp_gender = gender;
        emp_dep = department;
        this.year_of_joining = year_of_joining;
        this.emp_salary = salary;
    }


    public int getEmp_id() {
        return emp_id;
    }


    public void setEmp_id(int emp_id) {
        this.emp_id = emp_id;
    }


    public String getEmp_name() {
        return emp_name;
    }


    public void setEmp_name(String emp_name) {
        this.emp_name = emp_name;
    }


    public int getEmp_age() {
        return emp_age;
    }


    public void setEmp_age(int emp_age) {
        this.emp_age = emp_age;
    }


    public String getEmp_gender() {
        return emp_gender;
    }


    public void setEmp_gender(String emp_gender) {
        this.emp_gender = emp_gender;
    }


    public String getEmp_dep() {
        return emp_dep;
    }


    public void setEmp_dep(String emp_dep) {
        this.emp_dep = emp_dep;
    }


    public int getYear_of_joining() {
        return year_of_joining;
    }


    public void setYear_of_joining(int year_of_joining) {
        this.year_of_joining = year_of_joining;
    }


    public double getEmp_salary() {
        return emp_salary;
    }


    public void setEmp_salary(double emp_salary) {
        this.emp_salary = emp_salary;
    }

   
}

This is how the list of the Employee Objects being created.

public class StreamInterviewQuestion {
 static List<EmployeeObject> employeeObjectList = new ArrayList<EmployeeObject>();

 public static void main(String[] args) {
employeeObjectList.add(new EmployeeObject(111, "Jeya", 30, "Female", "HR", 2011, 25000.0));
  employeeObjectList.add(new EmployeeObject(122, "Polish", 29, "Male", "Sales", 2015, 18500.0));
  employeeObjectList.add(new EmployeeObject(133, "Thomas", 26, "Male", "Administartion", 2012, 18700.0));
  employeeObjectList.add(new EmployeeObject(144, "Gowthami", 29, "Female", "Development", 2014, 33500.0));
  employeeObjectList.add(new EmployeeObject(155, "Nisha", 25, "Female", "HR", 2013, 22000.0));
  employeeObjectList.add(new EmployeeObject(166, "Issac", 40, "Male", "Maintenance", 2016, 12000.0));
  employeeObjectList.add(new EmployeeObject(177, "Sharmila", 30, "Female", "Finance", 2010, 29000.0));
  employeeObjectList.add(new EmployeeObject(188, "Linga", 33, "Male", "Development", 2015, 35000.0));
  employeeObjectList.add(new EmployeeObject(199, "John", 34, "Male", "Sales", 2016, 14500.0));
  employeeObjectList.add(new EmployeeObject(200, "Jeyam", 36, "Male", "Maintenance", 2015, 17000.0));
  employeeObjectList.add(new EmployeeObject(211, "kumar", 37, "Male", "Administartion", 2014, 18700.0));
  employeeObjectList.add(new EmployeeObject(222, "Joshi", 25, "Male", "Development", 2016, 29000.0));
  employeeObjectList.add(new EmployeeObject(233, "Reddy", 29, "Male", "Finance", 2013, 27000.0));
  employeeObjectList.add(new EmployeeObject(244, "Denwer", 28, "Male", "Sales", 2017, 18000.));
  employeeObjectList.add(new EmployeeObject(255, "Alia", 26, "Female", "Administartion", 2018, 13000.0));
  employeeObjectList.add(new EmployeeObject(266, "Sangavi", 36, "Female", "Development", 2015, 29000.0));
  employeeObjectList.add(new EmployeeObject(277, "Anuja", 32, "Female", "Development", 2012, 38000.0));
}
}

Now let’s look into the Questions being asked on this.

1. How many EmployeeObjects in the company are male and female?

Here we use use the groupingBy method based on the Gender and called counting method to count based on the Gender.

Map<String, Long> countOfEmployee = employeeObjectList.stream()
    .collect(Collectors.groupingBy(EmployeeObject::getEmp_gender, Collectors.counting()));
  System.out.println(countOfEmployee);

The answer to the above question is as follows

Answer 1 : How many EmployeeObjects in the company are male and female?
{Male=10, Female=7}

2. How about printing the names of every department in the company?

We use getDepartment() method to get all the name of the department as a distinct names.

employeeObjectList.stream().map(EmployeeObject::getEmp_dep).distinct().forEach(System.out::println);

The answer to the above question is as follows

Answer 2 : printing the names of every department in the company?
HR
Sales
Administartion
Development
Maintenance
Finance

3. What is the average age of the employees, including men and women?

Here we use use the groupingBy method based on the Gender and collecting the average of the Age of all employees based on Gender.

Map<String, Double> avgAgeOfEmployees = employeeObjectList.stream()
    .collect(Collectors.groupingBy(EmployeeObject::getEmp_gender, Collectors.averagingInt(EmployeeObject::getEmp_age)));
  System.out.println(avgAgeOfEmployees);

The answer to the above question is as follows

Answer 3 : What is the average age of the employees, including men and women?
{Male=31.7, Female=29.714285714285715}

4. Find the Highest paid employee in the Company?

Here we are trying to get the details of the Employee whose salary is highest among all by using the Comparator Interface and finding the Max salary among all the employees.

Optional<EmployeeObject> highestSalary = employeeObjectList.stream()
    .collect(Collectors.maxBy(Comparator.comparingDouble(EmployeeObject::getEmp_salary)));
  System.out.println(highestSalary.get().getEmp_name());

The Answer to the above question is as follows

Answer 4 : Find the Highest paid employee in the Company?
Anuja 

5. List the names of all employees in the Company who have joined after 2014?

This Question is quite easy, you can use filter the joining date of the employees based on the year and then you can use map function to get the name of the Employee and print their name.

  employeeObjectList.stream().filter(e -> e.getYear_of_joining() > 2015).map(EmployeeObject::getEmp_name)
    .forEach(System.out::println);

The Answer to the above question is as follows

Answer 5 : List the names of all employees in the Company who have joined after 2014?
Polish
Issac
Linga
John
Jeyam
Joshi
Denwer
Alia
Sangavi

The remaining questions will be covered in the next part. Stay tuned…!!

Thanks for reading.Happy learning 😄

Do support our publication by following it

Reference :

Java
Java8
Stream
Spring
Interview
Recommended from ReadMedium