-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExample.java
More file actions
61 lines (47 loc) · 1.34 KB
/
Copy pathExample.java
File metadata and controls
61 lines (47 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
interface Printable {
void print();
}
abstract class Shape implements Printable {
protected String name;
protected double area;
public Shape(String name) {
this.name = name;
}
abstract double calculateArea();
}
class Circle extends Shape {
private double radius;
public Circle(String name, double radius) {
super(name);
this.radius = radius;
}
@Override
double calculateArea() {
this.area = Math.PI * radius * radius;
return this.area;
}
@Override
public void print() {
System.out.printf("%s - Area: %.2f%n", name, calculateArea());
}
}
public class Example {
private static List<Shape> shapes = new ArrayList<>();
public static void main(String[] args) {
// Lambda expression
Runnable task = () -> System.out.println("Hello from lambda!");
task.run();
// Stream and Optional usage
shapes.add(new Circle("Small Circle", 2.0));
shapes.add(new Circle("Large Circle", 5.0));
Optional<Shape> firstShape = shapes.stream()
.filter(s -> s.calculateArea() > 50)
.findFirst();
firstShape.ifPresent(Shape::print);
// Method reference
shapes.forEach(Shape::print);
}
}