//How many male and female employees are there in the organization?
//Print the name of all departments in the organization?
//What is the average age of male and female employees?
//Get the details of highest paid employee in the organization?
//Count the number of employees in each department?
//What is the average salary of each department?
//Get the details of youngest male employee in the product development department?
//Who has the most working experience in the organization?
//How many male and female employees are there in the sales and marketing team?
//What is the average salary of male and female employees?
//List down the names of all employees in each department?
//What is the average salary and total salary of the whole organization?
//Query 3.14 : Separate the employees who are younger or equal to 25 years from those employees who are older than 25 years.
//Find the occurance of each chars in the string
String input = "Saravanan";
Map<String, Long> map = Arrays.stream(input.split("")).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
// Find all duplicate element from a given string
List<String> list = Arrays.stream(input.split("")).collect(Collectors.groupingBy(s -> s, Collectors.counting()))
.entrySet().stream().filter(s -> s.getValue() > 1).map(Map.Entry :: getKey).collect(Collectors.toList());
// Find the unique elements from a given string
list = Arrays.stream(input.split("")).collect(Collectors.groupingBy(s -> s, Collectors.counting())).entrySet()
.stream().filter(s -> s.getValue() == 1).map(Map.Entry::getKey).collect(Collectors.toList());
// Find first non repeated elements from a given string
list = Arrays.stream(input.split("")).collect(Collectors.groupingBy(s -> s, LinkedHashMap::new, Collectors.counting()))
.entrySet().stream().filter(s -> s.getValue() == 1).map(Map.Entry::getKey).collect(Collectors.toList());
System.out.print(list);
//Find second highest number from given array
int[] numbers = {5,9,11,2,8,21,1};
int highestNo = Arrays.stream(numbers).boxed().sorted(Comparator.reverseOrder()).skip(1).findFirst().get().intValue();
// find longest string from given array
String[] array1 = { "saranasdasd", "raja", "mani", "saran1" };
String strngIs = Arrays.stream(array1)
.reduce((string1, string2) -> string1.length() > string2.length() ? string1 : string2).get();
System.out.print(strngIs);
// find all elements from array who starts with 1
int[] numbers3 = { 5, 9, 11, 2, 8, 21, 1 };
Arrays.stream(numbers).boxed().map(s -> s + "").filter(s -> s.startsWith("1")).collect(Collectors.toList())
.forEach(System.out::println);
//String join method
List<String> s = Arrays.asList("1", "2", "3", "4");
System.out.println(String.join(",", s));
//Sort a list and Map
. `**filter**`
*Question:* Fetch all numbers from a list that are greater than 5.
`**map**`
*Question:* Transform a list of strings into a list of their uppercase versions
`**flatMap**`
*Question:* Given a list of lists of strings, flatten them into a single list of strings.
`**distinct**`
*Question:* Remove duplicates from a list of integers.
`**sorted**`
*Question:* Sort a list of names in reverse alphabetical order.
`**peek**`
*Question:* Print each element in a list while converting them to uppercase.
`**limit**`
*Question:* Fetch the first 3 elements from a list of integers.
`**skip**`
*Question:* Skip the first 4 elements and fetch the remaining elements from a list of integers.
`**forEach**`
*Question:* Print each element of a list of strings with a prefix "Item: ".
`**collect**`
*Question:* Collect a list of integers into a `Set`.
`**reduce**`
*Question:* Compute the product of all numbers in a list.
`**allMatch**`
The `allMatch` method in Java Streams is used to check if all elements in the stream satisfy a given predicate. It returns `true` if every element in the stream matches the predicate, and `false` otherwise.
*Short-Circuiting:* The `allMatch` operation is short-circuiting, meaning it stops processing as soon as it finds the first element that does not match the predicate. If it finds such an element, it immediately returns `false`.
*Question:* Check if all numbers in a list are positive.
`**anyMatch**`
The `anyMatch` method checks whether *at least one element* in the stream matches a given predicate. It returns `true` as soon as it finds an element that satisfies the predicate and stops further processing. If no elements match, it returns `false`
`anyMatch` is short-circuiting, meaning it stops processing as soon as it finds the first element that matches the predicate, optimizing performance.
`**noneMatch**`
The `noneMatch` method in Java Streams is used to check if *no elements* in the stream match a given predicate. It returns `true` if none of the elements satisfy the predicate and `false` if at least one element does.
Like `allMatch` and `anyMatch`, `noneMatch` is short-circuiting. It stops processing as soon as it finds the first element that matches the predicate and immediately returns `false`
*Question:* Check if no elements in a list are negative.
`**findFirst**`
is used to retrieve the *first element* in a stream that matches a given condition or simply the first element in the stream if no filtering is applied. It returns the first element wrapped in an `Optional`, which is a container object that may or may not contain a non-null value.
No comments:
Post a Comment