BAEL-2524 (#6206)
* BAEL-2524 Uploading article examples. * Fixing build error Used List.of, which is not available on java 8 (running java 11 on my PC)
This commit is contained in:
parent
9a567f95b4
commit
4bd87241ce
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.java8.lambda.methodreference;
|
||||
|
||||
public class Bicycle {
|
||||
|
||||
private String brand;
|
||||
private Integer frameSize;
|
||||
|
||||
public Bicycle(String brand, Integer frameSize) {
|
||||
this.brand = brand;
|
||||
this.frameSize = frameSize;
|
||||
}
|
||||
|
||||
public String getBrand() {
|
||||
return brand;
|
||||
}
|
||||
|
||||
public void setBrand(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
|
||||
public Integer getFrameSize() {
|
||||
return frameSize;
|
||||
}
|
||||
|
||||
public void setFrameSize(Integer frameSize) {
|
||||
this.frameSize = frameSize;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.java8.lambda.methodreference;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public class BicycleComparator implements Comparator<Bicycle> {
|
||||
|
||||
@Override
|
||||
public int compare(Bicycle a, Bicycle b) {
|
||||
return a.getFrameSize()
|
||||
.compareTo(b.getFrameSize());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package com.baeldung.java8.lambda.methodreference;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class MethodReferenceExamples {
|
||||
|
||||
private static <T> void doNothingAtAll(Object... o) {
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
@Test
|
||||
public void referenceToStaticMethod() {
|
||||
List<String> messages = Arrays.asList("Hello", "Baeldung", "readers!");
|
||||
messages.forEach((word) -> {
|
||||
System.out.println(word);
|
||||
});
|
||||
messages.forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void referenceToInstanceMethodOfParticularObject() {
|
||||
BicycleComparator bikeFrameSizeComparator = new BicycleComparator();
|
||||
createBicyclesList().stream()
|
||||
.sorted((a, b) -> bikeFrameSizeComparator.compare(a, b));
|
||||
createBicyclesList().stream()
|
||||
.sorted(bikeFrameSizeComparator::compare);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void referenceToInstanceMethodOfArbitratyObjectOfParticularType() {
|
||||
List<Integer> numbers = Arrays.asList(5, 3, 50, 24, 40, 2, 9, 18);
|
||||
numbers.stream()
|
||||
.sorted((a, b) -> Integer.compare(a, b));
|
||||
numbers.stream()
|
||||
.sorted(Integer::compare);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void referenceToConstructor() {
|
||||
BiFunction<String, Integer, Bicycle> bikeCreator = (brand, frameSize) -> new Bicycle(brand, frameSize);
|
||||
BiFunction<String, Integer, Bicycle> bikeCreatorMethodReference = Bicycle::new;
|
||||
List<Bicycle> bikes = new ArrayList<>();
|
||||
bikes.add(bikeCreator.apply("Giant", 50));
|
||||
bikes.add(bikeCreator.apply("Scott", 20));
|
||||
bikes.add(bikeCreatorMethodReference.apply("Trek", 35));
|
||||
bikes.add(bikeCreatorMethodReference.apply("GT", 40));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void limitationsAndAdditionalExamples() {
|
||||
createBicyclesList().forEach(b -> System.out.printf("Bike brand is '%s' and frame size is '%d'%n", b.getBrand(), b.getFrameSize()));
|
||||
createBicyclesList().forEach((o) -> this.doNothingAtAll(o));
|
||||
}
|
||||
|
||||
private List<Bicycle> createBicyclesList() {
|
||||
List<Bicycle> bikes = new ArrayList<>();
|
||||
bikes.add(new Bicycle("Giant", 50));
|
||||
bikes.add(new Bicycle("Scott", 20));
|
||||
bikes.add(new Bicycle("Trek", 35));
|
||||
bikes.add(new Bicycle("GT", 40));
|
||||
return bikes;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue