Search Bar

Java 8 Stream API Interview ready coding questions that everyone should know.


Java 8 stream API interview question: Every Java developer should know how to perform operations on custom class or any collection.

 

Hello Java Developers,

Today I am very excited to share some meaningful interview perspectives Java 8 stream API coding questions as many of the industries now working on JAVA 8, JAVA 11, and JAVA 17 as well.

Before starting with todays article, you should know about the JAVA 8 Features and Stream API earlier I wrote one article on these where I explained What is JAVA 8 Stream API? How do create Stream, and Lambda expressions, what are the main terminal and intermediate functions of JAVA 8 Stream API. I recommend first going and reading about these topics.

Java 8 Stream API Interview ready coding questions
Java 8 Stream API Interview ready coding questions

Lets see what things we need before we can start.

1. Need a Student class with few properties.

2. Then we need some dummy data to play with Java 8 Stream API functions.

3. Note. we are covering map, filter, flatMap, collect, Collectors, groupingBy, couting, etc fuctions. 

Stuedent.java 

package test.amolsoftwares;

import java.util.List;
import java.util.Objects;

public class Student {

private int id;
private String name;
private int age;
private String gender;
private String dept;
private String city;
private int rank;
private List<String> contact;

public Student(int id, String name, int age, String gender, String dept, String city, int rank, List<String> contact) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.dept = dept;
this.city = city;
this.rank = rank;
this.contact = contact;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public String getDept() {
return dept;
}

public void setDept(String dept) {
this.dept = dept;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public int getRank() {
return rank;
}

public void setRank(int rank) {
this.rank = rank;
}

public List<String> getContact() {
return contact;
}

public void setContact(List<String> contact) {
this.contact = contact;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student student)) return false;
return id == student.id && age == student.age && rank == student.rank && Objects.equals(name, student.name) && Objects.equals(gender, student.gender) && Objects.equals(dept, student.dept) && Objects.equals(city, student.city) && Objects.equals(contact, student.contact);
}

@Override
public int hashCode() {
return Objects.hash(id, name, age, gender, dept, city, rank, contact);
}

@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", dept='" + dept + '\'' +
", city='" + city + '\'' +
", rank=" + rank +
", contact=" + contact +
'}';
}
}




Covering Interview questions like: 

//Q1. Find the list of students whose rank is in between 50 and 100
//Q2. Find the Students who stay in Phoenix and sort them by their names.
//Q3. Find all the department names
//Q4. Find all contact numbers



//Q5. Group the students by Department names
//Q6. Group the students by Department names (students present in numbers)
//Q6. In each department how many students are their?
//Q7. In which department having maximum students?
//Q8. Find the average age of male and female students
//Q9. Find the highest rank in each department
//Q10. Find the student who has second rank

InterviewPrep.java

