Java 8 Streams Interview Questions
1. Given a list of integers, write a Java 8 Stream program to find the sum of all even numbers in the list.
List<Integer> numbers =Arrays.asList(1,2,7, 8,9,6,3);
int sum = numbers.stream().filter(n-> n%2==0).mapToInt(Integer::intValue).sum();
System.out.println(sum);Output : 162.Given a list of numbers, write a Java 8 Stream program to find the second largest number in the list.List<Integer> numbers =Arrays.asList(1,2,7, 8,9,6,3);
int secondLargestNumber = numbers.stream().sorted(Collections.reverseOrder()).skip(1).findFirst().orElse(-1);
System.out.println("second largest number is :" + secondLargestNumber);output :second largest number is :83.Given a list of integers, write a Java 8 Stream program to find the average of all the numbers in the list.List<Integer> numbers =Arrays.asList(1,2,7, 8,9,6,3);
double average = numbers.stream().mapToInt(Integer::intValue).average().orElse(0.0);
System.out.println("avergae is " + average);output:avergae is 5.1428571428571434.Given a list of integers, write a Java 8 Stream program to find the product of all numbers in the list.List<Integer> numbers =Arrays.asList(1,2,7, 8,9,6,3);
long product = numbers.stream().reduce(1, (n ,m)-> n*m);
System.out.println("the product of all number is :" + product);output:the product of all number is :181445.Given a list of strings, write a Java 8 Stream program to count the number of strings that contain the letter 'a' in the list.List<String> str = Arrays.asList("John", "Vincent", "Erwan");
long count = str.stream().filter(s -> s.contains("a")).count();
System.out.println(count);output:16. Given a list of strings, write a Java 8 Stream program to find the shortest string in the list.List<String> str = Arrays.asList("John", "Vincent", "Erwan");
String shortestString = str.stream().min(Comparator.comparing(String::length)).get();
System.out.println("shortest String is:" + shortestString);output: shortest String is:John7.Given a list of strings, write a Java 8 Stream program to find the longest string in the list.List<String> str = Arrays.asList("John", "Vincent", "Erwan");
String longestStr = str.stream().max(Comparator.comparing(String::length)).get();
System.out.println("longest String is:" + longestStr);ouput: longest String is:Vincent8. Given a list of strings, write a Java 8 Stream program to find the number of strings that have more than 5 characters.List<String> str = Arrays.asList("John", "Vincent", "Erwan");
long strGreaterThanFive = str.stream().filter( n -> n.length()>5).count();
System.out.println("string count greater than five :" + strGreaterThanFive);output: string count greater than five :19.Given a list of Person objects (each Person object has a name and age), write a Java 8 Stream program to find the oldest person in the list.public class Person {
private String name;
private int age;
public Person(String name , int age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}List<Person> persons = Arrays.asList(new Person("Emilie", 30), new Person("Benoit", 32), new Person("Alexandre", 23));
Person person = persons.stream().max(Comparator.comparingInt(Person::getAge)).get();
System.out.println(person);output : Person{name='Benoit', age=32}10. Given a list of Employee objects (each Employee object has a name, age, and salary),write a Java 8 Stream program to find the total salary of all employees over the age of 30public class Employee {
private String empId;
private String firstName;
private String lastName;
private String email;
private String gender;
private String newJoiner;
private int salary;
private int rating;
private int age;
//getter setter and constructor
}List<Employee> employees = Arrays.asList(
new Employee("59-385-1088","Zacharias","Schwerin","zchwerin@gmail.com","Male","True",101146,0, 32),
new Employee("73-274-6476","Kyle","Frudd","kfrudd1@ovh.net","Male","FALSE",29310,2, 30),
new Employee("85-939-9584","Axe","Gumb","agumb2@twitter.com","Female","FALSE",62291,4, 25),
new Employee("08-180-8292","Robinet","Batterham","rbatterham3@last.fm","Male","FALSE",142439,4, 28),
new Employee("21-825-2623","Ulick","Burrel","uburrel4@google.ru","Male","FALSE",128764,5, 40),
new Employee("66-708-5539","Tailor","Ridding","Ridding","Female","FALSE",152924,4, 22),
new Employee("81-697-2363","Joete","Braybrooke","jbraybrooke6@prnewswire.com","Male","TRUE",128907,0, 45),
new Employee("63-019-1110","Elroy","Baverstock","ebaverstock7@ehow.com","Male","TRUE",2510,0, 21)
);long totalSalary = employees.stream().filter(e -> e.getAge()>30).mapToInt(Employee::getSalary).sum();
System.out.println("total salary of emplyee whose age > 30 : " + totalSalary);output :total salary of emplyee whose age > 30 : 358817

Comments
Post a Comment