BAEL-1035 Introduction to Eclipse Collections
This commit is contained in:
parent
af4a8eb101
commit
207f88b597
@ -453,6 +453,12 @@
|
||||
<artifactId>pcollections</artifactId>
|
||||
<version>${pcollections.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.collections</groupId>
|
||||
<artifactId>eclipse-collections</artifactId>
|
||||
<version>${eclipse-collections.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
<properties>
|
||||
<multiverse.version>0.7.0</multiverse.version>
|
||||
@ -493,5 +499,6 @@
|
||||
<hll.version>1.6.0</hll.version>
|
||||
<bytebuddy.version>1.7.1</bytebuddy.version>
|
||||
<pcollections.version>2.1.2</pcollections.version>
|
||||
<eclipse-collections.version>8.2.0</eclipse-collections.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
@ -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<String> cars = new UnifiedSet<>();
|
||||
|
||||
cars.add("Toyota");
|
||||
cars.add("Mercedes");
|
||||
cars.add("Volkswagen");
|
||||
|
||||
return cars.toList();
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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<Integer> 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);
|
||||
}
|
||||
}
|
@ -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<Integer> 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);
|
||||
}
|
||||
}
|
@ -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<Student> students = FastList.newListWith(student1, student2);
|
||||
|
||||
MutableList<String> lastNames = students.collect(Student::getLastName);
|
||||
|
||||
assertEquals(lastNames.get(0), "Hopkins");
|
||||
assertEquals(lastNames.get(1), "Adams");
|
||||
}
|
||||
}
|
@ -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<String> cars = (MutableList) ConvertContainerToAnother.convertToList();
|
||||
assertTrue(cars.anySatisfy(Predicates.equal("Toyota")));
|
||||
assertTrue(cars.anySatisfy(Predicates.equal("Mercedes")));
|
||||
assertTrue(cars.anySatisfy(Predicates.equal("Volkswagen")));
|
||||
}
|
||||
}
|
@ -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<Integer> 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);
|
||||
}
|
||||
}
|
@ -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<Integer, String> pair1 = Tuples.pair(1, "One");
|
||||
Pair<Integer, String> pair2 = Tuples.pair(2, "Two");
|
||||
Pair<Integer, String> pair3 = Tuples.pair(3, "Three");
|
||||
|
||||
UnifiedMap<Integer, String> 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));
|
||||
}
|
||||
}
|
||||
}
|
@ -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<Integer> 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);
|
||||
}
|
||||
}
|
@ -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<Student> students = Lists.mutable.with(student1, student2, student3);
|
||||
LazyIterable<Student> lazyStudents = students.asLazy();
|
||||
LazyIterable<String> lastNames = lazyStudents.collect(Student::getLastName);
|
||||
|
||||
assertTrue(lastNames.anySatisfy(Predicates.equal("Hopkins")));
|
||||
assertTrue(lastNames.anySatisfy(Predicates.equal("Adams")));
|
||||
assertTrue(lastNames.anySatisfy(Predicates.equal("Rodriguez")));
|
||||
}
|
||||
}
|
@ -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<Integer> 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<Integer> 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());
|
||||
}
|
||||
}
|
@ -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<Integer> 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<Integer> 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<Integer> 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<Integer> greaterThanThirty = selectUsingLambda();
|
||||
|
||||
Assert.assertEquals(31, (int) greaterThanThirty.getFirst());
|
||||
Assert.assertEquals(38, (int) greaterThanThirty.get(1));
|
||||
Assert.assertEquals(41, (int) greaterThanThirty.getLast());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user