Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions core-java-8/src/main/java/com/baeldung/java_8_features/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.baeldung.java_8_features;

public class Car {

private String model;
private int topSpeed;

public Car(String model, int topSpeed) {
super();
this.model = model;
this.topSpeed = topSpeed;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public int getTopSpeed() {
return topSpeed;
}

public void setTopSpeed(int topSpeed) {
this.topSpeed = topSpeed;
}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.baeldung.java8;

import com.baeldung.java_8_features.Car;
import com.baeldung.java_8_features.Person;

import org.junit.Test;

import java.util.Arrays;
Expand Down Expand Up @@ -44,4 +46,29 @@ public void whenListIsOfPersonObjectThenMinCanBeDoneUsingCustomComparatorThrough
assertEquals("Should be Alex", alex, minByAge);
}

@Test
public void whenArrayIsOfIntegerThenMinUsesIntegerComparator() {
int[] integers = new int[] { 20, 98, 12, 7, 35 };

int min = Arrays.stream(integers)
.min()
.getAsInt();

assertEquals(7, min);
}

@Test
public void whenArrayIsOfCustomTypeThenMaxUsesCustomComparator() {
final Car porsche = new Car("Porsche 959", 319);
final Car ferrari = new Car("Ferrari 288 GTO", 303);
final Car bugatti = new Car("Bugatti Veyron 16.4 Super Sport", 415);
final Car mcLaren = new Car("McLaren F1", 355);
final Car[] fastCars = { porsche, ferrari, bugatti, mcLaren };

final Car maxBySpeed = Arrays.stream(fastCars)
.max(Comparator.comparing(Car::getTopSpeed))
.orElseThrow(NoSuchElementException::new);

assertEquals(bugatti, maxBySpeed);
}
}