Merge pull request #16310 from sk1418/improvement-init-Long-list
[improvement-init-Long-list] add long list init
This commit is contained in:
commit
e6c33f07c4
|
@ -1,59 +0,0 @@
|
|||
package com.baeldung.java.listInitialization;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import lombok.extern.java.Log;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@Log
|
||||
public class ListInitializationUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenAnonymousInnerClass_thenInitialiseList() {
|
||||
List<String> cities = new ArrayList() {
|
||||
{
|
||||
add("New York");
|
||||
add("Rio");
|
||||
add("Tokyo");
|
||||
}
|
||||
};
|
||||
|
||||
Assert.assertTrue(cities.contains("New York"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArraysAsList_thenInitialiseList() {
|
||||
List<String> list = Arrays.asList("foo", "bar");
|
||||
|
||||
Assert.assertTrue(list.contains("foo"));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void givenArraysAsList_whenAdd_thenUnsupportedException() {
|
||||
List<String> list = Arrays.asList("foo", "bar");
|
||||
|
||||
list.add("baz");
|
||||
}
|
||||
|
||||
@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));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStream_thenInitializeList() {
|
||||
List<String> list = Stream.of("foo", "bar")
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Assert.assertTrue(list.contains("foo"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
package com.baeldung.java.listinitialization;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
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.Test;
|
||||
|
||||
import lombok.extern.java.Log;
|
||||
|
||||
@Log
|
||||
public class ListInitializationUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenAnonymousInnerClass_thenInitialiseList() {
|
||||
List<String> cities = new ArrayList() {
|
||||
{
|
||||
add("New York");
|
||||
add("Rio");
|
||||
add("Tokyo");
|
||||
}
|
||||
};
|
||||
|
||||
assertTrue(cities.contains("New York"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArraysAsList_thenInitialiseList() {
|
||||
List<String> list = Arrays.asList("foo", "bar");
|
||||
|
||||
assertTrue(list.contains("foo"));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void givenArraysAsList_whenAdd_thenUnsupportedException() {
|
||||
List<String> list = Arrays.asList("foo", "bar");
|
||||
|
||||
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";
|
||||
assertEquals("baz", list.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntNumbers_whenRequiredLong_thenCastAutomatically() {
|
||||
int intNum = 42;
|
||||
long longNum = intNum;
|
||||
|
||||
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);
|
||||
|
||||
assertEquals(expected, listOfLongFixedSize);
|
||||
assertEquals(expected, listOfLong);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStream_thenInitializeList() {
|
||||
List<String> list = Stream.of("foo", "bar")
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertTrue(list.contains("foo"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue