From 310e89e4f04402700be4a5fa4686a9c7914ec6f6 Mon Sep 17 00:00:00 2001 From: Ahmed Tawila Date: Sat, 12 Aug 2017 07:59:15 +0200 Subject: [PATCH] BAEL-1035 Introduction to Eclipse Collections (#2421) * Evaluation article: Different Types of Bean Injection in Spring * added tests & changed configuration to Java-based config * removed xml config files * rename unit tests * BAEL-972 - Apache Commons Text * remove code from evaluation article * remove code from evaluation article * BAEL-972 - Apache Commons Text - added another example * BAEL-972 - Apache Commons Text - just indentation * BAEL-994 - TemporalAdjuster in Java * BAEL-994 - TemporalAdjuster in Java * BAEL-994 - TemporalAdjuster in Java * BAEL-994 - TemporalAdjuster in Java * BAEL-994 - TemporalAdjuster in Java - fix problems * BAEL-1033 Introduction to StreamUtils * BAEL-1033 Introduction to StreamUtils * BAEL-1033 Introduction to StreamUtils * fix formatting * BAEL-1033 minor refactor * BAEL-1035 Introduction to Eclipse Collections * format * BAEL-1035 Introduction to Eclipse Collections * BAEL-1035 Introduction to Eclipse Collections --- libraries/pom.xml | 2 +- .../baeldung/eclipsecollections/Student.java | 26 ++++++++++++ .../CollectPatternTest.java | 4 +- .../eclipsecollections/FlatCollectTest.java | 42 +++++++++++++++++++ .../PartitionPatternTest.java | 1 - .../baeldung/eclipsecollections/ZipTest.java | 22 ++++++++++ .../eclipsecollections/ZipWithIndexTest.java | 22 ++++++++++ 7 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 libraries/src/test/java/com/baeldung/eclipsecollections/FlatCollectTest.java create mode 100644 libraries/src/test/java/com/baeldung/eclipsecollections/ZipTest.java create mode 100644 libraries/src/test/java/com/baeldung/eclipsecollections/ZipWithIndexTest.java diff --git a/libraries/pom.xml b/libraries/pom.xml index 490302ca29..a16a4de59d 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -515,4 +515,4 @@ 1.0 8.2.0 - + \ No newline at end of file diff --git a/libraries/src/main/java/com/baeldung/eclipsecollections/Student.java b/libraries/src/main/java/com/baeldung/eclipsecollections/Student.java index 2c634a28d9..cf6c06cec0 100644 --- a/libraries/src/main/java/com/baeldung/eclipsecollections/Student.java +++ b/libraries/src/main/java/com/baeldung/eclipsecollections/Student.java @@ -1,15 +1,25 @@ package com.baeldung.eclipsecollections; +import java.util.List; + public class Student { private String firstName; private String lastName; + private List addresses; public Student(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } + public Student(String firstName, String lastName, List addresses) { + super(); + this.firstName = firstName; + this.lastName = lastName; + this.addresses = addresses; + } + public String getFirstName() { return this.firstName; } @@ -17,4 +27,20 @@ public class Student { public String getLastName() { return this.lastName; } + + public List getAddresses() { + return addresses; + } + + public void setAddresses(List addresses) { + this.addresses = addresses; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } } \ No newline at end of file diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/CollectPatternTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/CollectPatternTest.java index 51591cd08c..5fb63794a1 100644 --- a/libraries/src/test/java/com/baeldung/eclipsecollections/CollectPatternTest.java +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/CollectPatternTest.java @@ -16,7 +16,7 @@ public class CollectPatternTest { MutableList lastNames = students.collect(Student::getLastName); - assertEquals(lastNames.get(0), "Hopkins"); - assertEquals(lastNames.get(1), "Adams"); + assertEquals("Hopkins", lastNames.get(0)); + assertEquals("Adams", lastNames.get(1)); } } diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/FlatCollectTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/FlatCollectTest.java new file mode 100644 index 0000000000..3d1453e557 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/FlatCollectTest.java @@ -0,0 +1,42 @@ +package com.baeldung.eclipsecollections; + +import org.eclipse.collections.api.list.MutableList; +import org.eclipse.collections.impl.list.mutable.FastList; +import static org.junit.Assert.assertEquals; +import org.junit.Before; +import org.junit.Test; + +public class FlatCollectTest { + + MutableList addresses1; + MutableList addresses2; + MutableList addresses3; + MutableList addresses4; + + MutableList students; + + @Before + public void setup() { + String address1 = "73 Pacific St., Forest Hills, NY 11375"; + String address2 = "93 Bayport Ave., South Richmond Hill, NY 11419"; + String address3 = "548 Market St, San Francisco, CA 94104"; + String address4 = "8605 Santa Monica Blvd, West Hollywood, CA 90069"; + + this.addresses1 = FastList.newListWith(address1, address2); + this.addresses2 = FastList.newListWith(address3, address4); + Student student1 = new Student("John", "Hopkins", addresses1); + Student student2 = new Student("George", "Adams", addresses2); + this.addresses2 = FastList.newListWith(address3, address4); + this.students = FastList.newListWith(student1, student2); + } + + @Test + public void whenFlatCollect_thenCorrect() { + MutableList addresses = students.flatCollect(Student::getAddresses); + + assertEquals("73 Pacific St., Forest Hills, NY 11375", addresses.get(0)); + assertEquals("93 Bayport Ave., South Richmond Hill, NY 11419", addresses.get(1)); + assertEquals("548 Market St, San Francisco, CA 94104", addresses.get(2)); + assertEquals("8605 Santa Monica Blvd, West Hollywood, CA 90069", addresses.get(3)); + } +} diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/PartitionPatternTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/PartitionPatternTest.java index 3e52829824..4d9619817b 100644 --- a/libraries/src/test/java/com/baeldung/eclipsecollections/PartitionPatternTest.java +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/PartitionPatternTest.java @@ -28,7 +28,6 @@ public class PartitionPatternTest { list.add(38); } - @SuppressWarnings({ "unused" }) @Test public void whenAnySatisfiesCondition_thenCorrect() { MutableList numbers = list; diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/ZipTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/ZipTest.java new file mode 100644 index 0000000000..9dfab8f32d --- /dev/null +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/ZipTest.java @@ -0,0 +1,22 @@ +package com.baeldung.eclipsecollections; + +import org.eclipse.collections.api.list.MutableList; +import org.eclipse.collections.api.tuple.Pair; +import org.eclipse.collections.impl.factory.Lists; +import org.eclipse.collections.impl.tuple.Tuples; +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class ZipTest { + + @Test + public void whenZip_thenCorrect() { + MutableList numbers = Lists.mutable.with("1", "2", "3", "Ignored"); + MutableList cars = Lists.mutable.with("Porsche", "Volvo", "Toyota"); + MutableList> pairs = numbers.zip(cars); + + assertEquals(Tuples.pair("1", "Porsche"), pairs.get(0)); + assertEquals(Tuples.pair("2", "Volvo"), pairs.get(1)); + assertEquals(Tuples.pair("3", "Toyota"), pairs.get(2)); + } +} diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/ZipWithIndexTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/ZipWithIndexTest.java new file mode 100644 index 0000000000..3e8fe9b410 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/ZipWithIndexTest.java @@ -0,0 +1,22 @@ +package com.baeldung.eclipsecollections; + +import static org.junit.Assert.assertEquals; + +import org.eclipse.collections.api.list.MutableList; +import org.eclipse.collections.api.tuple.Pair; +import org.eclipse.collections.impl.list.mutable.FastList; +import org.eclipse.collections.impl.tuple.Tuples; +import org.junit.Test; + +public class ZipWithIndexTest { + + @Test + public void whenZip_thenCorrect() { + MutableList cars = FastList.newListWith("Porsche", "Volvo", "Toyota"); + MutableList> pairs = cars.zipWithIndex(); + + assertEquals(Tuples.pair("Porsche", 0), pairs.get(0)); + assertEquals(Tuples.pair("Volvo", 1), pairs.get(1)); + assertEquals(Tuples.pair("Toyota", 2), pairs.get(2)); + } +}