diff --git a/algorithms-modules/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/integerstreammedian/MedianOfIntegerStream.java b/algorithms-modules/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/integerstreammedian/MedianOfIntegerStream.java
index 9d3b699956..d3fde8a827 100644
--- a/algorithms-modules/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/integerstreammedian/MedianOfIntegerStream.java
+++ b/algorithms-modules/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/integerstreammedian/MedianOfIntegerStream.java
@@ -29,13 +29,13 @@ public class MedianOfIntegerStream {
}
double getMedian() {
- int median;
+ double median;
if (minHeap.size() < maxHeap.size()) {
median = maxHeap.peek();
} else if (minHeap.size() > maxHeap.size()) {
median = minHeap.peek();
} else {
- median = (minHeap.peek() + maxHeap.peek()) / 2;
+ median = (minHeap.peek() + maxHeap.peek()) / 2.0;
}
return median;
}
diff --git a/algorithms-modules/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/integerstreammedian/MedianOfIntegerStream2.java b/algorithms-modules/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/integerstreammedian/MedianOfIntegerStream2.java
index 8aa7e6f6af..a28709c2d6 100644
--- a/algorithms-modules/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/integerstreammedian/MedianOfIntegerStream2.java
+++ b/algorithms-modules/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/integerstreammedian/MedianOfIntegerStream2.java
@@ -25,11 +25,11 @@ public class MedianOfIntegerStream2 {
}
double getMedian() {
- int median;
+ double median;
if (minHeap.size() > maxHeap.size()) {
median = minHeap.peek();
} else {
- median = (minHeap.peek() + maxHeap.peek()) / 2;
+ median = (minHeap.peek() + maxHeap.peek()) / 2.0;
}
return median;
}
diff --git a/core-java-modules/core-java-arrays-operations-basic-2/pom.xml b/core-java-modules/core-java-arrays-operations-basic-2/pom.xml
index 354da6d7ff..27ebc9166b 100644
--- a/core-java-modules/core-java-arrays-operations-basic-2/pom.xml
+++ b/core-java-modules/core-java-arrays-operations-basic-2/pom.xml
@@ -13,4 +13,12 @@
0.0.1-SNAPSHOT
+
+
+ org.apache.commons
+ commons-lang3
+ 3.14.0
+
+
+
\ No newline at end of file
diff --git a/core-java-modules/core-java-arrays-operations-basic-2/src/test/java/com/baeldung/array/flatarray/FlatArrayUnitTest.java b/core-java-modules/core-java-arrays-operations-basic-2/src/test/java/com/baeldung/array/flatarray/FlatArrayUnitTest.java
new file mode 100644
index 0000000000..5d67c68a37
--- /dev/null
+++ b/core-java-modules/core-java-arrays-operations-basic-2/src/test/java/com/baeldung/array/flatarray/FlatArrayUnitTest.java
@@ -0,0 +1,130 @@
+package com.baeldung.array.flatarray;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+class FlatArrayUnitTest {
+
+ @ParameterizedTest
+ @MethodSource("arrayProvider")
+ void giveTwoDimensionalArray_whenFlatWithStream_thenGetCorrectResult(int[][] initialArray,
+ int[] expected) {
+ int[] actual = Arrays.stream(initialArray).flatMapToInt(Arrays::stream).toArray();
+ assertThat(actual).containsExactly(expected);
+ }
+
+ @ParameterizedTest
+ @MethodSource("arrayProvider")
+ void giveTwoDimensionalArray_whenFlatWithForLoopAndAdditionalList_thenGetCorrectResult(int[][] initialArray,
+ int[] intArray) {
+ List expected = Arrays.stream(intArray).boxed().collect(Collectors.toList());
+ List actual = new ArrayList<>();
+ for (int[] numbers : initialArray) {
+ for (int number : numbers) {
+ actual.add(number);
+ }
+ }
+ assertThat(actual).isEqualTo(expected);
+ }
+
+ @ParameterizedTest
+ @MethodSource("arrayProvider")
+ void giveTwoDimensionalArray_whenFlatWithForLoopAndLists_thenGetCorrectResult(int[][] initialArray,
+ int[] intArray) {
+ List expected = Arrays.stream(intArray).boxed().collect(Collectors.toList());
+ List actual = new ArrayList<>();
+ for (int[] numbers : initialArray) {
+ List listOfNumbers = Arrays.stream(numbers).boxed().collect(Collectors.toList());
+ actual.addAll(listOfNumbers);
+ }
+ assertThat(actual).isEqualTo(expected);
+ }
+
+ @ParameterizedTest
+ @MethodSource("arrayProvider")
+ void giveTwoDimensionalArray_whenFlatWithArrayCopy_thenGetCorrectResult(int[][] initialArray,
+ int[] expected) {
+ int[] actual = new int[]{};
+ int position = 0;
+ for (int[] numbers : initialArray) {
+ if (actual.length < position + numbers.length) {
+ int[] newArray = new int[actual.length + numbers.length];
+ System.arraycopy(actual, 0, newArray, 0, actual.length);
+ actual = newArray;
+ }
+ System.arraycopy(numbers, 0, actual, position, numbers.length);
+ position += numbers.length;
+ }
+ assertThat(actual).isEqualTo(expected);
+ }
+
+ @ParameterizedTest
+ @MethodSource("arrayProvider")
+ void giveTwoDimensionalArray_whenFlatWithArrayCopyAndTotalNumberOfElements_thenGetCorrectResult(int[][] initialArray,
+ int[] expected) {
+ int totalNumberOfElements = 0;
+ for (int[] numbers : initialArray) {
+ totalNumberOfElements += numbers.length;
+ }
+ int[] actual = new int[totalNumberOfElements];
+ int position = 0;
+ for (int[] numbers : initialArray) {
+ System.arraycopy(numbers, 0, actual, position, numbers.length);
+ position += numbers.length;
+ }
+ assertThat(actual).isEqualTo(expected);
+ }
+
+ @ParameterizedTest
+ @MethodSource("arrayProvider")
+ void giveTwoDimensionalArray_whenFlatWithForLoopAndTotalNumberOfElements_thenGetCorrectResult(int[][] initialArray,
+ int[] expected) {
+ int totalNumberOfElements = 0;
+ for (int[] numbers : initialArray) {
+ totalNumberOfElements += numbers.length;
+ }
+ int[] actual = new int[totalNumberOfElements];
+ int position = 0;
+ for (int[] numbers : initialArray) {
+ for (int number : numbers) {
+ actual[position] = number;
+ ++position;
+ }
+ }
+ assertThat(actual).isEqualTo(expected);
+ }
+
+
+ static Stream arrayProvider() {
+ return Stream.of(
+ Arguments.of(
+ new int[][]{
+ {805, 902, 259, 162, 775},
+ {278, 216, 0, 72, 663},
+ {185, 390, 537, 909, 918},
+ {150, 782, 282, 482, 401},
+ {244, 685, 643, 364, 307},
+ {483, 939, 750, 190, 424},
+ {44, 160, 290, 963, 881}
+ },
+ new int[]{
+ 805, 902, 259, 162, 775,
+ 278, 216, 0, 72, 663,
+ 185, 390, 537, 909, 918,
+ 150, 782, 282, 482, 401,
+ 244, 685, 643, 364, 307,
+ 483, 939, 750, 190, 424,
+ 44, 160, 290, 963, 881
+ }
+ )
+ );
+ }
+}
diff --git a/core-java-modules/core-java-arrays-operations-basic-2/src/test/java/com/baeldung/array/smallestindex/SmallestElementIndexUnitTest.java b/core-java-modules/core-java-arrays-operations-basic-2/src/test/java/com/baeldung/array/smallestindex/SmallestElementIndexUnitTest.java
new file mode 100644
index 0000000000..9f8299b9e5
--- /dev/null
+++ b/core-java-modules/core-java-arrays-operations-basic-2/src/test/java/com/baeldung/array/smallestindex/SmallestElementIndexUnitTest.java
@@ -0,0 +1,132 @@
+package com.baeldung.array.smallestindex;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+import org.apache.commons.lang3.ArrayUtils;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+class SmallestElementIndexUnitTest {
+
+ @ParameterizedTest
+ @MethodSource("primitiveProvider")
+ void givenArray_whenUsingForLoop_thenGetCorrectResult(int[] array, int expectedIndex) {
+ int minValue = Integer.MAX_VALUE;
+ int minIndex = -1;
+ for (int i = 0; i < array.length; i++) {
+ if (array[i] < minValue) {
+ minValue = array[i];
+ minIndex = i;
+ }
+ }
+ assertThat(minIndex).isEqualTo(expectedIndex);
+ }
+
+ @ParameterizedTest
+ @MethodSource("primitiveProvider")
+ void givenArray_whenUsingForLoopAndLookForIndex_thenGetCorrectResult(int[] array, int expectedIndex) {
+ int minValue = Integer.MAX_VALUE;
+ for (int number : array) {
+ if (number < minValue) {
+ minValue = number;
+ }
+ }
+ int minIndex = -1;
+ for (int i = 0; i < array.length; i++) {
+ if (array[i] == minValue) {
+ minIndex = i;
+ break;
+ }
+ }
+ assertThat(minIndex).isEqualTo(expectedIndex);
+ }
+
+ @ParameterizedTest
+ @MethodSource("primitiveProvider")
+ void givenArray_whenUsingIntStreamAndLookForIndex_thenGetCorrectResult(int[] array, int expectedIndex) {
+ int minValue = Arrays.stream(array).min().orElse(Integer.MAX_VALUE);
+ int minIndex = -1;
+ for (int i = 0; i < array.length; i++) {
+ if (array[i] == minValue) {
+ minIndex = i;
+ break;
+ }
+ }
+ assertThat(minIndex).isEqualTo(expectedIndex);
+ }
+
+ @ParameterizedTest
+ @MethodSource("primitiveProvider")
+ void givenArray_whenUsingIntStreamAndLookForIndexWithIntStream_thenGetCorrectResult(int[] array, int expectedIndex) {
+ int minValue = Arrays.stream(array).min().orElse(Integer.MAX_VALUE);
+ int minIndex = IntStream.range(0, array.length)
+ .filter(index -> array[index] == minValue)
+ .findFirst().orElse(-1);
+ assertThat(minIndex).isEqualTo(expectedIndex);
+ }
+
+ @ParameterizedTest
+ @MethodSource("primitiveProvider")
+ void givenArray_whenUsingIntStreamAndLookForIndexWithArrayUtils_thenGetCorrectResult(int[] array, int expectedIndex) {
+ int minValue = Arrays.stream(array).min().orElse(Integer.MAX_VALUE);
+ int minIndex = ArrayUtils.indexOf(array, minValue);
+ assertThat(minIndex).isEqualTo(expectedIndex);
+ }
+
+ @ParameterizedTest
+ @MethodSource("referenceTypesProvider")
+ void givenArray_whenUsingReduce_thenGetCorrectResult(Integer[] array, int expectedIndex) {
+ int minValue = Arrays.stream(array).reduce(Integer.MAX_VALUE, Integer::min);
+ int minIndex = ArrayUtils.indexOf(array, minValue);
+ assertThat(minIndex).isEqualTo(expectedIndex);
+ }
+
+ @ParameterizedTest
+ @MethodSource("referenceTypesProvider")
+ void givenArray_whenUsingReduceAndList_thenGetCorrectResult(Integer[] array, int expectedIndex) {
+ List list = Arrays.asList(array);
+ int minValue = list.stream().reduce(Integer.MAX_VALUE, Integer::min);
+ int index = list.indexOf(minValue);
+ assertThat(index).isEqualTo(expectedIndex);
+ }
+
+ @ParameterizedTest
+ @MethodSource("primitiveProvider")
+ void givenArray_whenUsingReduceWithRange_thenGetCorrectResult(int[] array, int expectedIndex) {
+ int index = IntStream.range(0, array.length)
+ .reduce((a, b) -> array[a] <= array[b] ? a : b)
+ .orElse(-1);
+ assertThat(index).isEqualTo(expectedIndex);
+ }
+
+ @ParameterizedTest
+ @MethodSource("primitiveProvider")
+ void givenArray_whenUsingPrimitiveStreams_thenGetCorrectResult(int[] array, int expectedIndex) {
+ List list = Arrays.stream(array).boxed().collect(Collectors.toList());
+ int minValue = Arrays.stream(array).min().orElse(Integer.MAX_VALUE);
+ int index = list.indexOf(minValue);
+ assertThat(index).isEqualTo(expectedIndex);
+ }
+
+ static Stream primitiveProvider() {
+ return Stream.of(
+ Arguments.of(new int[]{585, 190, 201, 82, 332}, 3),
+ Arguments.of(new int[]{1, 1, 1}, 0),
+ Arguments.of(new int[]{}, -1)
+ );
+ }
+
+ static Stream referenceTypesProvider() {
+ return Stream.of(
+ Arguments.of(new Integer[]{585, 190, 201, 82, 332}, 3),
+ Arguments.of(new Integer[]{1, 1, 1}, 0),
+ Arguments.of(new Integer[]{}, -1)
+ );
+ }
+}
diff --git a/core-java-modules/core-java-collections-5/README.md b/core-java-modules/core-java-collections-5/README.md
index e1ad221a31..1608b96775 100644
--- a/core-java-modules/core-java-collections-5/README.md
+++ b/core-java-modules/core-java-collections-5/README.md
@@ -7,10 +7,8 @@
- [Creating Custom Iterator in Java](https://www.baeldung.com/java-creating-custom-iterator)
- [Difference Between Arrays.sort() and Collections.sort()](https://www.baeldung.com/java-arrays-collections-sort-methods)
- [Skipping the First Iteration in Java](https://www.baeldung.com/java-skip-first-iteration)
-- [Remove Elements From a Queue Using Loop](https://www.baeldung.com/java-remove-elements-queue)
- [Intro to Vector Class in Java](https://www.baeldung.com/java-vector-guide)
- [Time Complexity of Java Collections Sort in Java](https://www.baeldung.com/java-time-complexity-collections-sort)
-- [Check if List Contains at Least One Enum](https://www.baeldung.com/java-list-check-enum-presence)
- [Comparison of for Loops and Iterators](https://www.baeldung.com/java-for-loops-vs-iterators)
- [PriorityQueue iterator() Method in Java](https://www.baeldung.com/java-priorityqueue-iterator)
- [Immutable vs Unmodifiable Collection in Java](https://www.baeldung.com/java-collection-immutable-unmodifiable-differences)
diff --git a/core-java-modules/core-java-collections-6/README.md b/core-java-modules/core-java-collections-6/README.md
index 399e34a4d5..062e3e5409 100644
--- a/core-java-modules/core-java-collections-6/README.md
+++ b/core-java-modules/core-java-collections-6/README.md
@@ -5,4 +5,7 @@
### Relevant Articles:
- [Iterator vs forEach() in Java](https://www.baeldung.com/java-iterator-vs-foreach)
- [Adding Elements to a Collection During Iteration](https://www.baeldung.com/java-add-elements-collection)
+- [Remove Elements From a Queue Using Loop](https://www.baeldung.com/java-remove-elements-queue)
+- [Check if List Contains at Least One Enum](https://www.baeldung.com/java-list-check-enum-presence)
+
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-5)
diff --git a/core-java-modules/core-java-collections-5/src/test/java/com/baeldung/checkiflistcontainsenum/CheckIfListContainsEnumUnitTest.java b/core-java-modules/core-java-collections-6/src/test/java/com/baeldung/checkiflistcontainsenum/CheckIfListContainsEnumUnitTest.java
similarity index 96%
rename from core-java-modules/core-java-collections-5/src/test/java/com/baeldung/checkiflistcontainsenum/CheckIfListContainsEnumUnitTest.java
rename to core-java-modules/core-java-collections-6/src/test/java/com/baeldung/checkiflistcontainsenum/CheckIfListContainsEnumUnitTest.java
index 0b9fd356e2..2d87d0ca47 100644
--- a/core-java-modules/core-java-collections-5/src/test/java/com/baeldung/checkiflistcontainsenum/CheckIfListContainsEnumUnitTest.java
+++ b/core-java-modules/core-java-collections-6/src/test/java/com/baeldung/checkiflistcontainsenum/CheckIfListContainsEnumUnitTest.java
@@ -1,57 +1,57 @@
-package com.baeldung.checkiflistcontainsenum;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-import java.util.*;
-
-public class CheckIfListContainsEnumUnitTest {
- private final List