[improvement-init-Long-list] add long list init
This commit is contained in:
parent
317496fa87
commit
74da8faf7f
@ -1,16 +1,15 @@
|
|||||||
package com.baeldung.java.listInitialization;
|
package com.baeldung.java.listInitialization;
|
||||||
|
|
||||||
|
import lombok.extern.java.Log;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import lombok.extern.java.Log;
|
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
@Log
|
@Log
|
||||||
public class ListInitializationUnitTest {
|
public class ListInitializationUnitTest {
|
||||||
|
|
||||||
@ -49,10 +48,29 @@ public class ListInitializationUnitTest {
|
|||||||
Assert.assertEquals("baz", list.get(0));
|
Assert.assertEquals("baz", list.get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenIntNumbers_whenRequiredLong_thenCastAutomatically() {
|
||||||
|
int intNum = 42;
|
||||||
|
long longNum = intNum;
|
||||||
|
|
||||||
|
Assert.assertEquals(42L, longNum);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenArrayAsList_whenRequiredLongList_thenGetExpectedResult() {
|
||||||
|
List<Long> listOfLongFixedSize = Arrays.asList(1L, 2L, 3L);
|
||||||
|
List<Long> listOfLong = new ArrayList<>(Arrays.asList(1L, 2L, 3L));
|
||||||
|
|
||||||
|
List<Long> expected = List.of(1L, 2L, 3L);
|
||||||
|
|
||||||
|
Assert.assertEquals(expected, listOfLongFixedSize);
|
||||||
|
Assert.assertEquals(expected, listOfLong);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenStream_thenInitializeList() {
|
public void givenStream_thenInitializeList() {
|
||||||
List<String> list = Stream.of("foo", "bar")
|
List<String> list = Stream.of("foo", "bar")
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
Assert.assertTrue(list.contains("foo"));
|
Assert.assertTrue(list.contains("foo"));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user