Skip to content

Commit e8d8b06

Browse files
authored
Add files via upload
1 parent be49364 commit e8d8b06

File tree

2 files changed

+218
-0
lines changed

2 files changed

+218
-0
lines changed

qa/Java8CommonProgrammingQA.java

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}

qa/Student.java

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.interview.qa;
2+
3+
import java.util.List;
4+
import java.util.Objects;
5+
6+
public class Student {
7+
8+
private int id;
9+
private String firstName;
10+
private int age;
11+
private String gender;
12+
private String dept;
13+
private String city;
14+
private int rank;
15+
private List<String> contacts;
16+
17+
public Student(int id, String firstName, int age, String gender, String dept, String city, int rank, List<String> contacts) {
18+
this.id = id;
19+
this.firstName = firstName;
20+
this.age = age;
21+
this.gender = gender;
22+
this.dept = dept;
23+
this.city = city;
24+
this.rank = rank;
25+
this.contacts = contacts;
26+
}
27+
28+
public int getId() {
29+
return id;
30+
}
31+
32+
public void setId(int id) {
33+
this.id = id;
34+
}
35+
36+
public String getFirstName() {
37+
return firstName;
38+
}
39+
40+
public void setFirstName(String firstName) {
41+
this.firstName = firstName;
42+
}
43+
44+
public int getAge() {
45+
return age;
46+
}
47+
48+
public void setAge(int age) {
49+
this.age = age;
50+
}
51+
52+
public String getGender() {
53+
return gender;
54+
}
55+
56+
public void setGender(String gender) {
57+
this.gender = gender;
58+
}
59+
60+
public String getDept() {
61+
return dept;
62+
}
63+
64+
public void setDept(String dept) {
65+
this.dept = dept;
66+
}
67+
68+
public String getCity() {
69+
return city;
70+
}
71+
72+
public void setCity(String city) {
73+
this.city = city;
74+
}
75+
76+
public int getRank() {
77+
return rank;
78+
}
79+
80+
public void setRank(int rank) {
81+
this.rank = rank;
82+
}
83+
84+
public List<String> getContacts() {
85+
return contacts;
86+
}
87+
88+
public void setContacts(List<String> contacts) {
89+
this.contacts = contacts;
90+
}
91+
92+
@Override
93+
public boolean equals(Object o) {
94+
if (this == o) return true;
95+
if (o == null || getClass() != o.getClass()) return false;
96+
Student student = (Student) o;
97+
return id == student.id && age == student.age && rank == student.rank && Objects.equals(firstName, student.firstName) && Objects.equals(gender, student.gender) && Objects.equals(dept, student.dept) && Objects.equals(city, student.city) && Objects.equals(contacts, student.contacts);
98+
}
99+
100+
@Override
101+
public int hashCode() {
102+
return Objects.hash(id, firstName, age, gender, dept, city, rank, contacts);
103+
}
104+
105+
@Override
106+
public String toString() {
107+
return "Student{" +
108+
"id=" + id +
109+
", firstName='" + firstName + '\'' +
110+
", age=" + age +
111+
", gender='" + gender + '\'' +
112+
", dept='" + dept + '\'' +
113+
", city='" + city + '\'' +
114+
", rank=" + rank +
115+
", contacts=" + contacts +
116+
'}';
117+
}
118+
}

0 commit comments

Comments
 (0)