Java8_CodingProgramms
Java8_CodingProgramms
Output:
98, 15
O/P
Character occurrences in the string:
a: 2
r: 1
s: 1
g: 1
h: 2
i: 1
m: 1
n: 2
o: 1
O/P
Duplicate names in the list:
Alice: 2
John: 2
After swapping:
str1: World
str2: Hello
5 Java Program to Swap Two numbers Without Using Third
Variable
System.out.println("Before swapping:");
System.out.println("num1: " + num1);
System.out.println("num2: " + num2);
System.out.println("\nAfter swapping:");
System.out.println("num1: " + num1);
System.out.println("num2: " + num2);
}
}
O/P
Before swapping:
num1: 5
num2: 10
After swapping:
num1: 10
num2: 5
6 Given the list of integers, find the first element of the list using
Stream functions?
Output:
9
Output:
98
O/P
a
myList.stream()
.sorted(Collections.reverseOrder())
.forEach(System.out::println);
}
}
Output:
98
98
49
32
25
15
15
10
8
myList.stream()
.sorted()
.forEach(System.out::println);
}
}
Output:
8
10
15
15
25
32
49
98
98
List<String> uniqueStrngs =
listOfStrings.stream().distinct().collect(Collectors.toList());
System.out.println(uniqueStrngs);
}
}
O/P
int max =
listOfIntegers.stream().max(Comparator.naturalOrder()).get();
int min =
listOfIntegers.stream().min(Comparator.naturalOrder()).get();
Output :
Maximum Element : 89
Minimum Element : 12
Integer secondLargestNumber =
listOfIntegers.stream().sorted(Comparator.reverseOrder()).skip(1).findFirst
().get();
System.out.println(secondLargestNumber);
}
}
O/P
75
// Integer secondLargestNumber =
listOfIntegers.stream().sorted(Comparator.reverseOrder()).skip(1).findFirst
().get();
Integer thirdLargestNumber =
listOfIntegers.stream().sorted(Comparator.reverseOrder()).skip(2).findFirst
().get();
System.out.println(thirdLargestNumber);
}
}
O/P
56
17 Find maximum element of ArrayList with Java
O/P
Current Local Date: 2023-12-24
Current Local Time: 00:03:31.476149400
Current Local Date and Time: 2023-12-24T00:03:31.476149400
Integer lastElement =
listOfStrings.stream().skip(listOfStrings.size()-1 ).findFirst().get();
System.out.println(lastElement);
}
}
*/
String lastElement =
listOfStrings.stream().skip(listOfStrings.size() - 1).findFirst().get();
System.out.println(lastElement);
}
}
O/P
Six
/* ------------------start---------------
Integer secondElement = getSecondElement(arrayList);
--------------end---------------------------
*/
/* ------------------start---------------
Integer lastElement = getLastElement(arrayList);
--------------end---------------------------
*/
O/P
Common elements: [4, 5]
List<String>list=Arrays.asList("sing","manohar","aruna","anil");
list=list.stream().filter(e>e.startsWith("a")).collect(Collectors.toList())
;
for(String list1:list) {
System.out.println(list1);
}
}
}
O/P
aruna
anil
maxSalaryByDepartment.forEach((department,
maxSalary) -> System.out.println("Max salary in department " + department
+ ": " + maxSalary));
}
Collectors.mapping(Employee::getSalary,
Collectors.maxBy(Double::compare))))
.entrySet()
.stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry ->
entry.getValue().orElseThrow(() -> new IllegalStateException("No max
value"))
));
}
}
O/P
Max salary in department Finance: 75000.0
Max salary in department HR: 55000.0
Max salary in department IT: 72000.0
O/P
Employee{name='Bob', department='IT', salary=6000.0}
Employee{name='David', department='IT', salary=8000.0}
Employee{name='Alice', department='HR', salary=5000.0}
Employee{name='Charlie', department='HR', salary=7000.0}
Employee{name='Eva', department='Finance', salary=9000.0}
List<Integer> decimalList =
Arrays.asList(1,2,3,4,4,5,6,7,8);
decimalList.stream().sorted(Comparator.reverseOrder()).forEach(System.out::
print);
}
}
O/P
8
7
6
5
4
4
3
2
1
26 What does the filter() method do? when you use it?
The filter method is used to filter elements that satisfy a certain condition
that is specified using a Predicate function.
Ex:
employeeList.stream()
.map(Employee::getDepartment)
.distinct()
.forEach(System.out::println);
O/P
HR
Sales And Marketing
Infrastructure
Product Development
Security And Transport
Account And Finance
28 How many male and female employees are there in the organization
employeeList.stream().collect(Collectors.groupingBy(Employee::getGender,
Collectors.counting()));
System.out.println(noOfMaleAndFemaleEmployees);
O/P
{Male=11, Female=6}
employeeList.stream().collect(Collectors.groupingBy(Employee::getGender,
Collectors.averagingInt(Employee::getAge)));
System.out.println(avgAgeOfMaleAndFemaleEmployees);
O/P
{Male=30.181818181818183, Female=27.166666666666668}
Optional<Employee> highestPaidEmployeeWrapper=
employeeList.stream().collect(Collectors.maxBy(Comparator.comparingDouble(E
mployee::getSalary)));
System.out.println("==================================");
System.out.println("ID :
"+highestPaidEmployee.getId());
System.out.println("Name :
"+highestPaidEmployee.getName());
System.out.println("Age :
"+highestPaidEmployee.getAge());
System.out.println("Gender :
"+highestPaidEmployee.getGender());
System.out.println("Department :
"+highestPaidEmployee.getDepartment());
System.out.println("Year Of Joining :
"+highestPaidEmployee.getYearOfJoining());
System.out.println("Salary :
"+highestPaidEmployee.getSalary());
Output :
31 Get the names of all employees who have joined after 2015?
employeeList.stream()
.filter(e -> e.getYearOfJoining() > 2015)
.map(Employee::getName)
.forEach(System.out::println);
Output :
Iqbal Hussain
Amelia Zoe
Nitin Joshi
Nicolus Den
Ali Baig
O/P
--------------------
Number of employees in Product Development: 5
Number of employees in Security And Transport: 2
Number of employees in Sales And Marketing: 3
Number of employees in Infrastructure: 3
Number of employees in HR: 2
Number of employees in Account And Finance: 2
33 List down the names of all employees in each department?
employeeList.stream().collect(Collectors.groupingBy(Employee::getDepartment
));
O/P
--------------------
--------------------------------------
Employees In Product Development :
--------------------------------------
Murali Gowda
Wang Liu
Nitin Joshi
Sanvi Pandey
Anuj Chettiar
--------------------------------------
Employees In Security And Transport :
--------------------------------------
Iqbal Hussain
Jaden Dough
--------------------------------------
Employees In Sales And Marketing :
--------------------------------------
Paul Niksui
Amelia Zoe
Nicolus Den
--------------------------------------
Employees In Infrastructure :
--------------------------------------
Martin Theron
Jasna Kaur
Ali Baig
--------------------------------------
Employees In HR :
--------------------------------------
Jiya Brein
Nima Roy
--------------------------------------
Employees In Account And Finance :
--------------------------------------
Manu Sharma
Jyothi Reddy
numbers.stream().map(x-> x*2).forEach(System.out::println);
}
}
O/P
2
4
6
8
10
numbers.stream().filter(x-> x>2).forEach(System.out::println);
}
}
O/P
3
4
5
List<Integer>
list1=numbers.stream().sorted().collect(Collectors.toList());
System.out.println(list1);
O/P
[1, 2, 3, 4, 5]
numbers.stream().sorted(Comparator.reverseOrder()).forEach(System.out::prin
tln);
//System.out.println(list1);
}
}
O/P
5
4
3
2
1
long number=numbers.stream().count();
System.out.println(number);
}
}
O/P
5
employeeList.stream().map(Employee::getDepartment).di
stinct().forEach(System.out::println);
40 Find the each employee count in all departments in java8
Map< String,Long>
map=employeeList.stream().collect(Collectors.groupingBy(Employee::getDepart
ment,Collectors.counting()));
System.out.println(map);
O/P
{Product Development=5, Security And Transport=2, Sales And Marketing=3,
Infrastructure=3, HR=2, Account And Finance=2}
O/P
GROUP BY:
Usage: The GROUP BY clause is used to group rows that
have the same values in specified columns into
summary rows.
Function: It is often used in combination with
aggregate functions (such as COUNT, SUM, AVG, etc.)
to perform operations on each group of rows.
EX:
SELECT department, COUNT(*) as employee_count
FROM employees
GROUP BY department;
ORDER BY:
Usage: The ORDER BY clause is used to sort the result
set based on one or more columns.
Function: It sorts the result set either in ascending
(default) or descending order for each specified
column.
EX:
HAVING:
Usage: The HAVING clause is used to filter the
results of a GROUP BY clause based on specified
conditions.
Function: It is similar to the WHERE clause but is
applied after the GROUP BY and aggregate functions,
allowing you to filter on aggregated values.
EX:
In summary: