这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.baeldung.array.minmax;

import org.junit.Test;

import java.util.Arrays;
import java.util.IntSummaryStatistics;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class MinMax2DArrayUnitTest {

@Test
public void givenArray2DWhenFindMinAndMaxUsingForLoopsThenCorrect() {
int[][] array = {{8, 4, 1}, {2, 5, 7}, {3, 6, 9}};

int min = array[0][0];
int max = array[0][0];
for (int[] row : array) {
for (int currentValue : row) {
if (currentValue < min) {
min = currentValue;
} else if (currentValue > max) {
max = currentValue;
}
}
}

assertEquals(1, min);
assertEquals(9, max);
}

@Test
public void givenArray2DWhenFindMinAndMaxUsingStreamThenCorrect() {
int[][] array = {{8, 4, 1}, {2, 5, 7}, {3, 6, 9}};

IntSummaryStatistics stats = Arrays
.stream(array)
.flatMapToInt(Arrays::stream)
.summaryStatistics();

assertEquals(1, stats.getMin());
assertEquals(9, stats.getMax());
}

@Test
public void givenArray2DWhenFindMinAndMaxUsingParallelStreamThenCorrect() {
int[][] array = {{8, 4, 1}, {2, 5, 7}, {3, 6, 9}};

IntSummaryStatistics stats = Arrays
.stream(array)
.parallel()
.flatMapToInt(Arrays::stream)
.summaryStatistics();

assertEquals(1, stats.getMin());
assertEquals(9, stats.getMax());
}

}