|
| 1 | +package com.interview.qa; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.util.stream.Collectors; |
| 5 | +import java.util.stream.Stream; |
| 6 | + |
| 7 | +public class Java8CommonProgrammingQA { |
| 8 | + |
| 9 | + public static void main(String[] args) { |
| 10 | + List<Student> studentList = Stream.of( |
| 11 | + new Student(1, "Rohit", 30, "Male", "Mechanical Engineering", "Mumbai", 122, Arrays.asList("+912632632782", "+1673434729929")), |
| 12 | + new Student(2, "Pulkit", 56, "Male", "Computer Engineering", "Delhi", 67, Arrays.asList("+912632632762", "+1673434723929")), |
| 13 | + new Student(3, "Ankit", 25, "Female", "Mechanical Engineering", "Kerala", 164, Arrays.asList("+912632633882", "+1673434709929")), |
| 14 | + new Student(4, "Satish Ray", 30, "Male", "Mechanical Engineering", "Kerala", 26, Arrays.asList("+9126325832782", "+1671434729929")), |
| 15 | + new Student(5, "Roshan", 23, "Male", "Biotech Engineering", "Mumbai", 12, Arrays.asList("+012632632782")), |
| 16 | + new Student(6, "Chetan", 24, "Male", "Mechanical Engineering", "Karnataka", 90, Arrays.asList("+9126254632782", "+16736784729929")), |
| 17 | + new Student(7, "Arun", 26, "Male", "Electronics Engineering", "Karnataka", 324, Arrays.asList("+912632632782", "+1671234729929")), |
| 18 | + new Student(8, "Nam", 31, "Male", "Computer Engineering", "Karnataka", 433, Arrays.asList("+9126326355782", "+1673434729929")), |
| 19 | + new Student(9, "Sonu", 27, "Female", "Computer Engineering", "Karnataka", 7, Arrays.asList("+9126398932782", "+16563434729929", "+5673434729929")), |
| 20 | + new Student(10, "Shubham", 26, "Male", "Instrumentation Engineering", "Mumbai", 98, Arrays.asList("+912632646482", "+16734323229929"))) |
| 21 | + .collect(Collectors.toList()); |
| 22 | + |
| 23 | + // 1. Find the list of students whose rank is in between 50 and 100 |
| 24 | + |
| 25 | + List<Student> students = studentList.stream().filter(student -> student.getRank() > 50 && student.getRank() < 100) |
| 26 | + .collect(Collectors.toList()); |
| 27 | + // System.out.println(students); |
| 28 | + |
| 29 | + //2. Find the Students who stays in Karnataka and sort them by their names |
| 30 | + |
| 31 | + List<Student> studentsByCity = studentList.stream().filter(student -> student.getCity().equals("Karnataka")) |
| 32 | + .sorted(Comparator.comparing(Student::getFirstName,Comparator.reverseOrder())).collect(Collectors.toList()); |
| 33 | + // System.out.println(studentsByCity); |
| 34 | + |
| 35 | + // 3. Find all departments names |
| 36 | + |
| 37 | + List<String> deptNames = studentList.stream() |
| 38 | + .map(Student::getDept) |
| 39 | + .distinct() |
| 40 | + .collect(Collectors.toList()); |
| 41 | + |
| 42 | + Set<String> deptNamesInSet = studentList |
| 43 | + .stream().map(Student::getDept) |
| 44 | + .collect(Collectors.toSet()); |
| 45 | + |
| 46 | + //System.out.println(deptNames); |
| 47 | + //System.out.println(deptNamesInSet); |
| 48 | + |
| 49 | + //4. Find all the contact numbers |
| 50 | + List<String> contacts = studentList.stream() |
| 51 | + .flatMap(student -> student.getContacts().stream()) |
| 52 | + .distinct() |
| 53 | + .collect(Collectors.toList()); |
| 54 | + System.out.println(contacts); |
| 55 | + //one2one-> map |
| 56 | + //one2many-> flatmap |
| 57 | + |
| 58 | + //5. Group The Student By Department Names |
| 59 | + |
| 60 | + |
| 61 | + Map<String, List<Student>> studentMap = studentList.stream() |
| 62 | + .collect(Collectors.groupingBy(Student::getDept)); |
| 63 | + // System.out.println(studentMap); |
| 64 | + |
| 65 | + |
| 66 | + //6. Find the department who is having maximum number of students |
| 67 | + Map.Entry<String, Long> results = studentList.stream() |
| 68 | + .collect(Collectors.groupingBy(Student::getDept, Collectors.counting())) |
| 69 | + .entrySet().stream().max(Map.Entry.comparingByValue()).get(); |
| 70 | + |
| 71 | + System.out.println(results); |
| 72 | + |
| 73 | + //7. Find the average age of male and female students |
| 74 | + |
| 75 | + Map<String, Double> avgStudents = studentList.stream() |
| 76 | + .collect(Collectors |
| 77 | + .groupingBy(Student::getGender, |
| 78 | + Collectors.averagingInt(Student::getAge))); |
| 79 | + |
| 80 | + //System.out.println(avgStudents); |
| 81 | + |
| 82 | + //8. Find the highest rank in each department |
| 83 | + |
| 84 | + Map<String, Optional<Student>> stdMap = studentList.stream() |
| 85 | + .collect(Collectors.groupingBy(Student::getDept, |
| 86 | + Collectors.minBy(Comparator.comparing(Student::getRank)))); |
| 87 | + // System.out.println(stdMap); |
| 88 | + |
| 89 | + //9 .Find the student who has second rank |
| 90 | + |
| 91 | + Student student = studentList.stream() |
| 92 | + .sorted(Comparator.comparing(Student::getRank)) |
| 93 | + .skip(2) |
| 94 | + .findFirst().get(); |
| 95 | + System.out.println(student); |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | +} |
0 commit comments