package test.amolsoftwares;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class InterviewPrep {

public static void main(String[] args) {

List<Student> studentList = generateStudentData();

//Q1. Find the list of students whose rank is in between 50 and 100
List<Student> students50bet100 = studentList.stream()
.filter(s -> s.getRank() > 50 && s.getRank() < 100)
.toList();



//Q2. Find the Students who stay in Phoenix and sort them by their names.
List<Student> studentsFromPhoenix = studentList.stream()
.filter(s -> s.getCity().equals("Phoenix"))
.sorted(Comparator.comparing(Student::getName))
.toList();



//Q3. Find all the department names
List<String> departments = studentList.stream()
.map(s -> s.getDept()).distinct().toList();
Set<String> departmentNames = studentList.stream()
.map(Student::getDept)
.collect(Collectors.toSet());

//Q4. Find all contact numbers
List<String> allContacts = studentList.stream()
.flatMap(s -> s.getContact().stream()).toList();

//Q5 Group the students by Department names
Map<String, List<Student>> studentsByDepartments = studentList.stream()
.collect(Collectors.groupingBy(Student::getDept));

//Q6 Group the students by Department names (students present in numbers)
//Q6 In each department how many students are their?
Map<String, Long> studByDept = studentList.stream()
.collect(Collectors.groupingBy(Student::getDept, Collectors.counting()));

//Q7 In which department having maximum students?
Map.Entry<String, Long> maxStudentFromDepartment = studentList.stream()
.collect(Collectors.groupingBy(Student::getDept, Collectors.counting()))
.entrySet().stream()
.max(Map.Entry.comparingByValue()).get();

//Q8 Find the average age of male and female students
Map<String, Double> avgStudents = studentList.stream()
.collect(Collectors.groupingBy(Student::getGender,
Collectors.averagingInt(Student::getAge)));

//Q9 Find the highest rank in each department
Map<String, Optional<Student>> maxRankByDepartment = studentList.stream()
.collect(Collectors.groupingBy(Student::getDept,
Collectors.minBy(Comparator.comparing(Student::getRank))));

//Q10 Find the student who has second rank
Student secondStudentByRank = studentList.stream()
.sorted(Comparator.comparing(Student::getRank))
.skip(1)
.findFirst().get();


}

private static List<Student> generateStudentData() {

return Stream.of(
new Student(1, "John Doe", 20, "M", "Computer Science", "New York", 56, List.of("1234567890", "9876543210")),
new Student(2, "Jane Smith", 22, "F", "Electrical Engineering", "Los Angeles", 78, List.of("9876543210", "1234567890")),
new Student(3, "Bob Johnson", 21, "M", "Mechanical Engineering", "Boston", 34, List.of("5555555555", "4444444444")),
new Student(4, "Alice Brown", 23, "F", "Civil Engineering", "Boston", 89, List.of("1111111111", "2222222222")),
new Student(5, "Chris Wilson", 24, "M", "Chemical Engineering", "Phoenix", 67, List.of("9999999999", "8888888888")),
new Student(6, "Emily Davis", 20, "F", "Biomedical Engineering", "Philadelphia", 45, List.of("7777777777", "6666666666")),
new Student(7, "Michael Miller", 22, "M", "Computer Science", "San Antonio", 23, List.of("3333333333", "2222222222")),
new Student(8, "Sophia Garcia", 21, "F", "Electrical Engineering", "Phoenix", 12, List.of("4444444444", "5555555555")),
new Student(9, "David Martinez", 23, "M", "Mechanical Engineering", "Washington", 56, List.of("6666666666", "7777777777")),
new Student(10, "Olivia Rodriguez", 24, "F", "Civil Engineering", "San Jose", 78, List.of("8888888888", "9999999999")),
new Student(11, "Matthew Hernandez", 20, "M", "Chemical Engineering", "Washington", 90, List.of("1231231234", "9879879876")),
new Student(12, "Ava Jackson", 22, "F", "Biomedical Engineering", "Phoenix", 45, List.of("7654321098", "8765432109")),
new Student(13, "William Taylor", 21, "M", "Computer Science", "Phoenix", 67, List.of("1112223333", "4445556666")),
new Student(14, "Ella Anderson", 23, "F", "Electrical Engineering", "Washington", 34, List.of("7778889999", "1234567890")),
new Student(15, "James Davis", 24, "M", "Mechanical Engineering", "Boston", 56, List.of("9876543210", "5555555555")),
new Student(16, "Grace Harris", 20, "F", "Civil Engineering", "Boston", 78, List.of("111122223333", "444455556666")),
new Student(17, "Daniel Thompson", 22, "M", "Chemical Engineering", "Seattle", 15, List.of("777788889999", "1234567890")),
new Student(18, "Chloe White", 21, "F", "Biomedical Engineering", "Phoenix", 45, List.of("9876543210", "5555555555")),
new Student(19, "Benjamin Scott", 23, "M", "Computer Science", "Washington", 67, List.of("111122223333", "444455556666")),
new Student(20, "Zoe Green", 24, "F", "Electrical Engineering", "Boston", 34, List.of("777788889999", "1234567890"))
).toList();

}

}







Post a Comment

0 Comments