[improvement-init-Long-list] update
This commit is contained in:
parent
74da8faf7f
commit
ce618671be
@ -1,8 +1,7 @@
|
||||
package com.baeldung.java.listInitialization;
|
||||
|
||||
import lombok.extern.java.Log;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@ -10,6 +9,10 @@ import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import lombok.extern.java.Log;
|
||||
|
||||
@Log
|
||||
public class ListInitializationUnitTest {
|
||||
|
||||
@ -23,14 +26,14 @@ public class ListInitializationUnitTest {
|
||||
}
|
||||
};
|
||||
|
||||
Assert.assertTrue(cities.contains("New York"));
|
||||
assertTrue(cities.contains("New York"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArraysAsList_thenInitialiseList() {
|
||||
List<String> list = Arrays.asList("foo", "bar");
|
||||
|
||||
Assert.assertTrue(list.contains("foo"));
|
||||
assertTrue(list.contains("foo"));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
@ -40,12 +43,23 @@ public class ListInitializationUnitTest {
|
||||
list.add("baz");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArraysAsList_whenUsingArrayListConstructor_thenWeCanAddOrRemove() {
|
||||
List<String> list = new ArrayList<>(Arrays.asList("foo", "bar"));
|
||||
|
||||
list.add("baz");
|
||||
assertEquals(List.of("foo", "bar","baz"), list);
|
||||
|
||||
list.remove("baz");
|
||||
assertEquals(List.of("foo", "bar"), list);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArraysAsList_whenCreated_thenShareReference() {
|
||||
String[] array = { "foo", "bar" };
|
||||
List<String> list = Arrays.asList(array);
|
||||
array[0] = "baz";
|
||||
Assert.assertEquals("baz", list.get(0));
|
||||
assertEquals("baz", list.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -53,7 +67,7 @@ public class ListInitializationUnitTest {
|
||||
int intNum = 42;
|
||||
long longNum = intNum;
|
||||
|
||||
Assert.assertEquals(42L, longNum);
|
||||
assertEquals(42L, longNum);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -63,15 +77,15 @@ public class ListInitializationUnitTest {
|
||||
|
||||
List<Long> expected = List.of(1L, 2L, 3L);
|
||||
|
||||
Assert.assertEquals(expected, listOfLongFixedSize);
|
||||
Assert.assertEquals(expected, listOfLong);
|
||||
assertEquals(expected, listOfLongFixedSize);
|
||||
assertEquals(expected, listOfLong);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStream_thenInitializeList() {
|
||||
List<String> list = Stream.of("foo", "bar")
|
||||
.collect(Collectors.toList());
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Assert.assertTrue(list.contains("foo"));
|
||||
assertTrue(list.contains("foo"));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user