diff --git a/core-java-modules/core-java-arrays-convert/src/test/java/com/baeldung/array/conversions/PrimitiveIntArrayToHashSetUnitTest.java b/core-java-modules/core-java-arrays-convert/src/test/java/com/baeldung/array/conversions/PrimitiveIntArrayToHashSetUnitTest.java new file mode 100644 index 0000000000..ca658d3e97 --- /dev/null +++ b/core-java-modules/core-java-arrays-convert/src/test/java/com/baeldung/array/conversions/PrimitiveIntArrayToHashSetUnitTest.java @@ -0,0 +1,53 @@ +package com.baeldung.array.conversions; + +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.stream.Collectors; + +import org.apache.commons.lang3.ArrayUtils; +import org.junit.Test; + +import com.google.common.primitives.Ints; + +public class PrimitiveIntArrayToHashSetUnitTest { + int[] arr = { 1, 2, 3, 4, 5 }; + HashSet expected = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5)); + + @Test + public void givenPrimitiveIntArray_whenConvertingByDirectConstructor_thenGiveWrongResult() { + HashSet result = new HashSet<>(Arrays.asList(arr)); + assertEquals(1, result.size()); + assertNotEquals(expected, result); + } + + @Test + public void givenPrimitiveIntArray_whenConvertingByLoop_thenSuccess() { + HashSet result = new HashSet<>(); + for (int num : arr) { + result.add(num); + } + assertEquals(expected, result); + } + + @Test + public void givenPrimitiveIntArray_whenConvertingByStreams_thenSuccess() { + HashSet result = Arrays.stream(arr).boxed().collect(Collectors.toCollection(HashSet::new)); + assertEquals(expected, result); + } + + @Test + public void givenPrimitiveIntArray_whenConvertingByArrayUtils_thenSuccess() { + HashSet result = new HashSet<>(Arrays.asList(ArrayUtils.toObject(arr))); + assertEquals(expected, result); + } + + @Test + public void givenPrimitiveIntArray_whenConvertingByGuava_thenSuccess() { + HashSet result = new HashSet<>(Ints.asList(arr)); + assertEquals(expected, result); + } + +}