In This tutorial, I will share the top 10 java programming interview questions with answers.
1. Write a program to find duplicate elements in a given integers list in java using Stream API.
Ans: public class DuplicateElements {
public static void main(String args[]) { List<Integer> myList = Arrays.asList(21,18,12,54,21,17,15,12,76); Set<Integer> set = new HashSet(); myList.stream() .filter(n -> !set.add(n)) .forEach(System.out::println); } }
Output:
21, 12
2. Write a program to find the Maximum number of a Stream?
Ans:
import java.util.Comparator;
import java.util.stream.Collectors;
public class MaxNumber {
public static void main(String[] args) {
Integer max = Stream.of(1, 2, 3, 4, 5, 6,7) |
.max(Comparator.comparing(Integer::valueOf)) |
.get(); |
System.out.println("The Maximum number is: " + max); |
}
Output: The Maximum number is: 7 3. Write a program to find all the employees whose age is greater than 30 and print employee name. |
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class EmployeeMain {
public static void main(String[] args) {
List<Employee> employeeList = getListOfEmployees();
employeeList.stream()
.filter(e -> e.getEmpAge() > 30)
.map(Employee::getEmpName)
.forEach(System.out::println);
}
private static List<Employee> getListOfEmployees() {
List<Employee> employeeList = new ArrayList<>();
Employee employee1 = new Employee("Ram",24, 10000.00, Arrays.asList("K.S.Layout","Bangalore"));
Employee employee2 = new Employee("Shyam",30, 25000.00,Arrays.asList("Delhi","Mysore"));
Employee employee3 = new Employee("Pratik",22, 21000.00, Arrays.asList("Patna","Hyderabad"));
Employee employee4 = new Employee("Geeta",26, 11000.00, Arrays.asList("Lakhnow","U.P"));
Employee employee5 = new Employee("Priya",34, 51000.00, Arrays.asList("Bidar","Pune"));
Employee employee6 = new Employee("Ram",18, 5000.00, Arrays.asList("Bidar","Pune"));
employeeList.add(employee1);
employeeList.add(employee2);
employeeList.add(employee3);
employeeList.add(employee4);
employeeList.add(employee5);
employeeList.add(employee6);
return employeeList;
}
}
Output:
Priya
4. Write a program to join the all employee names with ",".
String empNames = employeeList.stream()
.map(Employee::getEmpName)
.collect(Collectors.joining(","));
System.out.println(empNames);
Output:
Ram,Shyam,Pratik,Geeta,Priya,Ram
5. Write a program for group all employee by name.
Map<String, List<Employee>> empGroup = employeeList.stream()
.collect(
Collectors
.groupingBy(
Employee::getEmpName));
empGroup.forEach(
(name, empGroupDetail) -> System.out.println
("Name: "+ name +"--->"+empGroupDetail));
Output:
Name: Shyam--->[Employee [empName=Shyam, empAge=30, salary=25000.0, listOfCities=[Delhi, Mysore]]]
Name: Priya--->[Employee [empName=Priya, empAge=34, salary=51000.0, listOfCities=[Bidar, Pune]]]
Name: Geeta--->[Employee [empName=Geeta, empAge=26, salary=11000.0, listOfCities=[Lakhnow, U.P]]]
Name: Pratik--->[Employee [empName=Pratik, empAge=22, salary=21000.0, listOfCities=[Patna, Hyderabad]]]
Name: Ram--->[Employee [empName=Ram, empAge=24, salary=10000.0, listOfCities=[K.S.Layout, Bangalore]], Employee [empName=Ram, empAge=18, salary=5000.0, listOfCities=[Bidar, Pune]]]
6. Write a program to sort employee list by age.
employeeList.stream()
.sorted(Comparator.comparing(Employee::getEmpAge))
.collect(Collectors.toList())
.forEach(System.out::println);
Output:
Employee [empName=Ram, empAge=18, salary=5000.0, listOfCities=[Bidar, Pune]]
Employee [empName=Pratik, empAge=22, salary=21000.0, listOfCities=[Patna, Hyderabad]]
Employee [empName=Ram, empAge=24, salary=10000.0, listOfCities=[K.S.Layout, Bangalore]]
Employee [empName=Geeta, empAge=26, salary=11000.0, listOfCities=[Lakhnow, U.P]]
Employee [empName=Shyam, empAge=30, salary=25000.0, listOfCities=[Delhi, Mysore]]
Employee [empName=Priya, empAge=34, salary=51000.0, listOfCities=[Bidar, Pune]]
7. Write a program to find highest age of the employee.
OptionalInt max = employeeList.stream()
.mapToInt(Employee::getEmpAge)
.max();
if(max.isPresent()) {
System.out.println("max age of the employee:: "+ max.getAsInt());
}
Output:
max age of the employee:: 34
8. Write a program to find the name whose name is Pratik.
employeeList.stream()
.filter(e-> e.getEmpName().equals("Pratik"))
.collect(Collectors.toList())
.forEach(System.out::println);
// other way
Optional<Employee> empName = employeeList.stream()
.filter(e->
e.getEmpName()
.equalsIgnoreCase("Pratik"))
.findAny();
if(empNameWithRam.isPresent()) {
System.out.println("Other way name is Pratik:: "+empName.get());
Output:
Employee [empName=Pratik, empAge=22, salary=21000.0, listOfCities=[Patna, Hyderabad]]
Other way name is Pratik:: Employee [empName=Pratik, empAge=22, salary=21000.0, listOfCities=[Patna, Hyderabad]]
9. Write a program to count duplicate character.
public class CountDuplicateChars {
public static void main(String[] args) {
// given input string
String input = "JavaJAVAEE";
// convert string into stream
Map < Character, Long > result = input
.chars().mapToObj(c -> (char) c)
.collect(Collectors.groupingBy(c -> c, Collectors.counting()));
result.forEach((k, v) -> {
if (v > 1) {
System.out.print(k + " : " + v);
}
});
//ignore case
Map < Character, Long > ignoreCase = input
.chars().mapToObj(c -> (char) c)
.collect(Collectors.groupingBy(c -> c.toUpperCase(c) , Collectors.counting()));
System.out.println();
ignoreCase.forEach((k, v) -> {
if (v > 1) {
System.out.print(k + " : " + v);
}
});
}
Output:
A : 2a : 2E : 2J : 2
A : 4E : 2V : 2J : 2
10. Write a program to get the sum of all numbers present in a list.
public class SumOfAllNumber {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7);
int sumList = list.stream()
.mapToInt(Integer::intValue)
.sum();
System.out.println("sum of all numbers present in a list : "+sumList);
Output:
28