Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
53 views

Java 8

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
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Java 8

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
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

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 = Arrays.asList(integers).stream().filter(e->!
set.add(e)).collect(Collectors.toSet());
System.out.println(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 =
Arrays.asList(integers).stream().distinct().collect(Collectors.toList());
System.out.println(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 = Arrays.asList(str.split(" "));
Map<String,Long> map =
list.stream().collect(Collectors.groupingBy(Function.identity(),Collectors.counting
()));
-----------------------------------------------------------------------------------
------------------------------------
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->Character.valueOf((char)s))
.collect(Collectors.groupingBy(Function.identity(),
LinkedHashMap::new,Collectors.counting()))
.entrySet()
.stream()
.filter(e->e.getValue() > 1L)
.map(e->e.getKey())
.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();
employeeList.stream().filter(e-
>e.getAge()>20).map(Employee::getName).collect(Collectors.toList());
-----------------------------------------------------------------------------------
------------------------------------
Given the list of employees, count number of employees with age 25?
List<Employee> employeeList = createEmployeeList();
long count = employeeList.stream().filter(e->e.getAge()>25).count();
System.out.println("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 = employeeList.stream().filter(e-
>e.getName().equalsIgnoreCase("Mary")).findAny();
if(e1.isPresent())
System.out.println(e1.get());
-----------------------------------------------------------------------------------
------------------------------------
Given a list of employee, find maximum age of employee?
List<Employee> employeeList = createEmployeeList();
OptionalInt max = employeeList.stream().mapToInt(Employee::getAge).max();
if(max.isPresent())
System.out.println("Maximum age of Employee: "+max.getAsInt());
-----------------------------------------------------------------------------------
------------------------------------
Given a list of employees, sort all the employee on the basis of age?
List<Employee> employeeList = createEmployeeList();
employeeList.sort((e1,e2)->e1.getAge()-e2.getAge());
employeeList.forEach(System.out::println);
-----------------------------------------------------------------------------------
------------------------------------
Given the list of employee, group them by employee name?
List<Employee> employeeList = createEmployeeList();
Map<String, List<Employee>> map = employeeList.stream()
.collect(Collectors.groupingBy(Employ
ee::getName));
map.forEach((name,employeeListTemp)->System.out.println("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?

You might also like