Skip to content

Commit afc5ab0

Browse files
committed
core-java-lambdas 项目添加
1 parent 0d46c06 commit afc5ab0

29 files changed

Lines changed: 1364 additions & 0 deletions
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## Relevant articles:
2+
3+
- [Why Do Local Variables Used in Lambdas Have to Be Final or Effectively Final?](https://www.baeldung.com/java-lambda-effectively-final-local-variables)
4+
- [Java 8 – Powerful Comparison with Lambdas](http://www.baeldung.com/java-8-sort-lambda)
5+
- [Functional Interfaces in Java 8](http://www.baeldung.com/java-8-functional-interfaces)
6+
- [Lambda Expressions and Functional Interfaces: Tips and Best Practices](http://www.baeldung.com/java-8-lambda-expressions-tips)
7+
- [Exceptions in Java 8 Lambda Expressions](http://www.baeldung.com/java-lambda-exceptions)
8+
- [Method References in Java](https://www.baeldung.com/java-method-references)
9+
- [The Double Colon Operator in Java 8](https://www.baeldung.com/java-8-double-colon-operator)
10+
- [Serialize a Lambda in Java](https://www.baeldung.com/java-serialize-lambda)
11+
- [Convert Anonymous Class into Lambda in Java](https://www.baeldung.com/java-from-anonymous-class-to-lambda)
12+
- [When to Use Callable and Supplier in Java](https://www.baeldung.com/java-callable-vs-supplier)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>core-java-lambdas</artifactId>
7+
<packaging>jar</packaging>
8+
<name>core-java-lambdas</name>
9+
10+
<parent>
11+
<groupId>com.ossez.core-java-modules</groupId>
12+
<artifactId>core-java-modules</artifactId>
13+
<version>0.0.2-SNAPSHOT</version>
14+
<relativePath>../pom.xml</relativePath>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.apache.commons</groupId>
20+
<artifactId>commons-lang3</artifactId>
21+
<version>${commons-lang3.version}</version>
22+
</dependency>
23+
</dependencies>
24+
25+
</project>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.baeldung.doublecolon;
2+
3+
public class Computer {
4+
5+
private Integer age;
6+
private String color;
7+
private Integer healty;
8+
9+
Computer(final int age, final String color) {
10+
this.age = age;
11+
this.color = color;
12+
}
13+
14+
Computer(final Integer age, final String color, final Integer healty) {
15+
this.age = age;
16+
this.color = color;
17+
this.healty = healty;
18+
}
19+
20+
public Computer() {
21+
}
22+
23+
public Integer getAge() {
24+
return age;
25+
}
26+
27+
public void setAge(final Integer age) {
28+
this.age = age;
29+
}
30+
31+
String getColor() {
32+
return color;
33+
}
34+
35+
public void setColor(final String color) {
36+
this.color = color;
37+
}
38+
39+
Integer getHealty() {
40+
return healty;
41+
}
42+
43+
void setHealty(final Integer healty) {
44+
this.healty = healty;
45+
}
46+
47+
public void turnOnPc() {
48+
System.out.println("Computer turned on");
49+
}
50+
51+
public void turnOffPc() {
52+
System.out.println("Computer turned off");
53+
}
54+
55+
public Double calculateValue(Double initialValue) {
56+
return initialValue / 1.50;
57+
}
58+
59+
@Override
60+
public String toString() {
61+
return "Computer{" + "age=" + age + ", color='" + color + '\'' + ", healty=" + healty + '}';
62+
}
63+
64+
@Override
65+
public boolean equals(final Object o) {
66+
if (this == o) {
67+
return true;
68+
}
69+
if (o == null || getClass() != o.getClass()) {
70+
return false;
71+
}
72+
73+
final Computer computer = (Computer) o;
74+
75+
return (age != null ? age.equals(computer.age) : computer.age == null) && (color != null ? color.equals(computer.color) : computer.color == null);
76+
77+
}
78+
79+
@Override
80+
public int hashCode() {
81+
int result = age != null ? age.hashCode() : 0;
82+
result = 31 * result + (color != null ? color.hashCode() : 0);
83+
return result;
84+
}
85+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.doublecolon;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import com.baeldung.doublecolon.function.ComputerPredicate;
7+
8+
public class ComputerUtils {
9+
10+
static final ComputerPredicate after2010Predicate = (c) -> (c.getAge() > 2010);
11+
static final ComputerPredicate blackPredicate = (c) -> "black".equals(c.getColor());
12+
13+
public static List<Computer> filter(final List<Computer> inventory, final ComputerPredicate p) {
14+
15+
final List<Computer> result = new ArrayList<>();
16+
inventory.stream().filter(p::filter).forEach(result::add);
17+
18+
return result;
19+
}
20+
21+
static void repair(final Computer computer) {
22+
if (computer.getHealty() < 50) {
23+
computer.setHealty(100);
24+
}
25+
}
26+
27+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.doublecolon;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.util.function.Function;
7+
8+
public class MacbookPro extends Computer {
9+
10+
private static final Logger LOG = LoggerFactory.getLogger(MacbookPro.class);
11+
12+
public MacbookPro(int age, String color) {
13+
super(age, color);
14+
}
15+
16+
MacbookPro(Integer age, String color, Integer healty) {
17+
super(age, color, healty);
18+
}
19+
20+
@Override
21+
public void turnOnPc() {
22+
LOG.debug("MacbookPro turned on");
23+
}
24+
25+
@Override
26+
public void turnOffPc() {
27+
LOG.debug("MacbookPro turned off");
28+
}
29+
30+
@Override
31+
public Double calculateValue(Double initialValue) {
32+
33+
Function<Double, Double> function = super::calculateValue;
34+
final Double pcValue = function.apply(initialValue);
35+
LOG.debug("First value is:" + pcValue);
36+
return pcValue + (initialValue / 10);
37+
38+
}
39+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.doublecolon.function;
2+
3+
import com.baeldung.doublecolon.Computer;
4+
5+
@FunctionalInterface
6+
public interface ComputerPredicate {
7+
8+
boolean filter(Computer c);
9+
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.doublecolon.function;
2+
3+
import java.util.Objects;
4+
import java.util.function.Function;
5+
6+
@FunctionalInterface
7+
public interface TriFunction<A, B, C, R> {
8+
9+
R apply(A a, B b, C c);
10+
11+
default <V> TriFunction<A, B, C, V> andThen(final Function<? super R, ? extends V> after) {
12+
Objects.requireNonNull(after);
13+
return (final A a, final B b, final C c) -> after.apply(apply(a, b, c));
14+
}
15+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.baeldung.java8.entity;
2+
3+
public class Human {
4+
private String name;
5+
private int age;
6+
7+
public Human() {
8+
super();
9+
}
10+
11+
public Human(final String name, final int age) {
12+
super();
13+
14+
this.name = name;
15+
this.age = age;
16+
}
17+
18+
// API
19+
20+
public String getName() {
21+
return name;
22+
}
23+
24+
public void setName(final String name) {
25+
this.name = name;
26+
}
27+
28+
public int getAge() {
29+
return age;
30+
}
31+
32+
public void setAge(final int age) {
33+
this.age = age;
34+
}
35+
36+
// compare
37+
38+
public static int compareByNameThenAge(final Human lhs, final Human rhs) {
39+
if (lhs.name.equals(rhs.name)) {
40+
return Integer.compare(lhs.age, rhs.age);
41+
} else {
42+
return lhs.name.compareTo(rhs.name);
43+
}
44+
}
45+
46+
//
47+
48+
@Override
49+
public int hashCode() {
50+
final int prime = 31;
51+
int result = 1;
52+
result = prime * result + age;
53+
result = prime * result + ((name == null) ? 0 : name.hashCode());
54+
return result;
55+
}
56+
57+
@Override
58+
public boolean equals(final Object obj) {
59+
if (this == obj) {
60+
return true;
61+
}
62+
if (obj == null) {
63+
return false;
64+
}
65+
if (getClass() != obj.getClass()) {
66+
return false;
67+
}
68+
final Human other = (Human) obj;
69+
if (age != other.age) {
70+
return false;
71+
}
72+
if (name == null) {
73+
if (other.name != null) {
74+
return false;
75+
}
76+
} else if (!name.equals(other.name)) {
77+
return false;
78+
}
79+
return true;
80+
}
81+
82+
@Override
83+
public String toString() {
84+
final StringBuilder builder = new StringBuilder();
85+
builder.append("Human [name=").append(name).append(", age=").append(age).append("]");
86+
return builder.toString();
87+
}
88+
89+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.java8.lambda.exceptions;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.util.function.Consumer;
7+
8+
public class LambdaExceptionWrappers {
9+
10+
private static final Logger LOGGER = LoggerFactory.getLogger(LambdaExceptionWrappers.class);
11+
12+
public static Consumer<Integer> lambdaWrapper(Consumer<Integer> consumer) {
13+
return i -> {
14+
try {
15+
consumer.accept(i);
16+
} catch (ArithmeticException e) {
17+
LOGGER.error("Arithmetic Exception occurred.", e);
18+
}
19+
};
20+
}
21+
22+
static <T, E extends Exception> Consumer<T> consumerWrapper(Consumer<T> consumer, Class<E> clazz) {
23+
return i -> {
24+
try {
25+
consumer.accept(i);
26+
} catch (Exception ex) {
27+
try {
28+
E exCast = clazz.cast(ex);
29+
LOGGER.error("Exception occurred.", exCast);
30+
} catch (ClassCastException ccEx) {
31+
throw ex;
32+
}
33+
}
34+
};
35+
}
36+
37+
public static <T> Consumer<T> throwingConsumerWrapper(ThrowingConsumer<T, Exception> throwingConsumer) {
38+
return i -> {
39+
try {
40+
throwingConsumer.accept(i);
41+
} catch (Exception ex) {
42+
throw new RuntimeException(ex);
43+
}
44+
};
45+
}
46+
47+
public static <T, E extends Exception> Consumer<T> handlingConsumerWrapper(ThrowingConsumer<T, E> throwingConsumer, Class<E> exceptionClass) {
48+
return i -> {
49+
try {
50+
throwingConsumer.accept(i);
51+
} catch (Exception ex) {
52+
try {
53+
E exCast = exceptionClass.cast(ex);
54+
LOGGER.error("Exception occurred.", exCast);
55+
} catch (ClassCastException ccEx) {
56+
throw new RuntimeException(ex);
57+
}
58+
}
59+
};
60+
}
61+
62+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.java8.lambda.exceptions;
2+
3+
@FunctionalInterface
4+
public interface ThrowingConsumer<T, E extends Exception> {
5+
6+
void accept(T t) throws E;
7+
8+
}

0 commit comments

Comments
 (0)