BAEL 1035 - Introduction to Eclipse Collections (#2395)
* 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
This commit is contained in:
parent
146b633bac
commit
c3de3dbfa9
|
@ -462,6 +462,11 @@
|
|||
<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>
|
||||
|
@ -503,5 +508,6 @@
|
|||
<bytebuddy.version>1.7.1</bytebuddy.version>
|
||||
<pcollections.version>2.1.2</pcollections.version>
|
||||
<rome.version>1.0</rome.version>
|
||||
<eclipse-collections.version>8.2.0</eclipse-collections.version>
|
||||
</properties>
|
||||
</project>
|
||||
</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,34 @@
|
|||
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.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AllSatisfyPatternTest {
|
||||
|
||||
MutableList<Integer> list;
|
||||
|
||||
@Before
|
||||
public void getList() {
|
||||
this.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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAnySatisfiesCondition_thenCorrect() {
|
||||
boolean result = list.allSatisfy(Predicates.greaterThan(0));
|
||||
|
||||
assertTrue(result);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
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.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AnySatisfyPatternTest {
|
||||
|
||||
MutableList<Integer> list;
|
||||
|
||||
@Before
|
||||
public void getList() {
|
||||
this.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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAnySatisfiesCondition_thenCorrect() {
|
||||
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,20 @@
|
|||
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,34 @@
|
|||
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.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DetectPatternTest {
|
||||
|
||||
MutableList<Integer> list;
|
||||
|
||||
@Before
|
||||
public void getList() {
|
||||
this.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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDetect_thenCorrect() {
|
||||
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,61 @@
|
|||
package com.baeldung.eclipsecollections;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.eclipse.collections.api.block.predicate.Predicate;
|
||||
import org.eclipse.collections.api.list.MutableList;
|
||||
import org.eclipse.collections.api.partition.list.PartitionMutableList;
|
||||
import org.eclipse.collections.impl.block.factory.Predicates;
|
||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PartitionPatternTest {
|
||||
|
||||
MutableList<Integer> list;
|
||||
|
||||
@Before
|
||||
public void getList() {
|
||||
this.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);
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unused" })
|
||||
@Test
|
||||
public void whenAnySatisfiesCondition_thenCorrect() {
|
||||
MutableList<Integer> numbers = list;
|
||||
PartitionMutableList<Integer> partitionedFolks = numbers.partition(new Predicate<Integer>() {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = -1551138743683678406L;
|
||||
|
||||
public boolean accept(Integer each) {
|
||||
return each > 30;
|
||||
}
|
||||
});
|
||||
MutableList<Integer> greaterThanThirty = partitionedFolks.getSelected()
|
||||
.sortThis();
|
||||
MutableList<Integer> smallerThanThirty = partitionedFolks.getRejected()
|
||||
.sortThis();
|
||||
|
||||
assertEquals(1, (int) smallerThanThirty.getFirst());
|
||||
assertTrue(smallerThanThirty.anySatisfy(Predicates.equal(5)));
|
||||
assertTrue(smallerThanThirty.anySatisfy(Predicates.equal(8)));
|
||||
assertTrue(smallerThanThirty.anySatisfy(Predicates.equal(17)));
|
||||
assertTrue(smallerThanThirty.anySatisfy(Predicates.equal(23)));
|
||||
|
||||
assertTrue(greaterThanThirty.anySatisfy(Predicates.equal(31)));
|
||||
assertTrue(greaterThanThirty.anySatisfy(Predicates.equal(38)));
|
||||
assertTrue(greaterThanThirty.anySatisfy(Predicates.equal(41)));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
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.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class RejectPatternTest {
|
||||
|
||||
MutableList<Integer> list;
|
||||
|
||||
@Before
|
||||
public void getList() {
|
||||
this.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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReject_thenCorrect() {
|
||||
MutableList<Integer> notGreaterThanThirty = list.reject(Predicates.greaterThan(30))
|
||||
.sortThis();
|
||||
|
||||
assertEquals(1, (int) notGreaterThanThirty.getFirst());
|
||||
assertEquals(5, (int) notGreaterThanThirty.get(1));
|
||||
assertEquals(8, (int) notGreaterThanThirty.get(2));
|
||||
assertEquals(17, (int) notGreaterThanThirty.get(3));
|
||||
assertEquals(23, (int) notGreaterThanThirty.getLast());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
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.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SelectPatternTest {
|
||||
|
||||
MutableList<Integer> list;
|
||||
|
||||
@Before
|
||||
public void getList() {
|
||||
this.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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListwhenSelect_thenCorrect() {
|
||||
MutableList<Integer> greaterThanThirty = list.select(Predicates.greaterThan(30))
|
||||
.sortThis();
|
||||
|
||||
assertEquals(31, (int) greaterThanThirty.getFirst());
|
||||
assertEquals(38, (int) greaterThanThirty.get(1));
|
||||
assertEquals(41, (int) greaterThanThirty.getLast());
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public MutableList selectUsingLambda() {
|
||||
return list.select(each -> each > 30)
|
||||
.sortThis();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void givenListwhenSelectUsingLambda_thenCorrect() {
|
||||
MutableList<Integer> greaterThanThirty = selectUsingLambda();
|
||||
|
||||
assertEquals(31, (int) greaterThanThirty.getFirst());
|
||||
assertEquals(38, (int) greaterThanThirty.get(1));
|
||||
assertEquals(41, (int) greaterThanThirty.getLast());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue