Skip to content

Commit e792f17

Browse files
author
alv21
committed
BAEL-1585
1 parent d338d06 commit e792f17

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.java_8_features;
2+
3+
public class Car {
4+
5+
private String model;
6+
private int topSpeed;
7+
8+
public Car(String model, int topSpeed) {
9+
super();
10+
this.model = model;
11+
this.topSpeed = topSpeed;
12+
}
13+
14+
public String getModel() {
15+
return model;
16+
}
17+
18+
public void setModel(String model) {
19+
this.model = model;
20+
}
21+
22+
public int getTopSpeed() {
23+
return topSpeed;
24+
}
25+
26+
public void setTopSpeed(int topSpeed) {
27+
this.topSpeed = topSpeed;
28+
}
29+
30+
31+
}

‎core-java-8/src/test/java/com/baeldung/java8/Java8MaxMinUnitTest.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.baeldung.java8;
22

33
import com.baeldung.java_8_features.Person;
4+
import com.baeldung.java_8_features.Car;
45
import org.junit.Test;
56

67
import java.util.Arrays;
@@ -44,4 +45,29 @@ public void whenListIsOfPersonObjectThenMinCanBeDoneUsingCustomComparatorThrough
4445
assertEquals("Should be Alex", alex, minByAge);
4546
}
4647

48+
@Test
49+
public void whenArrayIsOfIntegerThenMinCanBeDoneUsingIntegerComparator() {
50+
int[] integers = new int[] { 20, 98, 12, 7, 35 };
51+
52+
int min = Arrays.stream(integers)
53+
.min()
54+
.getAsInt();
55+
56+
assertEquals(7, min);
57+
}
58+
59+
@Test
60+
public void whenArrayIsOfPersonObjectThenMaxCanBeDoneUsingCustomComparatorThroughLambda() {
61+
final Car porsche = new Car("Porsche 959", 319);
62+
final Car ferrari = new Car("Ferrari 288 GTO", 303);
63+
final Car bugatti = new Car("Bugatti Veyron 16.4 Super Sport", 415);
64+
final Car mcLaren = new Car("McLaren F1", 355);
65+
final Car[] fastCars = { porsche, ferrari, bugatti, mcLaren };
66+
67+
final Car maxBySpeed = Arrays.stream(fastCars)
68+
.max(Comparator.comparing(Car::getTopSpeed))
69+
.orElseThrow(NoSuchElementException::new);
70+
71+
assertEquals(bugatti, maxBySpeed);
72+
}
4773
}

0 commit comments

Comments
 (0)