0% found this document useful (0 votes)
27 views2 pages

Java 8 Stream Operations on Lists

This document provides examples of using Java 8 streams to perform common operations on collections like lists and strings. These include finding duplicate elements, removing duplicates, counting character occurrences in a string, finding maximum/minimum values, filtering lists based on conditions, sorting lists, grouping lists, and more.

Uploaded by

prabhat singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Java 8 Stream Operations on Lists

This document provides examples of using Java 8 streams to perform common operations on collections like lists and strings. These include finding duplicate elements, removing duplicates, counting character occurrences in a string, finding maximum/minimum values, filtering lists based on conditions, sorting lists, grouping lists, and more.

Uploaded by

prabhat singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Find duplicate elements in given integer list in java using stream functions?

Integer integers[] = {10,28,87,10,20,76,28,80};


Set set = new HashSet();
Set<Integer> collect = [Link](integers).stream().filter(e->!
[Link](e)).collect([Link]());
[Link](collect);
-----------------------------------------------------------------------------------
------------------------------------
Remove duplicate elements in given integer list in java using stream functions?
Integer [] integers = {10,28,87,10,20,76,28,80};
List<Integer> result =
[Link](integers).stream().distinct().collect([Link]());
[Link](result);
-----------------------------------------------------------------------------------
-------------------------------------
Count the no of occurrence of char in given string using java8?
-----------------------------------------------------------------------------------
-------------------------------------
String str = "welcome to java and java welcome you";
Count the no of occurrence of words in given string using java8?
List<String> list = [Link]([Link](" "));
Map<String,Long> map =
[Link]().collect([Link]([Link](),[Link]
()));
-----------------------------------------------------------------------------------
------------------------------------
Given a list of integers, find the maximum and maximum value element present in it
using Stream functions?
input 10,15,8,49,25,98,98,32,15
-----------------------------------------------------------------------------------
------------------------------------
Given a String, find the first repeated character in it using Stream functions?
input "Java will always Alive"
String input = "Java will always Alive";
Character c = input
.chars()
.mapToObj(s->[Link]((char)s))
.collect([Link]([Link](),
LinkedHashMap::new,[Link]()))
.entrySet()
.stream()
.filter(e->[Link]() > 1L)
.map(e->[Link]())
.findFirst()
.get();
-----------------------------------------------------------------------------------
------------------------------------
Given a list of employees, you need to filter all the employee whose age is greater
than 20 and print the employee names.
List<String> employeeFilteredList = createEmployeeList();
[Link]().filter(e-
>[Link]()>20).map(Employee::getName).collect([Link]());
-----------------------------------------------------------------------------------
------------------------------------
Given the list of employees, count number of employees with age 25?
List<Employee> employeeList = createEmployeeList();
long count = [Link]().filter(e->[Link]()>25).count();
[Link]("Number of employees with age 25 are : "+count);
-----------------------------------------------------------------------------------
------------------------------------
Given the list of employees, find the employee with name “Mary”.
List<Employee> employeeList = createEmployeeList();
Optional<Employee> e1 = [Link]().filter(e-
>[Link]().equalsIgnoreCase("Mary")).findAny();
if([Link]())
[Link]([Link]());
-----------------------------------------------------------------------------------
------------------------------------
Given a list of employee, find maximum age of employee?
List<Employee> employeeList = createEmployeeList();
OptionalInt max = [Link]().mapToInt(Employee::getAge).max();
if([Link]())
[Link]("Maximum age of Employee: "+[Link]());
-----------------------------------------------------------------------------------
------------------------------------
Given a list of employees, sort all the employee on the basis of age?
List<Employee> employeeList = createEmployeeList();
[Link]((e1,e2)->[Link]()-[Link]());
[Link]([Link]::println);
-----------------------------------------------------------------------------------
------------------------------------
Given the list of employee, group them by employee name?
List<Employee> employeeList = createEmployeeList();
Map<String, List<Employee>> map = [Link]()
.collect([Link](Employ
ee::getName));
[Link]((name,employeeListTemp)->[Link]("Name: "+name+"
==>"+employeeListTemp));
-----------------------------------------------------------------------------------
------------------------------------
how we can achieve list of employee who have same address?

-----------------------------------------------------------------------------------
------------------------------------
filter out employee list which has id not null and salary between 1lack and 2 lack?

Common questions

Powered by AI

To identify the first repeated character in a string using Java Stream functions, convert the string into a stream of characters. Then, group these characters using groupingBy to count their occurrences, using a LinkedHashMap to maintain insertion order. Finally, filter the entries to find the first character with a count greater than one and retrieve the key of this entry as the first repeated character .

To find the maximum value in a list of integers using Java Stream functions, you use the max method with a comparator or mapToInt with max for primitives. This method is effective because it abstracts the iteration and comparison logic, providing a concise and readable way to determine the maximum value without manually writing loops and conditionals .

The distinct() function of Java Stream is used to remove duplicate elements from a list. It achieves this by filtering out duplicate elements and returning a stream with unique elements. Under the hood, it uses a Set to identify duplicates, ensuring that each element appears only once in the resulting collection .

To filter a list of employees by their address attribute using Java Stream functions, you can leverage the filter method to select employees whose addresses match a specified criterion. For example, to find employees with the same address, you could use a groupingBy to identify address-related groups and then filter those groups to isolate employees sharing similar addresses. This demonstrates advanced data querying by allowing complex conditions and interactive data manipulation .

Java Stream functions facilitate sorting a list of employees by age by providing the sorted method, which allows a Comparator to be applied directly to the stream. Sorting by age can be useful in real-world applications for organizing data display, processing salary increments based on seniority, or preparing reports based on employee tenure .

To count the occurrences of each word in a given string using Java 8 Stream functions, you can split the string into a list of words and then use stream operations to group the words by their identity, followed by counting the frequency of each word. This is done using the groupingBy collector combined with counting, resulting in a Map where keys are words and values are their counts .

Grouping a list of employees by their names using Java Stream functions involves the use of the groupingBy collector, which organizes the stream elements into a map based on the employee name. This approach provides structured data management, facilitating quick lookup, aggregation for reporting, and simplifying the handling of employees with common attributes for bulk processing .

Java Stream handles counting the number of employees with a specified age like 25 efficiently using the filter and count methods. The filter function narrows down the list to only include employees with the age of 25, after which the count method simply tallies the number of elements in the filtered stream. This process highlights how streams can efficiently process and compute large datasets without the verbosity and potential errors of traditional iterative constructs .

To find duplicate elements in a list of integers using Java Stream functions, you can use a HashSet to track elements that have been encountered. By checking whether the Set's add operation returns false, you identify elements that are duplicates because they couldn't be added to the Set again. This is accomplished using the filter method in conjunction with streams. Sets are used because they inherently prevent duplicates and provide an efficient check for existing elements .

Java Stream functions can be used to filter employees older than a specific age by using the filter method. This method selectively processes elements that meet the criteria (age > specified value), followed by mapping the employee objects to their name strings using the map method. The resulting stream of names can then be collected into a List .

You might also like