From 3d76035de823c08366b6b3dd4da2556ed7cde783 Mon Sep 17 00:00:00 2001 From: ACHRAF TAITAI <43656331+achraftt@users.noreply.github.com> Date: Wed, 26 Jul 2023 11:17:26 +0200 Subject: [PATCH] BAEL-6713: Convert List Object to long[] Array in Java (#14479) --- ...LongListToLongArrayConversionUnitTest.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 core-java-modules/core-java-collections-conversions-2/src/test/java/com/baeldung/convertlisttoarray/LongListToLongArrayConversionUnitTest.java diff --git a/core-java-modules/core-java-collections-conversions-2/src/test/java/com/baeldung/convertlisttoarray/LongListToLongArrayConversionUnitTest.java b/core-java-modules/core-java-collections-conversions-2/src/test/java/com/baeldung/convertlisttoarray/LongListToLongArrayConversionUnitTest.java new file mode 100644 index 0000000000..915a4bfc02 --- /dev/null +++ b/core-java-modules/core-java-collections-conversions-2/src/test/java/com/baeldung/convertlisttoarray/LongListToLongArrayConversionUnitTest.java @@ -0,0 +1,74 @@ +package com.baeldung.convertlisttoarray; + +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +import com.google.common.primitives.Longs; + +public class LongListToLongArrayConversionUnitTest { + + private List list; + + @Before + public void setUp() { + list = Arrays.asList(1L, 2L, 3L, 4L, 5L); + } + + @Test + public void givenALongList_whenConvertWithListToArray_thenReturnLongArray() { + + // Init an array with the same size as the list to convert + Long[] arrayWithSettedSize = new Long[list.size()]; + arrayWithSettedSize = list.toArray(arrayWithSettedSize); + assertTrue(list.size() == arrayWithSettedSize.length); + + // Init an empty array + Long[] arrayWithNoSettedSize = new Long[0]; + arrayWithNoSettedSize = list.toArray(arrayWithNoSettedSize); + assertTrue(list.size() == arrayWithNoSettedSize.length); + } + + @Test + public void givenALongList_whenConvertWithLongsToArray_thenReturnLongArray() { + // Convertion using Guava library Longs.toArray() method + long[] array = Longs.toArray(list); + assertTrue(compareListWithArray(list, array)); + } + + @Test + public void givenALongList_whenConvertWithStreamMapToLong_thenReturnLongArray() { + // Using mapToLong() - lambda expression + long[] arrayUsingLambda = list.stream() + .mapToLong(l -> l) + .toArray(); + assertTrue(compareListWithArray(list, arrayUsingLambda)); + + // Using mapToLong() - method reference + long[] arrayUsingMethodReference = list.stream() + .mapToLong(Long::longValue) + .toArray(); + assertTrue(compareListWithArray(list, arrayUsingMethodReference)); + } + + public static boolean compareListWithArray(List list, long[] array) { + // Check if the sizes of the array and list are equal + if (array.length != list.size()) { + return false; + } + + // Compare each element of the array with the corresponding element in the list + for (int i = 0; i < array.length; i++) { + // Convert Long to long for comparison + if (array[i] != list.get(i)) { + return false; + } + } + + return true; + } +}