Java8 Stream API Commonly asked Interview questions

Abhishek vt
4 min readNov 23, 2023

--

by Abhishek VT

Hi All, Below are the list of commonly asked interview questions on java8 stream API, here I have taken Student class as example and performed various stream API operations on it.

Create a Student class

public class Student {

private int id;
private String firstName;
private String lastName;
private int age;
private String gender;
private String departmantName;
private int joinedYear;
private String city;
private int rank;

public Student(int id, String firstName, String lastName, int age, String gender, String departmantName,
int joinedYear, String city, int rank) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.gender = gender;
this.departmantName = departmantName;
this.joinedYear = joinedYear;
this.city = city;
this.rank = rank;
}

public int getId() {
return id;
}

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

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 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 getDepartmantName() {
return departmantName;
}

public void setDepartmantName(String departmantName) {
this.departmantName = departmantName;
}

public int getJoinedYear() {
return joinedYear;
}

public void setJoinedYear(int joinedYear) {
this.joinedYear = joinedYear;
}

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;
}

@Override
public String toString() {
return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", age=" + age
+ ", gender=" + gender + ", departmantName=" + departmantName + ", joinedYear=" + joinedYear + ", city="
+ city + ", rank=" + rank + "]";
}

}

Create a list of Student objects

List<Student> list = Arrays.asList(
new Student(1, "Aditya", "Mall", 30, "Male", "Mechanical Engineering", 2014, "Mumbai", 122),
new Student(2, "Pulkith", "Singh", 26, "Male", "Computer Engineering", 2018, "Delhi", 67),
new Student(3, "Ankita", "Patil", 25, "Female", "Computer Engineering", 2019, "Kerala", 164),
new Student(4, "Satish", "Malaghan", 30, "Male", "Mechanical Engineering", 2014, "Kerala", 26),
new Student(5, "Darshan", "Mukd", 23, "Male", "Instrumentation Engineering", 2022, "Mumbai", 12),
new Student(6, "Chetan", "Star", 24, "Male", "Mechanical Engineering", 2023, "Karnataka", 90),
new Student(7, "Arun", "Vittal", 26, "Male", "Electronics Engineering", 2014, "Karnataka", 324),
new Student(8, "Nam", "Dev", 31, "Male", "Computer Engineering", 2014, "Karnataka", 433),
new Student(9, "Sonu", "Shankar", 27, "Female", "Computer Engineering", 2018, "Karnataka", 7),
new Student(10, "Satyam", "Pandey", 26, "Male", "Biotech Engineering", 2017, "Mumbai", 98)
);
  1. Group the students by department names.
Map<String, List<Student>> collect = list.stream().collect(Collectors.groupingBy(Student::getDepartmantName));

System.out.println("Students grouped by Department " +collect);

2. Find the count of students in each department.

 Map<String, Long> collect = list.stream().collect(Collectors.groupingBy(Student::getDepartmantName,Collectors.counting()));
System.out.println("Count of Students in each Department" + collect);

3. Find all departments names.

List<String> deptName = list.stream().map(t -> t.getDepartmantName()).distinct().collect(Collectors.toList());

System.out.println("Deaprtment Names " + deptName);

4. Find the list of students whose age is less than 25.


List<Student> collect = list.stream().filter(t -> t.getAge() < 25).collect(Collectors.toList());

System.out.println("List of Students whose Age is leass than 25 " + collect);

5. Find the max age of students.

 OptionalInt max = list.stream().mapToInt(t -> t.getAge()).max();

System.out.println("Max age of students " + max.getAsInt());

6. Find the average age of male and female students.

Map<String, Double> collect = list.stream().collect(Collectors.groupingBy(Student::getGender, Collectors.averagingInt(Student::getAge)));
System.out.println("Average age of Male and Female students " + collect);

7. Find the young student in all departments.

int min = list.stream().mapToInt(Student::getAge)
.min()
.getAsInt();

System.out.println("Minimum age of student is " + min);

// or

Student student = list.stream()
.min(Comparator.comparing(Student::getAge))
.get();

System.out.println("Young student is " + student);

8. Find the senior female student in all departments.

int seniorStudent = list.stream()
.filter(t -> t.getGender().equals("Female"))
.mapToInt(Student::getAge)
.max()
.getAsInt();

System.out.println("Senior Female student is " + seniorStudent);

//OR

Student student = list.stream()
.filter(t -> t.getGender().equals("Female"))
.max(Comparator.comparing(Student::getAge))
.get();

System.out.println("Senior Female student is " + student);

9. Find the list of students whose rank is between 50 and 100.

List<Student> collect = list.stream()
.filter(t -> t.getRank() > 50 && t.getRank() < 100)
.collect(Collectors.toList());

System.out.println("students whose rank is between 50 and 100 " + collect);

10. Find the department who is having maximum number of students.

Entry<String, Long> entry = list.stream()
.collect(Collectors.groupingBy(Student::getDepartmantName, Collectors.counting()))
.entrySet()
.stream()
.max(Map.Entry.comparingByValue())
.get();

System.out.println("Department having maximum number of students " + entry);

11. Find the Students who stays in Mumbai and sort them by their names.

List<Student> collect = list.stream()
.filter(t -> t.getCity().equals("Mumbai"))
.sorted(Comparator.comparing(Student::getFirstName))
.collect(Collectors.toList());

System.out.println(collect);

12. Find the total count of students.

long count = list.stream().count();

System.out.println("Total count " + count);

13. Find the average rank in all departments.

Map<String, Double> collect = list.stream()
.collect(Collectors.groupingBy(Student::getDepartmantName, Collectors.averagingInt(Student::getRank)));

System.out.println("Average ranks " + collect);

14. Find the highest rank in each department.

Map<String, Optional<Student>> collect = list.stream()
.collect(Collectors.groupingBy(Student::getDepartmantName,Collectors.minBy(Comparator.comparing(Student::getRank))));

System.out.println(collect);

15. Find the list of students , which are sorted by their rank.

List<Student> collect = list.stream()
.sorted(Comparator.comparing(Student::getRank))
.collect(Collectors.toList());

System.out.println(collect);

16. Find the second highest rank student.

 Student student = list.stream()
.sorted(Comparator.comparing(Student::getRank))
.skip(1).findFirst()
.get();

System.out.println("Student " + student);

17. Find the ranks of students in all department in ascending order.

Map<String, List<Student>> collect = list.stream()
.collect(Collectors.groupingBy(Student::getDepartmantName,Collectors.collectingAndThen(Collectors.toList(), list -> list.stream()
.sorted(Comparator.comparing(Student::getRank))
.collect(Collectors.toList()))));
System.out.println(collect);

Source code , Github link : https://github.com/Abhishek-Vittal-Talakeri/Java8-StreamAPI.git

Thank you for reading the article.

--

--

Abhishek vt

πŸ‘¨β€πŸ’»Java & Blockchain Developer 🌐 | Trainer πŸ“š | Interview Coach πŸŽ₯ | Sharing tutorials and tips on Java β˜•οΈ & Blockchain tech | www.abhishekvt.com