Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-4461-3
This commit is contained in:
commit
4b1aeb776f
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.java10.list;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class CopyListServiceUnitTest {
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void whenModifyCopyOfList_thenThrowsException() {
|
||||
List<Integer> copyList = List.copyOf(Arrays.asList(1, 2, 3, 4));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package com.baeldung.list;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class CopyListService {
|
||||
|
||||
public List<Flower> copyListByConstructor(List<Flower> source) {
|
||||
return new ArrayList<Flower>(source);
|
||||
}
|
||||
|
||||
public List<Flower> copyListByConstructorAndEditOneFlowerInTheNewList(List<Flower> source) {
|
||||
List<Flower> flowers = new ArrayList<>(source);
|
||||
if(flowers.size() > 0) {
|
||||
flowers.get(0).setPetals(flowers.get(0).getPetals() * 3);
|
||||
}
|
||||
|
||||
return flowers;
|
||||
}
|
||||
|
||||
public List<Flower> copyListByAddAllMethod(List<Flower> source) {
|
||||
List<Flower> flowers = new ArrayList<>();
|
||||
flowers.addAll(source);
|
||||
return flowers;
|
||||
}
|
||||
|
||||
public List<Flower> copyListByAddAllMethodAndEditOneFlowerInTheNewList(List<Flower> source) {
|
||||
List<Flower> flowers = new ArrayList<>();
|
||||
flowers.addAll(source);
|
||||
|
||||
if(flowers.size() > 0) {
|
||||
flowers.get(0).setPetals(flowers.get(0).getPetals() * 3);
|
||||
}
|
||||
|
||||
return flowers;
|
||||
}
|
||||
|
||||
public List<Integer> copyListByCopyMethod(List<Integer> source, List<Integer> dest) {
|
||||
Collections.copy(dest, source);
|
||||
return dest;
|
||||
}
|
||||
|
||||
public List<Flower> copyListByStream(List<Flower> source) {
|
||||
return source.stream().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<Flower> copyListByStreamAndSkipFirstElement(List<Flower> source) {
|
||||
return source.stream().skip(1).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<Flower> copyListByStreamWithFilter(List<Flower> source, Integer moreThanPetals) {
|
||||
return source.stream().filter(f -> f.getPetals() > moreThanPetals).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<Flower> copyListByStreamWithOptional(List<Flower> source) {
|
||||
return Optional.ofNullable(source)
|
||||
.map(List::stream)
|
||||
.orElseGet(Stream::empty)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<Flower> copyListByStreamWithOptionalAndSkip(List<Flower> source) {
|
||||
return Optional.ofNullable(source)
|
||||
.map(List::stream)
|
||||
.orElseGet(Stream::empty)
|
||||
.skip(1)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.list;
|
||||
|
||||
public class Flower {
|
||||
|
||||
private String name;
|
||||
private int petals;
|
||||
|
||||
public Flower(String name, int petals) {
|
||||
this.name = name;
|
||||
this.petals = petals;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getPetals() {
|
||||
return petals;
|
||||
}
|
||||
|
||||
public void setPetals(int petals) {
|
||||
this.petals = petals;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,129 @@
|
|||
package com.baeldung.list;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class CopyListServiceUnitTest {
|
||||
|
||||
List<Flower> flowers;
|
||||
|
||||
private CopyListService copyListService;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
this.copyListService = new CopyListService();
|
||||
this.flowers = new ArrayList<>();
|
||||
|
||||
Flower poppy = new Flower("Poppy", 12);
|
||||
flowers.add(poppy);
|
||||
Flower anemone = new Flower("Anemone", 8);
|
||||
flowers.add(anemone);
|
||||
Flower catmint = new Flower("Catmint", 12);
|
||||
flowers.add(catmint);
|
||||
Flower diascia = new Flower("Diascia", 5);
|
||||
flowers.add(diascia);
|
||||
Flower iris = new Flower("Iris", 3);
|
||||
flowers.add(iris);
|
||||
Flower pansy = new Flower("Pansy", 5);
|
||||
flowers.add(pansy);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithTheSameElementsByConstructor() {
|
||||
List<Flower> copy = copyListService.copyListByConstructor(flowers);
|
||||
assertEquals(copy.size(), flowers.size());
|
||||
assertTrue(copy.containsAll(flowers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithOneModifiedElementByConstructor() {
|
||||
List<Flower> copy = copyListService.copyListByConstructorAndEditOneFlowerInTheNewList(flowers);
|
||||
assertEquals(copy.size(), flowers.size());
|
||||
assertTrue(copy.containsAll(flowers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithTheSameElementsByAddAllmethod() {
|
||||
List<Flower> copy = copyListService.copyListByAddAllMethod(flowers);
|
||||
assertEquals(copy.size(), flowers.size());
|
||||
assertTrue(copy.containsAll(flowers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithOneModifiedElementByAddAllmethod() {
|
||||
List<Flower> copy = copyListService.copyListByAddAllMethodAndEditOneFlowerInTheNewList(flowers);
|
||||
assertEquals(copy.size(), flowers.size());
|
||||
assertTrue(copy.containsAll(flowers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenListsHaveSameSize_thenReturnAnotherListWithTheSameElementsByCopyMethod() {
|
||||
List<Integer> source = Arrays.asList(1,2,3);
|
||||
List<Integer> dest = Arrays.asList(4,5,6);
|
||||
|
||||
dest = copyListService.copyListByCopyMethod(source, dest);
|
||||
assertEquals(dest.size(), source.size());
|
||||
assertTrue(dest.containsAll(source));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenListsHaveDifferentSize_thenReturnAnotherListWithTheSameElementsByCopyMethod() {
|
||||
List<Integer> source = Arrays.asList(1,2,3);
|
||||
List<Integer> dest = Arrays.asList(5,6,7,8,9,10);
|
||||
|
||||
dest = copyListService.copyListByCopyMethod(source, dest);
|
||||
assertNotEquals(dest.size(), source.size());
|
||||
assertTrue(dest.containsAll(source));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithTheSameElementsByStreamProcess() {
|
||||
List<Flower> copy = copyListService.copyListByStream(flowers);
|
||||
assertEquals(copy.size(), flowers.size());
|
||||
assertTrue(copy.containsAll(flowers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithOneElementLessByStreamProcess() {
|
||||
List<Flower> copy = copyListService.copyListByStreamAndSkipFirstElement(flowers);
|
||||
assertNotEquals(copy.size(), flowers.size());
|
||||
assertEquals(copy.size() + 1, flowers.size());
|
||||
assertFalse(copy.containsAll(flowers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenListDoesNotHaveNullElements_thenReturnAnotherListWithFilterElementsByStreamProcess() {
|
||||
List<Flower> copy = copyListService.copyListByStreamWithFilter(flowers, 5);
|
||||
assertNotEquals(copy.size(), flowers.size());
|
||||
assertEquals(copy.size() + 3, flowers.size());
|
||||
assertFalse(copy.containsAll(flowers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenListIsNull_thenReturnEmptyListByStreamProcess() {
|
||||
List<Flower> copy = copyListService.copyListByStreamWithOptional(null);
|
||||
assertNotNull(copy);
|
||||
assertEquals(copy.size(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenListIsNotNull_thenReturnAnotherListWithTheElementsByStreamProcess() {
|
||||
List<Flower> copy = copyListService.copyListByStreamWithOptional(flowers);
|
||||
assertEquals(copy.size(), flowers.size());
|
||||
assertTrue(copy.containsAll(flowers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenListIsNotNull_thenReturnAnotherListWithOneElementLessByStreamProcess() {
|
||||
List<Flower> copy = copyListService.copyListByStreamWithOptionalAndSkip(flowers);
|
||||
assertNotEquals(copy.size(), flowers.size());
|
||||
assertEquals(copy.size() + 1, flowers.size());
|
||||
assertFalse(copy.containsAll(flowers));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
package com.baeldung.java.set;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Sets;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class HashSetInitalizingUnitTest {
|
||||
@Test
|
||||
public void whenUsingJava_usingArraysStaticMethod_thenCorrectSize() {
|
||||
Set<String> set = new HashSet<>(Arrays.asList("a", "b", "c"));
|
||||
assertEquals(3, set.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJava_usingAnonymousClass_thenCorrectSize() {
|
||||
Set<String> set = new HashSet<String>(){{
|
||||
add("a");
|
||||
add("b");
|
||||
add("c");
|
||||
}};
|
||||
assertEquals(3, set.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJava_creatingSingletonSet_thenCorrectSize() {
|
||||
Set<String> set = Collections.singleton("a");
|
||||
assertEquals(1, set.size());
|
||||
}
|
||||
|
||||
public static final <T> Set<T> newHashSet(T... objs) {
|
||||
Set<T> set = new HashSet<T>();
|
||||
Collections.addAll(set, objs);
|
||||
return set;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJava_usingCustomStaticUtilMethod_thenCorrectSize() {
|
||||
Set<String> set = newHashSet("a","b","c");
|
||||
assertEquals(3, set.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJava8_usingCollectOnStream_thenCorrectSize() {
|
||||
Set<String> set = Stream.of("a", "b", "c").collect(Collectors.toSet());
|
||||
assertEquals(3, set.size());
|
||||
}
|
||||
@Test
|
||||
public void whenUsingJava8_fromStringArray_thenCorrectSize() {
|
||||
String[] stringArray = {"a","b","c"};
|
||||
Set<String> set = Arrays.stream(stringArray).collect(Collectors.toCollection(HashSet::new));
|
||||
assertEquals(3, set.size());
|
||||
}
|
||||
|
||||
// Requires Java9 - uncomment if you are using Java 9 or higher
|
||||
/*@Test
|
||||
public void whenUsingJava9_usingCollectOnStream_thenCorrectSize() {
|
||||
Set set = Set.of("a", "b", "c");
|
||||
assertEquals(3, set.size());
|
||||
}*/
|
||||
|
||||
@Test
|
||||
public void whenUsingGoogleGuava_createMutableSet_thenCorrectSize() {
|
||||
Set<String> set = Sets.newHashSet("a", "b", "c");
|
||||
assertEquals(3, set.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingGoogleGuava_createImmutableSet_thenCorrectSize() {
|
||||
Set<String> set = ImmutableSet.of("a", "b", "c");
|
||||
assertEquals(3, set.size());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue