I'll be providing the most common interview questions and answers for Java 8 coding and programming in this tutorial. To answer the questions below, I have only used Stream API functions. I will try to add new questions regularly in this page. If you found some better way to solve the problems using Stream API, Please do share the solution in the comment section with question number, will update the blog according, once tested the same.
Before starting, I would like to request to create a Student Java Class which will be used throughout the blog to solve the given below problems.
1. Find list of students whose first name starts with alphabet A
1 2 | List<Student> output = list.stream().filter(s->s.getFirstName().startsWith("A")).toList(); |
2. Group the Student by Department Names
1 2 | Map<String, List<Student>> output = list.stream().collect(Collectors.groupingBy(Student::getDepartmantName)); |
3. Find the max age of Student
1 | int max_age = list.stream().mapToInt(m-> m.getAge()).max().getAsInt(); |
4. Find all departments names
1 | List<String> deptNames = list.stream().map(Student::getDepartmantName).distinct().toList(); |
5. Find the count of student in each department
1 2 | Map<String,Long> studInEachDepts = list.stream(). collect(Collectors.groupingBy(Student::getDepartmantName,Collectors.counting())); |
6. Find the list of students whose age is less than 30
1 | List<Student> output = list.stream().filter(f-> f.getAge() < 30).toList(); |
7. Find the list of students whose rank is in between 50 and 100
1 | List<Student> output = list.stream().filter(f-> (f.getRank() > 50 && f.getRank() <= 100 )).toList();
|
8. Find the number of male and female students
1 2 | Map<String, Long> mapAvgAge = list.stream(). collect(Collectors.groupingBy(Student::getGender,Collectors.counting())); |
9. Find the department who is having maximum number of students
1 2 3 | Map.Entry<String, Long> output = list.stream(). collect(Collectors.groupingBy(Student::getDepartmantName, Collectors.counting())) .entrySet().stream().max(Map.Entry.comparingByValue()).get(); |
10. Find the Students who stays in Delhi and sort them by their names in ascending order
1 2 | List<Student> output = list.stream().filter(f-> f.getCity().equalsIgnoreCase("Delhi")) .sorted(Comparator.comparing(Student::getFirstName)).toList(); |
11. Find the average rank in all departments
1 2 3 | Map<String, Double> collect = list.stream() .collect(Collectors.groupingBy(Student::getDepartmantName,Collectors.averagingInt(Student::getRank))); |
12. Find the highest rank in each department
1 2 | Map<String, Optional<Student>> studentData = list.stream() .collect(Collectors.groupingBy(Student::getDepartmantName, Collectors.maxBy(Comparator.comparing(Student::getRank)))); |
13. Group the student by rank and sort by rank
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Map<Integer, List<Student>> sameRank = list.stream() .collect( Collectors.groupingBy( Student::getRank, Collectors.toList() ) ) .entrySet() .stream() .collect( Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (e1,e2)-> e1, TreeMap::new )); |
14. Find the sum of a list of integers using reduce
1 2 3 4 5 6 | List<Integer> numbers = Arrays.asList(1,5,2,89,3,1,5,2); int output = numbers.stream().reduce(0, (a, b) -> a + b); // OR int output1 = numbers.stream().reduce(0, Integer::sum); |
15. Find average age of Student based on gender or group by gender
1 2 3 | Map<String, Double> avgAgeByGender = list.stream() .collect(Collectors.groupingBy(Student::getGender, Collectors.averagingInt(Student::getAge))); |
16. Find the longest String from a list of words
1 2 3 4 5 | List<String> words = Arrays.asList("Factory", "Abstract Factory" , "Facade", "Singleton", "Prototype"); String longestWord = words.stream() .max(Comparator.comparing(String::length)) .orElse(""); |
Similar Blogs:
Post a Comment