From 207f88b597bee2111c328c98c08771c0ba2ebb35 Mon Sep 17 00:00:00 2001 From: Ahmed Tawila Date: Wed, 9 Aug 2017 06:42:13 +0200 Subject: [PATCH] BAEL-1035 Introduction to Eclipse Collections --- libraries/pom.xml | 7 +++ .../ConvertContainerToAnother.java | 20 +++++++ .../baeldung/eclipsecollections/Student.java | 20 +++++++ .../AllSatisfyPatternTest.java | 28 ++++++++++ .../AnySatisfyPatternTest.java | 28 ++++++++++ .../CollectPatternTest.java | 22 ++++++++ .../ConvertContainerToAnotherTest.java | 19 +++++++ .../eclipsecollections/DetectPatternTest.java | 27 ++++++++++ .../ForEachPatternTest.java | 32 +++++++++++ .../InjectIntoPatternTest.java | 23 ++++++++ .../eclipsecollections/LazyIterationTest.java | 26 +++++++++ .../eclipsecollections/RejectPatternTest.java | 32 +++++++++++ .../eclipsecollections/SelectPatternTest.java | 54 +++++++++++++++++++ 13 files changed, 338 insertions(+) create mode 100644 libraries/src/main/java/com/baeldung/eclipsecollections/ConvertContainerToAnother.java create mode 100644 libraries/src/main/java/com/baeldung/eclipsecollections/Student.java create mode 100644 libraries/src/test/java/com/baeldung/eclipsecollections/AllSatisfyPatternTest.java create mode 100644 libraries/src/test/java/com/baeldung/eclipsecollections/AnySatisfyPatternTest.java create mode 100644 libraries/src/test/java/com/baeldung/eclipsecollections/CollectPatternTest.java create mode 100644 libraries/src/test/java/com/baeldung/eclipsecollections/ConvertContainerToAnotherTest.java create mode 100644 libraries/src/test/java/com/baeldung/eclipsecollections/DetectPatternTest.java create mode 100644 libraries/src/test/java/com/baeldung/eclipsecollections/ForEachPatternTest.java create mode 100644 libraries/src/test/java/com/baeldung/eclipsecollections/InjectIntoPatternTest.java create mode 100644 libraries/src/test/java/com/baeldung/eclipsecollections/LazyIterationTest.java create mode 100644 libraries/src/test/java/com/baeldung/eclipsecollections/RejectPatternTest.java create mode 100644 libraries/src/test/java/com/baeldung/eclipsecollections/SelectPatternTest.java diff --git a/libraries/pom.xml b/libraries/pom.xml index 795f255f36..3d00a61cf0 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -453,6 +453,12 @@ pcollections ${pcollections.version} + + org.eclipse.collections + eclipse-collections + ${eclipse-collections.version} + + 0.7.0 @@ -493,5 +499,6 @@ 1.6.0 1.7.1 2.1.2 + 8.2.0 diff --git a/libraries/src/main/java/com/baeldung/eclipsecollections/ConvertContainerToAnother.java b/libraries/src/main/java/com/baeldung/eclipsecollections/ConvertContainerToAnother.java new file mode 100644 index 0000000000..069baeab9f --- /dev/null +++ b/libraries/src/main/java/com/baeldung/eclipsecollections/ConvertContainerToAnother.java @@ -0,0 +1,20 @@ +package com.baeldung.eclipsecollections; + +import java.util.List; + +import org.eclipse.collections.api.list.MutableList; +import org.eclipse.collections.impl.set.mutable.UnifiedSet; + +public class ConvertContainerToAnother { + + @SuppressWarnings("rawtypes") + public static List convertToList() { + UnifiedSet cars = new UnifiedSet<>(); + + cars.add("Toyota"); + cars.add("Mercedes"); + cars.add("Volkswagen"); + + return cars.toList(); + } +} diff --git a/libraries/src/main/java/com/baeldung/eclipsecollections/Student.java b/libraries/src/main/java/com/baeldung/eclipsecollections/Student.java new file mode 100644 index 0000000000..2c634a28d9 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/eclipsecollections/Student.java @@ -0,0 +1,20 @@ +package com.baeldung.eclipsecollections; + +public class Student { + + private String firstName; + private String lastName; + + public Student(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + public String getFirstName() { + return this.firstName; + } + + public String getLastName() { + return this.lastName; + } +} \ No newline at end of file diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/AllSatisfyPatternTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/AllSatisfyPatternTest.java new file mode 100644 index 0000000000..9f66fee0a8 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/AllSatisfyPatternTest.java @@ -0,0 +1,28 @@ +package com.baeldung.eclipsecollections; + +import static org.junit.Assert.assertTrue; + +import org.eclipse.collections.api.list.MutableList; +import org.eclipse.collections.impl.block.factory.Predicates; +import org.eclipse.collections.impl.list.mutable.FastList; +import org.junit.Test; + +public class AllSatisfyPatternTest { + + @Test + public void whenAnySatisfiesCondition_thenCorrect() { + MutableList list = new FastList<>(); + list.add(1); + list.add(8); + list.add(5); + list.add(41); + list.add(31); + list.add(17); + list.add(23); + list.add(38); + + boolean result = list.allSatisfy(Predicates.greaterThan(0)); + + assertTrue(result); + } +} diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/AnySatisfyPatternTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/AnySatisfyPatternTest.java new file mode 100644 index 0000000000..c3ab83abdc --- /dev/null +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/AnySatisfyPatternTest.java @@ -0,0 +1,28 @@ +package com.baeldung.eclipsecollections; + +import static org.junit.Assert.assertTrue; + +import org.eclipse.collections.api.list.MutableList; +import org.eclipse.collections.impl.block.factory.Predicates; +import org.eclipse.collections.impl.list.mutable.FastList; +import org.junit.Test; + +public class AnySatisfyPatternTest { + + @Test + public void whenAnySatisfiesCondition_thenCorrect() { + MutableList list = new FastList<>(); + list.add(1); + list.add(8); + list.add(5); + list.add(41); + list.add(31); + list.add(17); + list.add(23); + list.add(38); + + boolean result = list.anySatisfy(Predicates.greaterThan(30)); + + assertTrue(result); + } +} diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/CollectPatternTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/CollectPatternTest.java new file mode 100644 index 0000000000..51591cd08c --- /dev/null +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/CollectPatternTest.java @@ -0,0 +1,22 @@ +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.Test; + +public class CollectPatternTest { + + @Test + public void whenCollect_thenCorrect() { + Student student1 = new Student("John", "Hopkins"); + Student student2 = new Student("George", "Adams"); + + MutableList students = FastList.newListWith(student1, student2); + + MutableList lastNames = students.collect(Student::getLastName); + + assertEquals(lastNames.get(0), "Hopkins"); + assertEquals(lastNames.get(1), "Adams"); + } +} diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/ConvertContainerToAnotherTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/ConvertContainerToAnotherTest.java new file mode 100644 index 0000000000..d8cee898a2 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/ConvertContainerToAnotherTest.java @@ -0,0 +1,19 @@ +package com.baeldung.eclipsecollections; + +import static org.junit.Assert.assertTrue; + +import org.eclipse.collections.api.list.MutableList; +import org.eclipse.collections.impl.block.factory.Predicates; +import org.junit.Test; + +public class ConvertContainerToAnotherTest { + + @SuppressWarnings({ "unchecked", "rawtypes" }) + @Test + public void whenConvertContainerToAnother_thenCorrect() { + MutableList cars = (MutableList) ConvertContainerToAnother.convertToList(); + assertTrue(cars.anySatisfy(Predicates.equal("Toyota"))); + assertTrue(cars.anySatisfy(Predicates.equal("Mercedes"))); + assertTrue(cars.anySatisfy(Predicates.equal("Volkswagen"))); + } +} diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/DetectPatternTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/DetectPatternTest.java new file mode 100644 index 0000000000..39d77498ca --- /dev/null +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/DetectPatternTest.java @@ -0,0 +1,27 @@ +package com.baeldung.eclipsecollections; + +import org.eclipse.collections.api.list.MutableList; +import org.eclipse.collections.impl.block.factory.Predicates; +import org.eclipse.collections.impl.list.mutable.FastList; +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class DetectPatternTest { + + @Test + public void whenDetect_thenCorrect() { + MutableList list = new FastList<>(); + list.add(1); + list.add(8); + list.add(5); + list.add(41); + list.add(31); + list.add(17); + list.add(23); + list.add(38); + + Integer result = list.detect(Predicates.greaterThan(30)); + + assertEquals((int) result, 41); + } +} diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/ForEachPatternTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/ForEachPatternTest.java new file mode 100644 index 0000000000..e8d93a2af6 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/ForEachPatternTest.java @@ -0,0 +1,32 @@ +package com.baeldung.eclipsecollections; + +import org.eclipse.collections.api.block.procedure.Procedure; +import org.eclipse.collections.api.tuple.Pair; +import org.eclipse.collections.impl.map.mutable.UnifiedMap; +import org.eclipse.collections.impl.tuple.Tuples; +import static org.junit.Assert.assertEquals; + +import java.util.Map; + +import org.junit.Test; + +public class ForEachPatternTest { + + @SuppressWarnings("unchecked") + @Test + public void whenInstantiateAndChangeValues_thenCorrect() { + Pair pair1 = Tuples.pair(1, "One"); + Pair pair2 = Tuples.pair(2, "Two"); + Pair pair3 = Tuples.pair(3, "Three"); + + UnifiedMap map = UnifiedMap.newMapWith(pair1, pair2, pair3); + + for (int i = 0; i < map.size(); i++) { + map.put(i + 1, "New Value"); + } + + for (int i = 0; i < map.size(); i++) { + assertEquals("New Value", map.get(i + 1)); + } + } +} diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/InjectIntoPatternTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/InjectIntoPatternTest.java new file mode 100644 index 0000000000..bcd34021b1 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/InjectIntoPatternTest.java @@ -0,0 +1,23 @@ +package com.baeldung.eclipsecollections; + +import static org.junit.Assert.assertEquals; + +import java.util.List; + +import org.eclipse.collections.impl.factory.Lists; +import org.junit.Test; + +public class InjectIntoPatternTest { + + @Test + public void whenInjectInto_thenCorrect() { + List list = Lists.mutable.of(1, 2, 3, 4); + int result = 5; + for (int i = 0; i < list.size(); i++) { + Integer v = list.get(i); + result = result + v.intValue(); + } + + assertEquals(15, result); + } +} diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/LazyIterationTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/LazyIterationTest.java new file mode 100644 index 0000000000..8fe1286d41 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/LazyIterationTest.java @@ -0,0 +1,26 @@ +package com.baeldung.eclipsecollections; + +import org.eclipse.collections.api.LazyIterable; +import org.eclipse.collections.api.list.MutableList; +import org.eclipse.collections.impl.block.factory.Predicates; +import org.eclipse.collections.impl.factory.Lists; +import static org.junit.Assert.assertTrue; +import org.junit.Test; + +public class LazyIterationTest { + + @Test + public void whenLazyIteration_thenCorrect() { + Student student1 = new Student("John", "Hopkins"); + Student student2 = new Student("George", "Adams"); + Student student3 = new Student("Jennifer", "Rodriguez"); + + MutableList students = Lists.mutable.with(student1, student2, student3); + LazyIterable lazyStudents = students.asLazy(); + LazyIterable lastNames = lazyStudents.collect(Student::getLastName); + + assertTrue(lastNames.anySatisfy(Predicates.equal("Hopkins"))); + assertTrue(lastNames.anySatisfy(Predicates.equal("Adams"))); + assertTrue(lastNames.anySatisfy(Predicates.equal("Rodriguez"))); + } +} diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/RejectPatternTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/RejectPatternTest.java new file mode 100644 index 0000000000..2cc1cdb945 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/RejectPatternTest.java @@ -0,0 +1,32 @@ +package com.baeldung.eclipsecollections; + +import org.eclipse.collections.api.list.MutableList; +import org.eclipse.collections.impl.block.factory.Predicates; +import org.eclipse.collections.impl.list.mutable.FastList; +import org.junit.Assert; +import org.junit.Test; + +public class RejectPatternTest { + + @Test + public void whenReject_thenCorrect() { + MutableList list = new FastList<>(); + list.add(1); + list.add(8); + list.add(5); + list.add(41); + list.add(31); + list.add(17); + list.add(23); + list.add(38); + + MutableList notGreaterThanThirty = list.reject(Predicates.greaterThan(30)) + .sortThis(); + + Assert.assertEquals(1, (int) notGreaterThanThirty.getFirst()); + Assert.assertEquals(5, (int) notGreaterThanThirty.get(1)); + Assert.assertEquals(8, (int) notGreaterThanThirty.get(2)); + Assert.assertEquals(17, (int) notGreaterThanThirty.get(3)); + Assert.assertEquals(23, (int) notGreaterThanThirty.getLast()); + } +} diff --git a/libraries/src/test/java/com/baeldung/eclipsecollections/SelectPatternTest.java b/libraries/src/test/java/com/baeldung/eclipsecollections/SelectPatternTest.java new file mode 100644 index 0000000000..c5e3af3a3f --- /dev/null +++ b/libraries/src/test/java/com/baeldung/eclipsecollections/SelectPatternTest.java @@ -0,0 +1,54 @@ +package com.baeldung.eclipsecollections; + +import org.eclipse.collections.api.list.MutableList; +import org.eclipse.collections.impl.block.factory.Predicates; +import org.eclipse.collections.impl.list.mutable.FastList; +import org.junit.Assert; +import org.junit.Test; + +public class SelectPatternTest { + + @Test + public void whenSelect_thenCorrect() { + MutableList list = new FastList<>(); + list.add(1); + list.add(8); + list.add(5); + list.add(41); + list.add(31); + list.add(17); + list.add(23); + list.add(38); + + MutableList greaterThanThirty = list.select(Predicates.greaterThan(30)) + .sortThis(); + + Assert.assertEquals(31, (int) greaterThanThirty.getFirst()); + Assert.assertEquals(38, (int) greaterThanThirty.get(1)); + Assert.assertEquals(41, (int) greaterThanThirty.getLast()); + } + + public MutableList selectUsingLambda() { + MutableList list = new FastList<>(); + list.add(1); + list.add(8); + list.add(5); + list.add(41); + list.add(31); + list.add(17); + list.add(23); + list.add(38); + + return list.select(each -> each > 30) + .sortThis(); + } + + @Test + public void whenSelectUsingLambda_thenCorrect() { + MutableList greaterThanThirty = selectUsingLambda(); + + Assert.assertEquals(31, (int) greaterThanThirty.getFirst()); + Assert.assertEquals(38, (int) greaterThanThirty.get(1)); + Assert.assertEquals(41, (int) greaterThanThirty.getLast()); + } +}