Top 10 Java 8 Stream and Lambda Expression Interview Questions Answers -2022

ArrayList<Integer> arr=new ArrayList<>(Arrays.asList(12,44,76,11,8,9,9));List<Integer> result=  arr.stream().distinct().filter(n->n<20).collect(Collectors.toList());result.forEach(s->System.out.println(s));
String str = "Better Butter";String ch=String.valueOf(str.replace(" ",""));Map<String,Long> map=Arrays.stream(ch.split("")).collect(Collectors.groupingBy(c->c,Collectors.counting()));map.forEach((k,v)->System.out.println(k+"  "+v));
ArrayList<Integer> arr=new ArrayList<>(Arrays.asList(12,44,76,11,15,99,19));
int n=3;
int highest = arr.stream().sorted(Collections.reverseOrder()).collect(Collectors.toList()).get(n-1);System.out.println("nth highest number from array : "+highest);
String str="hi guys welcome to The team";Arrays.stream(str.toUpperCase().split(" ")).filter(e->e.startsWith("T")).forEach(s->System.out.println(" Start With T Word :"+s));
//We have employee class with variable Name,Age,Salary,Department,DOJ
List<Employe> emp=new ArrayList<>();
emp.add(new Employe("Jay",24 , 25000,"IT",new Date(2009,01,22)));emp.add(new Employe("Rehman",45 , 55000,"Testing",new Date(2021,01,02)));emp.add(new Employe("Rajan",34 , 120000,"Prodcution",new Date(2012,11,12)));emp.add(new Employe("Sagar",46 , 320000,"Testing",new Date(2016,03,02)));emp.add(new Employe("Abhijit",26 , 50000,"IT",new Date(2010,06,23)));
Map<String, Long> result=emp.stream().collect(Collectors.groupingBy(Employe::getDept,
Collectors.counting()));
result.forEach((k,v)->{System.out.println(k+" "+v);});
emp.stream().filter(e->e.getDoj().getYear()>2010).forEach(
e->System.out.println(e.getName()));
long count=emp.stream().filter(e->e.getAge()>30).count();System.out.println(count);
emp.stream().sorted(Comparator.comparingInt(Employee::getSalary).reversed()).collect(Collectors.toList()).forEach(e->System.out.println(e.getName()+":"+e.getSalary()));;
emp.stream().sorted(Comparator.comparing(Employe::getAge)
.thenComparing(Employe::getDept)).collect(Collectors.toList())
.forEach(s->System.out.println(s.getAge()+”::”+s.getDept()));
String str="Java Is Good For APP Devleopment";String s=str.replace(" " , "");List<String> vowel=Arrays.asList("a","i","o","u","e");Arrays.stream(s.toLowerCase().split("")).filter(vowel::contains) .forEach(System.out::println );
emp.stream().sorted(Comparator.comparing(Employe::getSalary)
.reversed()).limit(3).forEach(s>System.out.println(s.getName()"::"+s.getSalary()));

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jay Patel

Jay Patel

I am Java/J2EE Developer , Working with well known IT industry .