Examples of Method Reference and Constructor Reference
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package com.baeldung.doublecolumn;
|
||||
|
||||
|
||||
public class Computer {
|
||||
|
||||
private Integer age;
|
||||
private String color;
|
||||
private Integer healty;
|
||||
|
||||
public Computer(int age, String color) {
|
||||
this.age = age;
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public Computer(Integer age, String color, Integer healty) {
|
||||
this.age = age;
|
||||
this.color = color;
|
||||
this.healty = healty;
|
||||
}
|
||||
|
||||
public Computer() {
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public Integer getHealty() {
|
||||
return healty;
|
||||
}
|
||||
|
||||
public void setHealty(Integer healty) {
|
||||
this.healty = healty;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Computer{" +
|
||||
"age=" + age +
|
||||
", color='" + color + '\'' +
|
||||
", healty=" + healty +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Computer computer = (Computer) o;
|
||||
|
||||
if (age != null ? !age.equals(computer.age) : computer.age != null) return false;
|
||||
return color != null ? color.equals(computer.color) : computer.color == null;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = age != null ? age.hashCode() : 0;
|
||||
result = 31 * result + (color != null ? color.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.doublecolumn;
|
||||
|
||||
import com.baeldung.doublecolumn.function.ComputerPredicate;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ComputerUtils {
|
||||
|
||||
|
||||
public static final ComputerPredicate after2010Predicate = (c ) -> ( c.getAge() > 2010 );
|
||||
public static final ComputerPredicate blackPredicate = ( c ) -> "black".equals(c.getColor());
|
||||
|
||||
public static List<Computer> filter(List<Computer> inventory, ComputerPredicate p){
|
||||
|
||||
List<Computer> result = new ArrayList<>();
|
||||
inventory.stream().filter(p::filter).forEach(result::add);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public static void repair (Computer computer){
|
||||
if(computer.getHealty()<50){
|
||||
computer.setHealty(100);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.doublecolumn.function;
|
||||
|
||||
import com.baeldung.doublecolumn.Computer;
|
||||
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ComputerPredicate {
|
||||
|
||||
boolean filter(Computer c);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.doublecolumn.function;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface TriFunction<A,B,C,R> {
|
||||
|
||||
R apply(A a, B b, C c);
|
||||
|
||||
default <V> TriFunction<A, B, C, V> andThen(
|
||||
Function<? super R, ? extends V> after) {
|
||||
Objects.requireNonNull(after);
|
||||
return (A a, B b, C c) -> after.apply(apply(a, b, c));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user