0% found this document useful (0 votes)
4 views10 pages

Java 21 Programs For Interview

The document contains Java code snippets demonstrating various operations using streams, such as filtering even numbers, finding duplicates, sorting employees by salary, and calculating averages. Each code example illustrates a specific functionality, including grouping, partitioning, and reducing collections. The document serves as a practical reference for using Java streams to manipulate and analyze data.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Java 21 Programs For Interview

The document contains Java code snippets demonstrating various operations using streams, such as filtering even numbers, finding duplicates, sorting employees by salary, and calculating averages. Each code example illustrates a specific functionality, including grouping, partitioning, and reducing collections. The document serves as a practical reference for using Java streams to manipulate and analyze data.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Even Numbers from List

import [Link].*;

public class EvenNumbers {


public static void main(String[] args) {
List<Integer> list = [Link](1,2,3,4,5,6);

List<Integer> result = [Link]()


.filter(n -> n % 2 == 0)
.toList();

[Link](result);
}
}

2. Find Duplicate Elements

import [Link].*;
import [Link].*;

public class DuplicateElements {


public static void main(String[] args) {
List<Integer> list = [Link](1,2,3,2,4,5,1);

Set<Integer> seen = new HashSet<>();

Set<Integer> duplicates = [Link]()


.filter(n -> ![Link](n))
.collect([Link]());

[Link](duplicates);
}
}

3. First Non-Repeated Character

import [Link].*;
import [Link].*;

public class FirstNonRepeatedChar {


public static void main(String[] args) {
String str = "aabbcde";

Character result = [Link]()


.mapToObj(c -> (char) c)
.collect([Link](
c -> c,
LinkedHashMap::new,
[Link]()))
.entrySet().stream()
.filter(e -> [Link]() == 1)
.map([Link]::getKey)
.findFirst()
.orElse(null);

[Link](result);
}
}

4. Sort Employees by Salary

import [Link].*;

class Employee {
String name;
double salary;

Employee(String name, double salary) {


[Link] = name;
[Link] = salary;
}
}

public class SortEmployees {


public static void main(String[] args) {
List<Employee> list = [Link](
new Employee("A", 50000),
new Employee("B", 70000),
new Employee("C", 60000)
);

[Link]()
.sorted([Link](e -> [Link]))
.forEach(e -> [Link]([Link] + " " + [Link]));
}
}

5. Highest Salary

import [Link].*;

class Employee {
String name;
double salary;
Employee(String name, double salary) {
[Link] = name;
[Link] = salary;
}
}

public class HighestSalary {


public static void main(String[] args) {
List<Employee> list = [Link](
new Employee("A", 50000),
new Employee("B", 70000),
new Employee("C", 60000)
);

Employee emp = [Link]()


.max([Link](e -> [Link]))
.orElse(null);

[Link]([Link] + " " + [Link]);


}
}

6. Group Employees by Department

import [Link].*;
import [Link].*;

class Employee {
String name;
String dept;

Employee(String name, String dept) {


[Link] = name;
[Link] = dept;
}
}

public class GroupByDept {


public static void main(String[] args) {
List<Employee> list = [Link](
new Employee("A", "IT"),
new Employee("B", "HR"),
new Employee("C", "IT")
);

Map<String, List<Employee>> map = [Link]()


.collect([Link](e -> [Link]));

[Link](map);
}
}

7. Character Count

import [Link].*;
import [Link].*;

public class CharacterCount {


public static void main(String[] args) {
String str = "hello";

Map<Character, Long> map = [Link]()


.mapToObj(c -> (char) c)
.collect([Link](
c -> c,
[Link]()));

[Link](map);
}
}

8. List to Map

import [Link].*;
import [Link].*;

public class ListToMap {


public static void main(String[] args) {
List<Integer> list = [Link](1,2,3);

Map<Integer, String> map = [Link]()


.collect([Link](
n -> n,
n -> "Number-" + n
));

[Link](map);
}
}

