Posts
Showing posts from December, 2023
What is Intermediate and Terminal Operations ?
- Get link
- X
- Other Apps
Q. What is Intermediate and Terminal Operations ? Ans:- stream<T> will perform the following operations:- 1.Intermediate operations 2.Terminal operations 1.Intermediate operations:- the operations which are performed in b/w streams of objects are known as Intermediate operations. a. filter() b.map() c.sorted() d.distinct() e.peek() 2.Terminal operations:- the operations which are performed at the end of stream are known as Terminal operations => The following are some important Terminal operations. a. forEach() b.collect() c.reduce() d.count() e.anyMatch(),allMatch(),noneMatch() f.findAny(),findFirst() ****************************************************************
What is stream API, why is it needed?
- Get link
- X
- Other Apps
What is stream API, why is it needed? Ans:- stream is an interface from java.util.stream package which supports stream of objects,which means continuous flow of objects. Stream<T> will perform the following operations:- a. Intermediate operations b.Terminal operations => The stream API is needed for several reasons:- 1.Functional programming Paradigm:- The Stream API supports functional programming paradigm, allowing developers to express operations on data in a concise and expressive manner. 2.Parallelism:- The stream API supports parallel processing, making it easier to take advantage of multi-core processors .Parallel stream can be created to process elements concurrently. 3.Code Readability:- Using the Stream API often leads to more readable and maintainable code. 4.Efficient Operations:- Stream operations, such as map, filter, and reduce, are designed to be efficient.
Can I write Lambda expressions for any Interface ?
- Get link
- X
- Other Apps
Accenture Interview Question for 3+ Q. Can I write Lambda Expression for any Interface? Ans :- Yes, you can use lambda expressions in java for any interface that has only one abstract method. Such interfaces are known as functional interfaces, and lambda expressions provide a concise way to implement the abstract method of a function Interface And we use “@FunctionalInterface” annotation for functional Interface.
Write a program to return list of all even numbers that are greater than 5 in list.
- Get link
- X
- Other Apps
Q. Write a program to return list of all even numbers that are greater than 5 in list. SOLUTION :- package mv; import java.util.Arrays; import java.util.stream.Collectors; import java.util.*; public class GreaterFive { public static void main(String[] args) { List<Integer> list=Arrays.asList(1,2,3,4,5,6,7,8,9,10); List<Integer> greterThanFive=list.stream().filter(i->i%2==0 && i >5).collect(Collectors.toList()); System.out.println(greterThanFive); } } *******************************************************************
How to find sum of Integer using For Loop?
- Get link
- X
- Other Apps
Q. How to find sum of Integer using For Loop? SOLUTION:- package v1; import java.util.*; import java.util.Arrays; public class SumOfIntegerUsingForLoop { public static void main(String[] args) { List<Integer> list=Arrays.asList(10,20,30,40); Integer sum=0; for(Integer a:list) { sum=sum+a; } System.out.println("Sum Of Integer:"+sum); } } ************************************************************************
How to get Count of Users Starts with “A” where user have two fields Id and name
- Get link
- X
- Other Apps
Q. How to get Count of Users Starts with “A” where user have two fields Id and name SOLUTION:- User.java package v1; public class User { int id; String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public User(int id, String name) { super(); this.id = id; this.name = name; } } *********************************************************** Test.java package v1; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { List<User> list=new ArrayList<>(); list.add(new User(1, "Bharat")); list.add(new User(2, "Aditya")); list.add(new User(3, "Mohan")); list.add(new User(4, "Apurva")); list.add(new User(5, "Aditi")); list.add(new User(6, "Neha")); Long count...
How to find Sum of Odd and even number using stream.
- Get link
- X
- Other Apps
Q. How to find Sum of Odd and even number using stream. Ans :- SOLUTION:- package v1; import java.util.Arrays; import java.util.List; public class SomOfOddAndEven { public static void main(String[] args) { Integer[] number= {1,2,3,4,5,6,7,8,9,10}; List<Integer> list=Arrays.asList(number); int sumOfEven=list.stream().filter(i->i%2==0).mapToInt(i->i).sum(); System.out.println("Sum of Even numbers:"+sumOfEven); int sumOfOdd=list.stream().filter(i->i%2!=0).mapToInt(i->i).sum(); System.out.println("Sum of Odd numbers:"+sumOfOdd); } } ****************************************************************
What is the difference between List and Set ?
- Get link
- X
- Other Apps
Q. What is the difference between List and Set. Ans:- s.No List Set 1. List allow duplicates Set does’t allow duplicates 2. The List is an ordered collection which maintains the insertion order. Whereas set is an unordered collection which does not preserve the insertion order. 3. The List interface contains a single legacy class which is vector class Where as set interface does not have any legacy class 4. The List Interface can allow n number of null values 5.set Interface only allows a Single null value
What is flatMap() ? Write code for flatMap.
- Get link
- X
- Other Apps
Interview Question Q. What is flatMap() ? Write code for flatMap. Ans:- flatMap() :- flatMap() transform each element and flattern the resulting collection into a single combined collection. Example of flatMap():- package v1; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class FlatMapExample { public static void main(String[] args) { List<List<Integer>> list=new ArrayList<>(); list.add(Arrays.asList(1,2,3)); list.add(Arrays.asList(4,5,6)); list.add(Arrays.asList(7,8,9)); System.out.println(list); List<Integer> flatList=list.stream().flatMap(i->i.stream().map(j->j*2)).collect(Collectors.toList()); System.out.println("FlatList:"+flatList); } } ************************************************************