From 646c2a32ca7398f662adec7ab48cf59c5c6c9791 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Fri, 21 Apr 2023 16:15:44 -0400 Subject: [PATCH 1/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20core-java-collections-?= =?UTF-8?q?conversions=20=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../README.md | 15 ++ .../core-java-collections-conversions/pom.xml | 41 +++++ .../java/com/baeldung/convertToMap/Book.java | 48 ++++++ .../baeldung/convertToMap/ConvertToMap.java | 30 ++++ .../convertcollectiontoarraylist/Foo.java | 52 +++++++ .../com/baeldung/convertlisttomap/Animal.java | 27 ++++ .../ConvertListToMapService.java | 43 ++++++ .../src/main/resources/logback.xml | 13 ++ .../convertToMap/ConvertToMapUnitTest.java | 53 +++++++ .../CollectionToArrayListUnitTest.java | 145 ++++++++++++++++++ .../ConvertIteratorToListServiceUnitTest.java | 100 ++++++++++++ .../ConvertListToMapServiceUnitTest.java | 68 ++++++++ ...tWithDuplicatedIdToMapServiceUnitTest.java | 68 ++++++++ .../IterableToCollectionUnitTest.java | 122 +++++++++++++++ .../JavaCollectionConversionUnitTest.java | 145 ++++++++++++++++++ .../java/lists/ListToStringUnitTest.java | 31 ++++ core-java-modules/pom.xml | 1 + .../src/main/resources/application.properties | 2 +- 18 files changed, 1003 insertions(+), 1 deletion(-) create mode 100644 core-java-modules/core-java-collections-conversions/README.md create mode 100644 core-java-modules/core-java-collections-conversions/pom.xml create mode 100644 core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/Book.java create mode 100644 core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/ConvertToMap.java create mode 100644 core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertcollectiontoarraylist/Foo.java create mode 100644 core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/Animal.java create mode 100644 core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java create mode 100644 core-java-modules/core-java-collections-conversions/src/main/resources/logback.xml create mode 100644 core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertToMap/ConvertToMapUnitTest.java create mode 100644 core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java create mode 100644 core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java create mode 100644 core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListToMapServiceUnitTest.java create mode 100644 core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListWithDuplicatedIdToMapServiceUnitTest.java create mode 100644 core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/IterableToCollectionUnitTest.java create mode 100644 core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/JavaCollectionConversionUnitTest.java create mode 100644 core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/lists/ListToStringUnitTest.java diff --git a/core-java-modules/core-java-collections-conversions/README.md b/core-java-modules/core-java-collections-conversions/README.md new file mode 100644 index 0000000000..5dd1848ce0 --- /dev/null +++ b/core-java-modules/core-java-collections-conversions/README.md @@ -0,0 +1,15 @@ +## Java Collections Cookbooks and Examples + +This module contains articles about conversions among Collection types and arrays in Java. + +### Relevant Articles: +- [Converting between an Array and a List in Java](https://www.baeldung.com/convert-array-to-list-and-list-to-array) +- [Converting Between an Array and a Set in Java](https://www.baeldung.com/convert-array-to-set-and-set-to-array) +- [Convert a Map to an Array, List or Set in Java](https://www.baeldung.com/convert-map-values-to-array-list-set) +- [Converting a List to String in Java](https://www.baeldung.com/java-list-to-string) +- [How to Convert List to Map in Java](https://www.baeldung.com/java-list-to-map) +- [Converting a Collection to ArrayList in Java](https://www.baeldung.com/java-convert-collection-arraylist) +- [Java 8 Collectors toMap](https://www.baeldung.com/java-collectors-tomap) +- [Converting Iterable to Collection in Java](https://www.baeldung.com/java-iterable-to-collection) +- [Converting Iterator to List](https://www.baeldung.com/java-convert-iterator-to-list) +- More articles: [[next -->]](../core-java-collections-conversions-2) diff --git a/core-java-modules/core-java-collections-conversions/pom.xml b/core-java-modules/core-java-collections-conversions/pom.xml new file mode 100644 index 0000000000..8be96c6ee7 --- /dev/null +++ b/core-java-modules/core-java-collections-conversions/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + core-java-collections-conversions + 0.1.0-SNAPSHOT + core-java-collections-conversions + jar + + + com.ossez.core-java-modules + core-java-modules + 0.0.2-SNAPSHOT + ../pom.xml + + + + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + + + core-java-collections-conversions + + + src/main/resources + true + + + + + \ No newline at end of file diff --git a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/Book.java b/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/Book.java new file mode 100644 index 0000000000..847e0bd8cd --- /dev/null +++ b/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/Book.java @@ -0,0 +1,48 @@ +package com.baeldung.convertToMap; + +public class Book { + private String name; + private int releaseYear; + private String isbn; + + + @Override + public String toString() { + return "Book{" + + "name='" + name + '\'' + + ", releaseYear=" + releaseYear + + ", isbn='" + isbn + '\'' + + '}'; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getReleaseYear() { + return releaseYear; + } + + public void setReleaseYear(int releaseYear) { + this.releaseYear = releaseYear; + } + + public String getIsbn() { + return isbn; + } + + public void setIsbn(String isbn) { + this.isbn = isbn; + } + + public Book(String name, int releaseYear, String isbn) { + this.name = name; + this.releaseYear = releaseYear; + this.isbn = isbn; + } +} + diff --git a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/ConvertToMap.java b/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/ConvertToMap.java new file mode 100644 index 0000000000..317cdca529 --- /dev/null +++ b/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/ConvertToMap.java @@ -0,0 +1,30 @@ +package com.baeldung.convertToMap; + +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; +import java.util.stream.Collectors; + +public class ConvertToMap { + public Map listToMap(List books) { + return books.stream().collect(Collectors.toMap(Book::getIsbn, Book::getName)); + } + + public Map listToMapWithDupKeyError(List books) { + return books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity())); + } + + public Map listToMapWithDupKey(List books) { + return books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity(), (existing, replacement) -> existing)); + } + + public Map listToConcurrentMap(List books) { + return books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity(), (o1, o2) -> o1, ConcurrentHashMap::new)); + } + + public TreeMap listToSortedMap(List books) { + return books.stream() + .collect(Collectors.toMap(Book::getName, Function.identity(), (o1, o2) -> o1, TreeMap::new)); + } +} + diff --git a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertcollectiontoarraylist/Foo.java b/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertcollectiontoarraylist/Foo.java new file mode 100644 index 0000000000..5c9464182e --- /dev/null +++ b/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertcollectiontoarraylist/Foo.java @@ -0,0 +1,52 @@ +package com.baeldung.convertcollectiontoarraylist; + +/** + * This POJO is the element type of our collection. It has a deepCopy() method. + * + * @author chris + */ +public class Foo { + + private int id; + private String name; + private Foo parent; + + public Foo() { + } + + public Foo(int id, String name, Foo parent) { + this.id = id; + this.name = name; + this.parent = parent; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Foo getParent() { + return parent; + } + + public void setParent(Foo parent) { + this.parent = parent; + } + + public Foo deepCopy() { + return new Foo( + this.id, this.name, this.parent != null ? this.parent.deepCopy() : null); + } + +} diff --git a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/Animal.java b/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/Animal.java new file mode 100644 index 0000000000..b8eddf71a5 --- /dev/null +++ b/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/Animal.java @@ -0,0 +1,27 @@ +package com.baeldung.convertlisttomap; + +public class Animal { + private int id; + private String name; + + public Animal(int id, String name) { + this.id = id; + this.setName(name); + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java b/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java new file mode 100644 index 0000000000..57579e948f --- /dev/null +++ b/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java @@ -0,0 +1,43 @@ +package com.baeldung.convertlisttomap; + +import com.google.common.collect.Maps; +import org.apache.commons.collections4.MapUtils; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +public class ConvertListToMapService { + + public Map convertListBeforeJava8(List list) { + + Map map = new HashMap<>(); + + for (Animal animal : list) { + map.put(animal.getId(), animal); + } + return map; + } + + public Map convertListAfterJava8(List list) { + Map map = list.stream().collect(Collectors.toMap(Animal::getId, Function.identity())); + return map; + } + + public Map convertListWithGuava(List list) { + + Map map = Maps.uniqueIndex(list, Animal::getId); + return map; + } + + public Map convertListWithApacheCommons(List list) { + + Map map = new HashMap<>(); + + MapUtils.populateMap(map, list, Animal::getId); + + return map; + } +} diff --git a/core-java-modules/core-java-collections-conversions/src/main/resources/logback.xml b/core-java-modules/core-java-collections-conversions/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/core-java-modules/core-java-collections-conversions/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertToMap/ConvertToMapUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertToMap/ConvertToMapUnitTest.java new file mode 100644 index 0000000000..d6eab461d7 --- /dev/null +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertToMap/ConvertToMapUnitTest.java @@ -0,0 +1,53 @@ +package com.baeldung.convertToMap; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + + +public class ConvertToMapUnitTest { + + private List bookList; + private ConvertToMap convertToMap = new ConvertToMap(); + + @Before + public void init() { + bookList = new ArrayList<>(); + bookList.add(new Book("The Fellowship of the Ring", 1954, "0395489318")); + bookList.add(new Book("The Two Towers", 1954, "0345339711")); + bookList.add(new Book("The Return of the King", 1955, "0618129111")); + } + + @Test + public void whenConvertFromListToMap() { + assertTrue(convertToMap.listToMap(bookList).size() == 3); + } + + @Test(expected = IllegalStateException.class) + public void whenMapHasDuplicateKey_without_merge_function_then_runtime_exception() { + convertToMap.listToMapWithDupKeyError(bookList); + } + + @Test + public void whenMapHasDuplicateKeyThenMergeFunctionHandlesCollision() { + Map booksByYear = convertToMap.listToMapWithDupKey(bookList); + assertEquals(2, booksByYear.size()); + assertEquals("0395489318", booksByYear.get(1954).getIsbn()); + } + + @Test + public void whenCreateConcurrentHashMap() { + assertTrue(convertToMap.listToConcurrentMap(bookList) instanceof ConcurrentHashMap); + } + + @Test + public void whenMapisSorted() { + assertTrue(convertToMap.listToSortedMap(bookList).firstKey().equals("The Fellowship of the Ring")); + } +} diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java new file mode 100644 index 0000000000..b8134de08a --- /dev/null +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java @@ -0,0 +1,145 @@ +package com.baeldung.convertcollectiontoarraylist; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Comparator; +import java.util.HashSet; +import java.util.Iterator; +import static java.util.stream.Collectors.toCollection; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author chris + */ +public class CollectionToArrayListUnitTest { + private static Collection srcCollection = new HashSet<>(); + + public CollectionToArrayListUnitTest() { + } + + @BeforeClass + public static void setUpClass() { + int i = 0; + Foo john = new Foo(i++, "John", null); + Foo mary = new Foo(i++, "Mary", null); + Foo sam = new Foo(i++, "Sam", john); + Foo alice = new Foo(i++, "Alice", john); + Foo buffy = new Foo(i++, "Buffy", sam); + srcCollection.add(john); + srcCollection.add(mary); + srcCollection.add(sam); + srcCollection.add(alice); + srcCollection.add(buffy); + } + + /** + * Section 3. Using the ArrayList Constructor + */ + @Test + public void whenUsingConstructor_thenVerifyShallowCopy() { + ArrayList newList = new ArrayList<>(srcCollection); + verifyShallowCopy(srcCollection, newList); + } + + /** + * Section 4. Using the Streams API + */ + @Test + public void whenUsingStream_thenVerifyShallowCopy() { + ArrayList newList = srcCollection.stream().collect(toCollection(ArrayList::new)); + + verifyShallowCopy(srcCollection, newList); + } + + /** + * Section 5. Deep Copy + */ + @Test + public void whenUsingDeepCopy_thenVerifyDeepCopy() { + ArrayList newList = srcCollection.stream() + .map(Foo::deepCopy) + .collect(toCollection(ArrayList::new)); + + verifyDeepCopy(srcCollection, newList); + } + + /** + * Section 6. Controlling the List Order + */ + @Test + public void whenUsingSortedStream_thenVerifySortOrder() { + ArrayList newList = srcCollection.stream() + .sorted(Comparator.comparing(Foo::getName)) + .collect(toCollection(ArrayList::new)); + + assertTrue("ArrayList is not sorted by name", isSorted(newList)); + } + + /** + * Verify that the contents of the two collections are the same + * @param a + * @param b + */ + private void verifyShallowCopy(Collection a, Collection b) { + assertEquals("Collections have different lengths", a.size(), b.size()); + Iterator iterA = a.iterator(); + Iterator iterB = b.iterator(); + while (iterA.hasNext()) { + // test instance identity + assertSame("Foo instances differ!", iterA.next(), iterB.next()); + } + } + + /** + * Verify that the contents of the two collections are the same + * @param a + * @param b + */ + private void verifyDeepCopy(Collection a, Collection b) { + assertEquals("Collections have different lengths", a.size(), b.size()); + Iterator iterA = a.iterator(); + Iterator iterB = b.iterator(); + while (iterA.hasNext()) { + Foo nextA = iterA.next(); + Foo nextB = iterB.next(); + // should not be same instance + assertNotSame("Foo instances are the same!", nextA, nextB); + // but should have same content + assertFalse("Foo instances have different content!", fooDiff(nextA, nextB)); + } + } + + /** + * Return true if the contents of a and b differ. Test parent recursively + * @param a + * @param b + * @return False if the two items are the same + */ + private boolean fooDiff(Foo a, Foo b) { + if (a != null && b != null) { + return a.getId() != b.getId() + || !a.getName().equals(b.getName()) + || fooDiff(a.getParent(), b.getParent()); + } + return !(a == null && b == null); + } + + /** + * @param c collection of Foo + * @return true if the collection is sorted by name + */ + private static boolean isSorted(Collection c) { + String prevName = null; + for (Foo foo : c) { + if (prevName == null || foo.getName().compareTo(prevName) > 0) { + prevName = foo.getName(); + } else { + return false; + } + } + return true; + } +} diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java new file mode 100644 index 0000000000..7d94f88d21 --- /dev/null +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java @@ -0,0 +1,100 @@ +package com.baeldung.convertiteratortolist; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +import org.apache.commons.collections4.IteratorUtils; +import org.junit.Before; +import org.junit.Test; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; + +public class ConvertIteratorToListServiceUnitTest { + + Iterator iterator; + + @Before + public void setUp() { + iterator = Arrays.asList(1, 2, 3) + .iterator(); + } + + @Test + public void givenAnIterator_whenConvertIteratorToListUsingWhileLoop_thenReturnAList() { + + List actualList = new ArrayList<>(); + + // Convert Iterator to List using while loop dsf + while (iterator.hasNext()) { + actualList.add(iterator.next()); + } + + assertThat(actualList, hasSize(3)); + assertThat(actualList, containsInAnyOrder(1, 2, 3)); + } + + @Test + public void givenAnIterator_whenConvertIteratorToListAfterJava8_thenReturnAList() { + List actualList = new ArrayList<>(); + + // Convert Iterator to List using Java 8 + iterator.forEachRemaining(actualList::add); + + assertThat(actualList, hasSize(3)); + assertThat(actualList, containsInAnyOrder(1, 2, 3)); + } + + @Test + public void givenAnIterator_whenConvertIteratorToListJava8Stream_thenReturnAList() { + + // Convert iterator to iterable + Iterable iterable = () -> iterator; + + // Extract List from stream + List actualList = StreamSupport + .stream(iterable.spliterator(), false) + .collect(Collectors.toList()); + + assertThat(actualList, hasSize(3)); + assertThat(actualList, containsInAnyOrder(1, 2, 3)); + } + + @Test + public void givenAnIterator_whenConvertIteratorToImmutableListWithGuava_thenReturnAList() { + + // Convert Iterator to an Immutable list using Guava library in Java + List actualList = ImmutableList.copyOf(iterator); + + assertThat(actualList, hasSize(3)); + assertThat(actualList, containsInAnyOrder(1, 2, 3)); + } + + @Test + public void givenAnIterator_whenConvertIteratorToMutableListWithGuava_thenReturnAList() { + + // Convert Iterator to a mutable list using Guava library in Java + List actualList = Lists.newArrayList(iterator); + + assertThat(actualList, hasSize(3)); + assertThat(actualList, containsInAnyOrder(1, 2, 3)); + } + + @Test + public void givenAnIterator_whenConvertIteratorToMutableListWithApacheCommons_thenReturnAList() { + + // Convert Iterator to a mutable list using Apache Commons library in Java + List actualList = IteratorUtils.toList(iterator); + + assertThat(actualList, hasSize(3)); + assertThat(actualList, containsInAnyOrder(1, 2, 3)); + } +} diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListToMapServiceUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListToMapServiceUnitTest.java new file mode 100644 index 0000000000..4d783f4525 --- /dev/null +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListToMapServiceUnitTest.java @@ -0,0 +1,68 @@ +package com.baeldung.convertlisttomap; + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; + +public class ConvertListToMapServiceUnitTest { + List list; + + private ConvertListToMapService convertListService; + + @Before + public void init() { + this.convertListService = new ConvertListToMapService(); + this.list = new ArrayList<>(); + + Animal cat = new Animal(1, "Cat"); + list.add(cat); + Animal dog = new Animal(2, "Dog"); + list.add(dog); + Animal pig = new Animal(3, "Pig"); + list.add(pig); + Animal cow = new Animal(4, "Cow"); + list.add(cow); + Animal goat = new Animal(5, "Goat"); + list.add(goat); + + } + + @Test + public void givenAList_whenConvertBeforeJava8_thenReturnMapWithTheSameElements() { + + Map map = convertListService.convertListBeforeJava8(list); + + assertThat(map.values(), containsInAnyOrder(list.toArray())); + } + + @Test + public void givenAList_whenConvertAfterJava8_thenReturnMapWithTheSameElements() { + + Map map = convertListService.convertListAfterJava8(list); + + assertThat(map.values(), containsInAnyOrder(list.toArray())); + } + + @Test + public void givenAList_whenConvertWithGuava_thenReturnMapWithTheSameElements() { + + Map map = convertListService.convertListWithGuava(list); + + assertThat(map.values(), containsInAnyOrder(list.toArray())); + } + + @Test + public void givenAList_whenConvertWithApacheCommons_thenReturnMapWithTheSameElements() { + + Map map = convertListService.convertListWithApacheCommons(list); + + assertThat(map.values(), containsInAnyOrder(list.toArray())); + } + +} diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListWithDuplicatedIdToMapServiceUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListWithDuplicatedIdToMapServiceUnitTest.java new file mode 100644 index 0000000000..5e6828a3d5 --- /dev/null +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListWithDuplicatedIdToMapServiceUnitTest.java @@ -0,0 +1,68 @@ +package com.baeldung.convertlisttomap; + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasSize; + +public class ConvertListWithDuplicatedIdToMapServiceUnitTest { + List duplicatedIdList; + + private ConvertListToMapService convertListService = new ConvertListToMapService(); + + @Before + public void init() { + + this.duplicatedIdList = new ArrayList<>(); + + Animal cat = new Animal(1, "Cat"); + duplicatedIdList.add(cat); + Animal dog = new Animal(2, "Dog"); + duplicatedIdList.add(dog); + Animal pig = new Animal(3, "Pig"); + duplicatedIdList.add(pig); + Animal cow = new Animal(4, "Cow"); + duplicatedIdList.add(cow); + Animal goat = new Animal(4, "Goat"); + duplicatedIdList.add(goat); + + } + + @Test + public void givenADupIdList_whenConvertBeforeJava8_thenReturnMapWithRewrittenElement() { + + Map map = convertListService.convertListBeforeJava8(duplicatedIdList); + + assertThat(map.values(), hasSize(4)); + assertThat(map.values(), hasItem(duplicatedIdList.get(4))); + } + + @Test + public void givenADupIdList_whenConvertWithApacheCommons_thenReturnMapWithRewrittenElement() { + + Map map = convertListService.convertListWithApacheCommons(duplicatedIdList); + + assertThat(map.values(), hasSize(4)); + assertThat(map.values(), hasItem(duplicatedIdList.get(4))); + } + + @Test(expected = IllegalStateException.class) + public void givenADupIdList_whenConvertAfterJava8_thenException() { + + convertListService.convertListAfterJava8(duplicatedIdList); + } + + @Test(expected = IllegalArgumentException.class) + public void givenADupIdList_whenConvertWithGuava_thenException() { + + convertListService.convertListWithGuava(duplicatedIdList); + + } + +} diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/IterableToCollectionUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/IterableToCollectionUnitTest.java new file mode 100644 index 0000000000..0283191b74 --- /dev/null +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/IterableToCollectionUnitTest.java @@ -0,0 +1,122 @@ +package com.baeldung.java.collections; + +import static org.hamcrest.Matchers.contains; +import static org.junit.Assert.assertThat; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Spliterator; +import java.util.Spliterators; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +import org.apache.commons.collections4.IterableUtils; +import org.apache.commons.collections4.IteratorUtils; +import org.junit.Test; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; + +public class IterableToCollectionUnitTest { + + Iterable iterable = Arrays.asList("john", "tom", "jane"); + Iterator iterator = iterable.iterator(); + + @Test + public void whenConvertIterableToListUsingJava_thenSuccess() { + List result = new ArrayList(); + for (String str : iterable) { + result.add(str); + } + + assertThat(result, contains("john", "tom", "jane")); + } + + @Test + public void whenConvertIterableToListUsingJava8_thenSuccess() { + List result = new ArrayList(); + iterable.forEach(result::add); + + assertThat(result, contains("john", "tom", "jane")); + } + + @Test + public void whenConvertIterableToListUsingJava8WithSpliterator_thenSuccess() { + List result = StreamSupport.stream(iterable.spliterator(), false) + .collect(Collectors.toList()); + + assertThat(result, contains("john", "tom", "jane")); + } + + @Test + public void whenConvertIterableToListUsingGuava_thenSuccess() { + List result = Lists.newArrayList(iterable); + + assertThat(result, contains("john", "tom", "jane")); + } + + @Test + public void whenConvertIterableToImmutableListUsingGuava_thenSuccess() { + List result = ImmutableList.copyOf(iterable); + + assertThat(result, contains("john", "tom", "jane")); + } + + @Test + public void whenConvertIterableToListUsingApacheCommons_thenSuccess() { + List result = IterableUtils.toList(iterable); + + assertThat(result, contains("john", "tom", "jane")); + } + + // ======================== Iterator + + @Test + public void whenConvertIteratorToListUsingJava_thenSuccess() { + List result = new ArrayList(); + while (iterator.hasNext()) { + result.add(iterator.next()); + } + + assertThat(result, contains("john", "tom", "jane")); + } + + @Test + public void whenConvertIteratorToListUsingJava8_thenSuccess() { + List result = new ArrayList(); + iterator.forEachRemaining(result::add); + + assertThat(result, contains("john", "tom", "jane")); + } + + @Test + public void whenConvertIteratorToListUsingJava8WithSpliterator_thenSuccess() { + List result = StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false) + .collect(Collectors.toList()); + + assertThat(result, contains("john", "tom", "jane")); + } + + @Test + public void whenConvertIteratorToListUsingGuava_thenSuccess() { + List result = Lists.newArrayList(iterator); + + assertThat(result, contains("john", "tom", "jane")); + } + + @Test + public void whenConvertIteratorToImmutableListUsingGuava_thenSuccess() { + List result = ImmutableList.copyOf(iterator); + + assertThat(result, contains("john", "tom", "jane")); + } + + @Test + public void whenConvertIteratorToListUsingApacheCommons_thenSuccess() { + List result = IteratorUtils.toList(iterator); + + assertThat(result, contains("john", "tom", "jane")); + } +} diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/JavaCollectionConversionUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/JavaCollectionConversionUnitTest.java new file mode 100644 index 0000000000..7947d1b6c7 --- /dev/null +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/JavaCollectionConversionUnitTest.java @@ -0,0 +1,145 @@ +package com.baeldung.java.collections; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.ArrayUtils; +import org.junit.Test; + +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +import com.google.common.primitives.Ints; + +@SuppressWarnings("unused") +public class JavaCollectionConversionUnitTest { + + // List -> array; array -> List + + @Test + public final void givenUsingCoreJava_whenArrayConvertedToList_thenCorrect() { + final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final List targetList = Arrays.asList(sourceArray); + } + + @Test + public final void givenUsingCoreJava_whenListConvertedToArray_thenCorrect() { + final List sourceList = Arrays.asList(0, 1, 2, 3, 4, 5); + final Integer[] targetArray = sourceList.toArray(new Integer[0]); + } + + @Test + public final void givenUsingGuava_whenArrayConvertedToList_thenCorrect() { + final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final List targetList = Lists.newArrayList(sourceArray); + } + + @Test + public final void givenUsingGuava_whenListConvertedToArray_thenCorrect() { + final List sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5); + final int[] targetArray = Ints.toArray(sourceList); + } + + @Test + public final void givenUsingCommonsCollections_whenArrayConvertedToList_thenCorrect() { + final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final List targetList = new ArrayList<>(6); + CollectionUtils.addAll(targetList, sourceArray); + } + + // Set -> array; array -> Set + + @Test + public final void givenUsingCoreJavaV1_whenArrayConvertedToSet_thenCorrect() { + final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Set targetSet = new HashSet(Arrays.asList(sourceArray)); + } + + @Test + public final void givenUsingCoreJavaV2_whenArrayConvertedToSet_thenCorrect() { + final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Set targetSet = new HashSet(); + Collections.addAll(targetSet, sourceArray); + } + + @Test + public final void givenUsingCoreJava_whenSetConvertedToArray_thenCorrect() { + final Set sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5); + final Integer[] targetArray = sourceSet.toArray(new Integer[0]); + } + + @Test + public final void givenUsingGuava_whenArrayConvertedToSet_thenCorrect() { + final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Set targetSet = Sets.newHashSet(sourceArray); + } + + @Test + public final void givenUsingGuava_whenSetConvertedToArray_thenCorrect() { + final Set sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5); + final int[] targetArray = Ints.toArray(sourceSet); + } + + @Test + public final void givenUsingCommonsCollections_whenArrayConvertedToSet_thenCorrect() { + final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Set targetSet = new HashSet<>(6); + CollectionUtils.addAll(targetSet, sourceArray); + } + + @Test + public final void givenUsingCommonsCollections_whenSetConvertedToArrayOfPrimitives_thenCorrect() { + final Set sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5); + final Integer[] targetArray = sourceSet.toArray(new Integer[0]); + final int[] primitiveTargetArray = ArrayUtils.toPrimitive(targetArray); + } + + // Map (values) -> Array, List, Set + + @Test + public final void givenUsingCoreJava_whenMapValuesConvertedToArray_thenCorrect() { + final Map sourceMap = createMap(); + + final Collection values = sourceMap.values(); + final String[] targetArray = values.toArray(new String[0]); + } + + @Test + public final void givenUsingCoreJava_whenMapValuesConvertedToList_thenCorrect() { + final Map sourceMap = createMap(); + + final List targetList = new ArrayList<>(sourceMap.values()); + } + + @Test + public final void givenUsingGuava_whenMapValuesConvertedToList_thenCorrect() { + final Map sourceMap = createMap(); + + final List targetList = Lists.newArrayList(sourceMap.values()); + } + + @Test + public final void givenUsingCoreJava_whenMapValuesConvertedToSet_thenCorrect() { + final Map sourceMap = createMap(); + + final Set targetSet = new HashSet<>(sourceMap.values()); + } + + // UTIL + + private final Map createMap() { + final Map sourceMap = new HashMap<>(3); + sourceMap.put(0, "zero"); + sourceMap.put(1, "one"); + sourceMap.put(2, "two"); + return sourceMap; + } + +} diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/lists/ListToStringUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/lists/ListToStringUnitTest.java new file mode 100644 index 0000000000..0fdc096d14 --- /dev/null +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/lists/ListToStringUnitTest.java @@ -0,0 +1,31 @@ +package com.baeldung.java.lists; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import org.apache.commons.lang3.StringUtils; +import org.junit.Test; + +public class ListToStringUnitTest { + + @Test + public void whenListToString_thenPrintDefault() { + List intLIst = Arrays.asList(1, 2, 3); + System.out.println(intLIst); + } + + @Test + public void whenCollectorsJoining_thenPrintCustom() { + List intList = Arrays.asList(1, 2, 3); + System.out.println(intList.stream() + .map(n -> String.valueOf(n)) + .collect(Collectors.joining("-", "{", "}"))); + } + + @Test + public void whenStringUtilsJoin_thenPrintCustom() { + List intList = Arrays.asList(1, 2, 3); + System.out.println(StringUtils.join(intList, "|")); + } +} diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 3e7854fbc2..fd197093eb 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -24,6 +24,7 @@ core-java-collections core-java-collections-2 core-java-collections-3 + core-java-collections-conversions core-java-collections-list core-java-collections-list-2 core-java-collections-list-3 diff --git a/persistence-modules/spring-data-jpa-repo-2/src/main/resources/application.properties b/persistence-modules/spring-data-jpa-repo-2/src/main/resources/application.properties index cb1eab9443..024d51bc26 100644 --- a/persistence-modules/spring-data-jpa-repo-2/src/main/resources/application.properties +++ b/persistence-modules/spring-data-jpa-repo-2/src/main/resources/application.properties @@ -1,3 +1,3 @@ spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1 spring.datasource.username=sa -spring.datasource.password=sa \ No newline at end of file +spring.datasource.password=sa From f482b249aa21d187294b0d89492430abd4ab8c7f Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Fri, 21 Apr 2023 16:15:50 -0400 Subject: [PATCH 2/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20core-java-collections-?= =?UTF-8?q?conversions=20=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/compiler.xml | 1 + .idea/encodings.xml | 2 ++ 2 files changed, 3 insertions(+) diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 9c02d6075b..0a977aecaa 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -21,6 +21,7 @@ + diff --git a/.idea/encodings.xml b/.idea/encodings.xml index 20d0f1e3d8..dc58147bcb 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -15,6 +15,8 @@ + + From b7e57d12400e5d6012ae7dd26786904cc0e45caa Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Fri, 21 Apr 2023 16:35:42 -0400 Subject: [PATCH 3/5] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20Java=20=E6=8A=8A=20Map?= =?UTF-8?q?=20=E7=9A=84=E5=80=BC=EF=BC=88Value=EF=BC=89=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E4=B8=BA=20Array,=20List=20=E6=88=96=20Set=20https://www.ossez?= =?UTF-8?q?.com/t/java-map-value-array-list-set/14388?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core-java-collections-conversions/README.md | 8 ++++---- .../{baeldung => ossez}/convertToMap/Book.java | 2 +- .../convertToMap/ConvertToMap.java | 2 +- .../convertcollectiontoarraylist/Foo.java | 2 +- .../convertlisttomap/Animal.java | 2 +- .../ConvertListToMapService.java | 2 +- .../convertToMap/ConvertToMapUnitTest.java | 2 +- .../CollectionToArrayListUnitTest.java | 2 +- .../ConvertIteratorToListServiceUnitTest.java | 2 +- .../ConvertListToMapServiceUnitTest.java | 2 +- ...ListWithDuplicatedIdToMapServiceUnitTest.java | 7 ++++--- .../IterableToCollectionUnitTest.java | 2 +- .../JavaCollectionConversionUnitTest.java | 16 ++++++++-------- .../java/lists/ListToStringUnitTest.java | 2 +- 14 files changed, 27 insertions(+), 26 deletions(-) rename core-java-modules/core-java-collections-conversions/src/main/java/com/{baeldung => ossez}/convertToMap/Book.java (96%) rename core-java-modules/core-java-collections-conversions/src/main/java/com/{baeldung => ossez}/convertToMap/ConvertToMap.java (97%) rename core-java-modules/core-java-collections-conversions/src/main/java/com/{baeldung => ossez}/convertcollectiontoarraylist/Foo.java (94%) rename core-java-modules/core-java-collections-conversions/src/main/java/com/{baeldung => ossez}/convertlisttomap/Animal.java (91%) rename core-java-modules/core-java-collections-conversions/src/main/java/com/{baeldung => ossez}/convertlisttomap/ConvertListToMapService.java (96%) rename core-java-modules/core-java-collections-conversions/src/test/java/com/{baeldung => ossez}/convertToMap/ConvertToMapUnitTest.java (97%) rename core-java-modules/core-java-collections-conversions/src/test/java/com/{baeldung => ossez}/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java (98%) rename core-java-modules/core-java-collections-conversions/src/test/java/com/{baeldung => ossez}/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java (98%) rename core-java-modules/core-java-collections-conversions/src/test/java/com/{baeldung => ossez}/convertlisttomap/ConvertListToMapServiceUnitTest.java (97%) rename core-java-modules/core-java-collections-conversions/src/test/java/com/{baeldung => ossez}/convertlisttomap/ConvertListWithDuplicatedIdToMapServiceUnitTest.java (89%) rename core-java-modules/core-java-collections-conversions/src/test/java/com/{baeldung => ossez}/java/collections/IterableToCollectionUnitTest.java (98%) rename core-java-modules/core-java-collections-conversions/src/test/java/com/{baeldung => ossez}/java/collections/JavaCollectionConversionUnitTest.java (90%) rename core-java-modules/core-java-collections-conversions/src/test/java/com/{baeldung => ossez}/java/lists/ListToStringUnitTest.java (96%) diff --git a/core-java-modules/core-java-collections-conversions/README.md b/core-java-modules/core-java-collections-conversions/README.md index 5dd1848ce0..c4050c49dd 100644 --- a/core-java-modules/core-java-collections-conversions/README.md +++ b/core-java-modules/core-java-collections-conversions/README.md @@ -1,11 +1,11 @@ -## Java Collections Cookbooks and Examples +## Java 集合(Collections)相关文章和实例 -This module contains articles about conversions among Collection types and arrays in Java. +本模块中的内容包含有 Java 集合(Collections)转换相关的方法。 -### Relevant Articles: +### 相关文章 - [Converting between an Array and a List in Java](https://www.baeldung.com/convert-array-to-list-and-list-to-array) - [Converting Between an Array and a Set in Java](https://www.baeldung.com/convert-array-to-set-and-set-to-array) -- [Convert a Map to an Array, List or Set in Java](https://www.baeldung.com/convert-map-values-to-array-list-set) +- [Java 把 Map 的值(Value)转换为 Array, List 或 Set](https://www.ossez.com/t/java-map-value-array-list-set/14388) - [Converting a List to String in Java](https://www.baeldung.com/java-list-to-string) - [How to Convert List to Map in Java](https://www.baeldung.com/java-list-to-map) - [Converting a Collection to ArrayList in Java](https://www.baeldung.com/java-convert-collection-arraylist) diff --git a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/Book.java b/core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertToMap/Book.java similarity index 96% rename from core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/Book.java rename to core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertToMap/Book.java index 847e0bd8cd..2a4e969ae6 100644 --- a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/Book.java +++ b/core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertToMap/Book.java @@ -1,4 +1,4 @@ -package com.baeldung.convertToMap; +package com.ossez.convertToMap; public class Book { private String name; diff --git a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/ConvertToMap.java b/core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertToMap/ConvertToMap.java similarity index 97% rename from core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/ConvertToMap.java rename to core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertToMap/ConvertToMap.java index 317cdca529..61393a0ac5 100644 --- a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/ConvertToMap.java +++ b/core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertToMap/ConvertToMap.java @@ -1,4 +1,4 @@ -package com.baeldung.convertToMap; +package com.ossez.convertToMap; import java.util.*; import java.util.concurrent.ConcurrentHashMap; diff --git a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertcollectiontoarraylist/Foo.java b/core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertcollectiontoarraylist/Foo.java similarity index 94% rename from core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertcollectiontoarraylist/Foo.java rename to core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertcollectiontoarraylist/Foo.java index 5c9464182e..9a54f000bf 100644 --- a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertcollectiontoarraylist/Foo.java +++ b/core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertcollectiontoarraylist/Foo.java @@ -1,4 +1,4 @@ -package com.baeldung.convertcollectiontoarraylist; +package com.ossez.convertcollectiontoarraylist; /** * This POJO is the element type of our collection. It has a deepCopy() method. diff --git a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/Animal.java b/core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertlisttomap/Animal.java similarity index 91% rename from core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/Animal.java rename to core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertlisttomap/Animal.java index b8eddf71a5..c8d9fdfcaf 100644 --- a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/Animal.java +++ b/core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertlisttomap/Animal.java @@ -1,4 +1,4 @@ -package com.baeldung.convertlisttomap; +package com.ossez.convertlisttomap; public class Animal { private int id; diff --git a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java b/core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertlisttomap/ConvertListToMapService.java similarity index 96% rename from core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java rename to core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertlisttomap/ConvertListToMapService.java index 57579e948f..d162183f56 100644 --- a/core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java +++ b/core-java-modules/core-java-collections-conversions/src/main/java/com/ossez/convertlisttomap/ConvertListToMapService.java @@ -1,4 +1,4 @@ -package com.baeldung.convertlisttomap; +package com.ossez.convertlisttomap; import com.google.common.collect.Maps; import org.apache.commons.collections4.MapUtils; diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertToMap/ConvertToMapUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertToMap/ConvertToMapUnitTest.java similarity index 97% rename from core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertToMap/ConvertToMapUnitTest.java rename to core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertToMap/ConvertToMapUnitTest.java index d6eab461d7..745a801698 100644 --- a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertToMap/ConvertToMapUnitTest.java +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertToMap/ConvertToMapUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.convertToMap; +package com.ossez.convertToMap; import java.util.ArrayList; import java.util.List; diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java similarity index 98% rename from core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java rename to core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java index b8134de08a..be470671a7 100644 --- a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.convertcollectiontoarraylist; +package com.ossez.convertcollectiontoarraylist; import java.util.ArrayList; import java.util.Collection; diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java similarity index 98% rename from core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java rename to core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java index 7d94f88d21..97ba2e4326 100644 --- a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.convertiteratortolist; +package com.ossez.convertiteratortolist; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.collection.IsCollectionWithSize.hasSize; diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListToMapServiceUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertlisttomap/ConvertListToMapServiceUnitTest.java similarity index 97% rename from core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListToMapServiceUnitTest.java rename to core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertlisttomap/ConvertListToMapServiceUnitTest.java index 4d783f4525..e538cfc9b9 100644 --- a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListToMapServiceUnitTest.java +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertlisttomap/ConvertListToMapServiceUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.convertlisttomap; +package com.ossez.convertlisttomap; import org.junit.Before; import org.junit.Test; diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListWithDuplicatedIdToMapServiceUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertlisttomap/ConvertListWithDuplicatedIdToMapServiceUnitTest.java similarity index 89% rename from core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListWithDuplicatedIdToMapServiceUnitTest.java rename to core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertlisttomap/ConvertListWithDuplicatedIdToMapServiceUnitTest.java index 5e6828a3d5..c8f61814fb 100644 --- a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListWithDuplicatedIdToMapServiceUnitTest.java +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/convertlisttomap/ConvertListWithDuplicatedIdToMapServiceUnitTest.java @@ -1,5 +1,6 @@ -package com.baeldung.convertlisttomap; +package com.ossez.convertlisttomap; +import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Test; @@ -40,7 +41,7 @@ public class ConvertListWithDuplicatedIdToMapServiceUnitTest { Map map = convertListService.convertListBeforeJava8(duplicatedIdList); assertThat(map.values(), hasSize(4)); - assertThat(map.values(), hasItem(duplicatedIdList.get(4))); + assertThat(map.values(), Matchers.hasItem(duplicatedIdList.get(4))); } @Test @@ -49,7 +50,7 @@ public class ConvertListWithDuplicatedIdToMapServiceUnitTest { Map map = convertListService.convertListWithApacheCommons(duplicatedIdList); assertThat(map.values(), hasSize(4)); - assertThat(map.values(), hasItem(duplicatedIdList.get(4))); + assertThat(map.values(), Matchers.hasItem(duplicatedIdList.get(4))); } @Test(expected = IllegalStateException.class) diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/IterableToCollectionUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/java/collections/IterableToCollectionUnitTest.java similarity index 98% rename from core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/IterableToCollectionUnitTest.java rename to core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/java/collections/IterableToCollectionUnitTest.java index 0283191b74..22293a7c77 100644 --- a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/IterableToCollectionUnitTest.java +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/java/collections/IterableToCollectionUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.java.collections; +package com.ossez.java.collections; import static org.hamcrest.Matchers.contains; import static org.junit.Assert.assertThat; diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/JavaCollectionConversionUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/java/collections/JavaCollectionConversionUnitTest.java similarity index 90% rename from core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/JavaCollectionConversionUnitTest.java rename to core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/java/collections/JavaCollectionConversionUnitTest.java index 7947d1b6c7..9ab666a765 100644 --- a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/JavaCollectionConversionUnitTest.java +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/java/collections/JavaCollectionConversionUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.java.collections; +package com.ossez.java.collections; import java.util.ArrayList; import java.util.Arrays; @@ -25,7 +25,7 @@ public class JavaCollectionConversionUnitTest { @Test public final void givenUsingCoreJava_whenArrayConvertedToList_thenCorrect() { - final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Integer[] sourceArray = {0, 1, 2, 3, 4, 5}; final List targetList = Arrays.asList(sourceArray); } @@ -37,7 +37,7 @@ public class JavaCollectionConversionUnitTest { @Test public final void givenUsingGuava_whenArrayConvertedToList_thenCorrect() { - final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Integer[] sourceArray = {0, 1, 2, 3, 4, 5}; final List targetList = Lists.newArrayList(sourceArray); } @@ -49,7 +49,7 @@ public class JavaCollectionConversionUnitTest { @Test public final void givenUsingCommonsCollections_whenArrayConvertedToList_thenCorrect() { - final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Integer[] sourceArray = {0, 1, 2, 3, 4, 5}; final List targetList = new ArrayList<>(6); CollectionUtils.addAll(targetList, sourceArray); } @@ -58,13 +58,13 @@ public class JavaCollectionConversionUnitTest { @Test public final void givenUsingCoreJavaV1_whenArrayConvertedToSet_thenCorrect() { - final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Integer[] sourceArray = {0, 1, 2, 3, 4, 5}; final Set targetSet = new HashSet(Arrays.asList(sourceArray)); } @Test public final void givenUsingCoreJavaV2_whenArrayConvertedToSet_thenCorrect() { - final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Integer[] sourceArray = {0, 1, 2, 3, 4, 5}; final Set targetSet = new HashSet(); Collections.addAll(targetSet, sourceArray); } @@ -77,7 +77,7 @@ public class JavaCollectionConversionUnitTest { @Test public final void givenUsingGuava_whenArrayConvertedToSet_thenCorrect() { - final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Integer[] sourceArray = {0, 1, 2, 3, 4, 5}; final Set targetSet = Sets.newHashSet(sourceArray); } @@ -89,7 +89,7 @@ public class JavaCollectionConversionUnitTest { @Test public final void givenUsingCommonsCollections_whenArrayConvertedToSet_thenCorrect() { - final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; + final Integer[] sourceArray = {0, 1, 2, 3, 4, 5}; final Set targetSet = new HashSet<>(6); CollectionUtils.addAll(targetSet, sourceArray); } diff --git a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/lists/ListToStringUnitTest.java b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/java/lists/ListToStringUnitTest.java similarity index 96% rename from core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/lists/ListToStringUnitTest.java rename to core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/java/lists/ListToStringUnitTest.java index 0fdc096d14..b05002b9f4 100644 --- a/core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/lists/ListToStringUnitTest.java +++ b/core-java-modules/core-java-collections-conversions/src/test/java/com/ossez/java/lists/ListToStringUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.java.lists; +package com.ossez.java.lists; import java.util.Arrays; import java.util.List; From e5d5c2dcbe62f8a4a18431d8fc69ca2f8c98e985 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Fri, 21 Apr 2023 17:09:52 -0400 Subject: [PATCH 4/5] =?UTF-8?q?=E6=8A=8A=E6=96=87=E7=AB=A0=EF=BC=9Ahttps:/?= =?UTF-8?q?/www.ossez.com/t/java-map-value-array-list-set/14388=20?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 10 -- .idea/compiler.xml | 10 +- .idea/encodings.xml | 4 - .idea/jarRepositories.xml | 10 -- .idea/php.xml | 12 -- .idea/thriftCompiler.xml | 6 - .../java/com/baeldung/convertToMap/Book.java | 48 ------ .../baeldung/convertToMap/ConvertToMap.java | 30 ---- .../convertcollectiontoarraylist/Foo.java | 52 ------- .../com/baeldung/convertlisttomap/Animal.java | 27 ---- .../ConvertListToMapService.java | 43 ------ .../convertToMap/ConvertToMapUnitTest.java | 53 ------- .../CollectionToArrayListUnitTest.java | 145 ------------------ .../ConvertIteratorToListServiceUnitTest.java | 100 ------------ .../ConvertListToMapServiceUnitTest.java | 68 -------- ...tWithDuplicatedIdToMapServiceUnitTest.java | 68 -------- .../IterableToCollectionUnitTest.java | 122 --------------- .../JavaCollectionConversionUnitTest.java | 145 ------------------ .../java/lists/ListToStringUnitTest.java | 31 ---- 19 files changed, 2 insertions(+), 982 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/php.xml delete mode 100644 .idea/thriftCompiler.xml delete mode 100644 core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/Book.java delete mode 100644 core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertToMap/ConvertToMap.java delete mode 100644 core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertcollectiontoarraylist/Foo.java delete mode 100644 core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/Animal.java delete mode 100644 core-java-modules/core-java-collections-conversions/src/main/java/com/baeldung/convertlisttomap/ConvertListToMapService.java delete mode 100644 core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertToMap/ConvertToMapUnitTest.java delete mode 100644 core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertcollectiontoarraylist/CollectionToArrayListUnitTest.java delete mode 100644 core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertiteratortolist/ConvertIteratorToListServiceUnitTest.java delete mode 100644 core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListToMapServiceUnitTest.java delete mode 100644 core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/convertlisttomap/ConvertListWithDuplicatedIdToMapServiceUnitTest.java delete mode 100644 core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/IterableToCollectionUnitTest.java delete mode 100644 core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/collections/JavaCollectionConversionUnitTest.java delete mode 100644 core-java-modules/core-java-collections-conversions/src/test/java/com/baeldung/java/lists/ListToStringUnitTest.java diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 0a8642fac0..0000000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,10 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml -# Zeppelin ignored files -/ZeppelinRemoteNotebooks/ diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 0a977aecaa..7e75ee986b 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -2,7 +2,6 @@ - @@ -22,10 +21,10 @@ - - + + @@ -33,11 +32,6 @@ - - - - -