Skip to content

Commit caebf86

Browse files
committed
add Stream related solutions
1 parent 63b6553 commit caebf86

File tree

8 files changed

+152
-1
lines changed

8 files changed

+152
-1
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package CreatingStreams;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
import java.util.stream.Stream;
7+
8+
public class CreatingStreams {
9+
public static void main(String[] args) {
10+
// A stream consists of a source followed by zero or more intermediate methods combined together(pipelined) and
11+
// a terminal method to process the objects obtained from the source as per the methods described.
12+
// Stream is used to compute elements as per the pipelined methods without altering the original value of the object.
13+
14+
Stream<Integer> stream = Stream.of(1,2,3,4,5); // Stream using Stream.of()
15+
stream.forEach(System.out::println);
16+
17+
int []arr = {2,4,8,16};
18+
int sum = Arrays.stream(arr).sum(); // Stream from array
19+
System.out.println(sum);
20+
21+
List<Integer> numbers = Arrays.asList(1,3,5,7);
22+
numbers.stream().map(i -> i+1).collect(Collectors.toList()).forEach(System.out::println); // Stream from collection
23+
}
24+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ExceptionHandlingBestPractices;
2+
3+
public class ListOfBestPractices {
4+
// Use specific exception classes for different types of errors
5+
// Catch exceptions at the appropriate level of abstraction
6+
// Log and handle exceptions in a consistent and informative manner
7+
// Avoid empty catch blocks and swallowing exceptions
8+
// Propagate exceptions up the call stack when appropriate
9+
// Use finally blocks for cleanup and resource management
10+
// Use a global exception handler
11+
// Don’t close resources manually
12+
}

More Core Java Concepts to Know/FunctionalInterfacesInJDK/FuncInterfacesDemo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ public static void main(String[] args) {
2929
System.out.println(supplier.get());
3030

3131
}
32-
}
32+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package InfiniteStreams;
2+
3+
import java.util.Random;
4+
import java.util.stream.IntStream;
5+
6+
public class InfiniteStreams {
7+
public static void main(String[] args) {
8+
IntStream
9+
.iterate(0, i -> i+1)
10+
.forEach(System.out::println);
11+
12+
Random random = new Random();
13+
IntStream.generate(random::nextInt).forEach(System.out::println);
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package StreamAPIForFilteringAndTransformation;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
public class FilteringAndTransformation {
8+
public static void main(String[] args) {
9+
List<Integer> numbers = new ArrayList<>();
10+
numbers.add(1);
11+
numbers.add(2);
12+
numbers.add(3);
13+
numbers.add(4);
14+
numbers.add(5);
15+
16+
List evenNumbers = numbers.stream().filter(e -> e % 2 == 0).map(e -> e*e).collect(Collectors.toList());
17+
System.out.println(evenNumbers);
18+
}
19+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package StreamAPIForGroupingAndAggregation;
2+
3+
public class Employee {
4+
5+
private String name;
6+
private String department;
7+
private double salary;
8+
9+
public Employee(String name, String department, double salary) {
10+
this.name = name;
11+
this.department = department;
12+
this.salary = salary;
13+
}
14+
15+
public String getDepartment() {
16+
return department;
17+
}
18+
19+
public double getSalary() {
20+
return salary;
21+
}
22+
23+
@Override
24+
public String toString() {
25+
return name;
26+
}
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package StreamAPIForGroupingAndAggregation;
2+
3+
import java.util.ArrayList;
4+
import java.util.DoubleSummaryStatistics;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
public class GroupingAndAggregation {
9+
public static void main(String[] args) {
10+
List<Employee> employees = new ArrayList<>();
11+
employees.add(new Employee("Imam", "Technical", 10000));
12+
employees.add(new Employee("Biplob", "HR", 9000));
13+
employees.add(new Employee("Rakib", "Technical", 8000));
14+
15+
var groupedEmployees = employees.stream()
16+
.collect(Collectors.groupingBy(Employee::getDepartment,
17+
Collectors.averagingDouble(Employee::getSalary)));
18+
System.out.println(groupedEmployees); // Average salary for all departments
19+
20+
DoubleSummaryStatistics summary = employees.stream().mapToDouble(Employee::getSalary).summaryStatistics();
21+
System.out.println(summary.getAverage()); // Average salary for all employees
22+
}
23+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package StreamIntermediateAndTerminalOps;
2+
3+
import StreamAPIForGroupingAndAggregation.Employee;
4+
5+
import java.util.Arrays;
6+
import java.util.Comparator;
7+
import java.util.List;
8+
import java.util.stream.Collectors;
9+
10+
public class StreamOps {
11+
public static void main(String[] args) {
12+
List<Integer> numbers = Arrays.asList(64,1,2,4,8,16,32);
13+
int sumOfFilteredNumbers = numbers.stream()
14+
.filter(n -> n%4==0)
15+
.map(n -> n*n)
16+
.reduce(0,(sum,i) -> sum+i);
17+
System.out.println(sumOfFilteredNumbers);
18+
19+
numbers.stream()
20+
.sorted()
21+
.collect(Collectors.toList())
22+
.forEach(System.out::println);
23+
24+
List<Employee> employees = List.of(new Employee("Imam", "Technical", 10000),
25+
new Employee("Biplob", "HR", 9000),
26+
new Employee("Rakib", "Technical", 8000));
27+
employees.stream()
28+
.sorted(Comparator.comparing(Employee::getSalary))
29+
.forEach(System.out::println);
30+
}
31+
}

0 commit comments

Comments
 (0)