9. Second Highest Number


import [Link].*;

public class SecondHighest {


public static void main(String[] args) {
List<Integer> list = [Link](1,2,3,4,5);

Integer result = [Link]()


.sorted([Link]())
.skip(1)
.findFirst()
.orElse(null);

[Link](result);
}
}

10. Join Strings

import [Link].*;
import [Link].*;

public class JoinStrings {


public static void main(String[] args) {
List<String> list = [Link]("Java", "Stream", "API");

String result = [Link]()


.collect([Link](", "));

[Link](result);
}
}

11. Partition Even/Odd

import [Link].*;
import [Link].*;

public class PartitionEvenOdd {


public static void main(String[] args) {
List<Integer> list = [Link](1,2,3,4,5,6);

Map<Boolean, List<Integer>> map = [Link]()


.collect([Link](n -> n % 2 == 0));

[Link](map);
}
}

12. Top 3 Numbers

import [Link].*;

public class Top3Numbers {


public static void main(String[] args) {
List<Integer> list = [Link](5,1,9,3,7);

List<Integer> result = [Link]()


.sorted([Link]())
.limit(3)
.toList();

[Link](result);
}
}

13. Find Average Salary

import [Link].*;
import [Link].*;

class Employee {
double salary;

Employee(double salary) {
[Link] = salary;
}
}

public class AverageSalary {


public static void main(String[] args) {
List<Employee> employees = [Link](
new Employee(50000),
new Employee(70000),
new Employee(60000)
);

double avg = [Link]()


.collect([Link](e -> [Link]));

[Link](avg);
}
}

14. Employees Above Average Salary

import [Link].*;
import [Link].*;

class Employee {
double salary;

Employee(double salary) {
[Link] = salary;
}
}

public class AboveAverageSalary {


public static void main(String[] args) {
List<Employee> employees = [Link](
new Employee(50000),
new Employee(70000),
new Employee(60000)
);

double avg = [Link]()


.collect([Link](e -> [Link]));

List<Employee> result = [Link]()


.filter(e -> [Link] > avg)
.toList();

[Link]([Link]());
}
}

15. Flatten List of Lists

import [Link].*;

public class FlattenList {


public static void main(String[] args) {
List<List<Integer>> list = [Link](
[Link](1,2),
[Link](3,4)
);
List<Integer> flat = [Link]()
.flatMap(Collection::stream)
.toList();

[Link](flat);
}
}

16. Join Strings

import [Link].*;
import [Link].*;

public class JoinStrings {


public static void main(String[] args) {
List<String> list = [Link]("Java", "Stream", "API");

String result = [Link]()


.collect([Link](", "));

[Link](result);
}
}

17. Reduce (Sum)

import [Link].*;

public class ReduceSum {


public static void main(String[] args) {
List<Integer> list = [Link](1,2,3,4);

int sum = [Link]()


.reduce(0, Integer::sum);

[Link](sum);
}
}

[Link] First Element

import [Link].*;

public class FindFirst {


public static void main(String[] args) {
List<Integer> list = [Link](10,20,30);

Integer first = [Link]()


.findFirst()
.orElse(null);
[Link](first);
}
}

19. Sort Map by Value

import [Link].*;

public class SortMap {


public static void main(String[] args) {
Map<String, Integer> map = [Link]("A", 3, "B", 1, "C", 2);

[Link]().stream()
.sorted([Link]())
.forEach([Link]::println);
}
}

20. Find Longest String

import [Link].*;

public class LongestString {


public static void main(String[] args) {
List<String> list = [Link]("Java", "Stream", "API");

String longest = [Link]()


.max([Link](String::length))
.orElse("");

[Link](longest);
}
}

21. Find First Element

import [Link].*;

public class FindFirst {


public static void main(String[] args) {
List<Integer> list = [Link](10,20,30);

Integer first = [Link]()


.findFirst()
.orElse(null);

[Link](first);
}
}

You might also like