BAEL-1035 Introduction to Eclipse Collections
This commit is contained in:
parent
ac1e77f5d9
commit
c8a6913956
@ -5,13 +5,16 @@ import static org.junit.Assert.assertTrue;
|
|||||||
import org.eclipse.collections.api.list.MutableList;
|
import org.eclipse.collections.api.list.MutableList;
|
||||||
import org.eclipse.collections.impl.block.factory.Predicates;
|
import org.eclipse.collections.impl.block.factory.Predicates;
|
||||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class AllSatisfyPatternTest {
|
public class AllSatisfyPatternTest {
|
||||||
|
|
||||||
@Test
|
MutableList<Integer> list;
|
||||||
public void whenAnySatisfiesCondition_thenCorrect() {
|
|
||||||
MutableList<Integer> list = new FastList<>();
|
@Before
|
||||||
|
public void getList() {
|
||||||
|
this.list = new FastList<>();
|
||||||
list.add(1);
|
list.add(1);
|
||||||
list.add(8);
|
list.add(8);
|
||||||
list.add(5);
|
list.add(5);
|
||||||
@ -20,7 +23,10 @@ public class AllSatisfyPatternTest {
|
|||||||
list.add(17);
|
list.add(17);
|
||||||
list.add(23);
|
list.add(23);
|
||||||
list.add(38);
|
list.add(38);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenAnySatisfiesCondition_thenCorrect() {
|
||||||
boolean result = list.allSatisfy(Predicates.greaterThan(0));
|
boolean result = list.allSatisfy(Predicates.greaterThan(0));
|
||||||
|
|
||||||
assertTrue(result);
|
assertTrue(result);
|
||||||
|
@ -5,13 +5,16 @@ import static org.junit.Assert.assertTrue;
|
|||||||
import org.eclipse.collections.api.list.MutableList;
|
import org.eclipse.collections.api.list.MutableList;
|
||||||
import org.eclipse.collections.impl.block.factory.Predicates;
|
import org.eclipse.collections.impl.block.factory.Predicates;
|
||||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class AnySatisfyPatternTest {
|
public class AnySatisfyPatternTest {
|
||||||
|
|
||||||
@Test
|
MutableList<Integer> list;
|
||||||
public void whenAnySatisfiesCondition_thenCorrect() {
|
|
||||||
MutableList<Integer> list = new FastList<>();
|
@Before
|
||||||
|
public void getList() {
|
||||||
|
this.list = new FastList<>();
|
||||||
list.add(1);
|
list.add(1);
|
||||||
list.add(8);
|
list.add(8);
|
||||||
list.add(5);
|
list.add(5);
|
||||||
@ -20,7 +23,10 @@ public class AnySatisfyPatternTest {
|
|||||||
list.add(17);
|
list.add(17);
|
||||||
list.add(23);
|
list.add(23);
|
||||||
list.add(38);
|
list.add(38);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenAnySatisfiesCondition_thenCorrect() {
|
||||||
boolean result = list.anySatisfy(Predicates.greaterThan(30));
|
boolean result = list.anySatisfy(Predicates.greaterThan(30));
|
||||||
|
|
||||||
assertTrue(result);
|
assertTrue(result);
|
||||||
|
@ -4,13 +4,17 @@ import org.eclipse.collections.api.list.MutableList;
|
|||||||
import org.eclipse.collections.impl.block.factory.Predicates;
|
import org.eclipse.collections.impl.block.factory.Predicates;
|
||||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class DetectPatternTest {
|
public class DetectPatternTest {
|
||||||
|
|
||||||
@Test
|
MutableList<Integer> list;
|
||||||
public void whenDetect_thenCorrect() {
|
|
||||||
MutableList<Integer> list = new FastList<>();
|
@Before
|
||||||
|
public void getList() {
|
||||||
|
this.list = new FastList<>();
|
||||||
list.add(1);
|
list.add(1);
|
||||||
list.add(8);
|
list.add(8);
|
||||||
list.add(5);
|
list.add(5);
|
||||||
@ -19,7 +23,10 @@ public class DetectPatternTest {
|
|||||||
list.add(17);
|
list.add(17);
|
||||||
list.add(23);
|
list.add(23);
|
||||||
list.add(38);
|
list.add(38);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenDetect_thenCorrect() {
|
||||||
Integer result = list.detect(Predicates.greaterThan(30));
|
Integer result = list.detect(Predicates.greaterThan(30));
|
||||||
|
|
||||||
assertEquals((int) result, 41);
|
assertEquals((int) result, 41);
|
||||||
|
@ -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)));
|
||||||
|
}
|
||||||
|
}
|
@ -3,14 +3,17 @@ package com.baeldung.eclipsecollections;
|
|||||||
import org.eclipse.collections.api.list.MutableList;
|
import org.eclipse.collections.api.list.MutableList;
|
||||||
import org.eclipse.collections.impl.block.factory.Predicates;
|
import org.eclipse.collections.impl.block.factory.Predicates;
|
||||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||||
import org.junit.Assert;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class RejectPatternTest {
|
public class RejectPatternTest {
|
||||||
|
|
||||||
@Test
|
MutableList<Integer> list;
|
||||||
public void whenReject_thenCorrect() {
|
|
||||||
MutableList<Integer> list = new FastList<>();
|
@Before
|
||||||
|
public void getList() {
|
||||||
|
this.list = new FastList<>();
|
||||||
list.add(1);
|
list.add(1);
|
||||||
list.add(8);
|
list.add(8);
|
||||||
list.add(5);
|
list.add(5);
|
||||||
@ -19,14 +22,17 @@ public class RejectPatternTest {
|
|||||||
list.add(17);
|
list.add(17);
|
||||||
list.add(23);
|
list.add(23);
|
||||||
list.add(38);
|
list.add(38);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenReject_thenCorrect() {
|
||||||
MutableList<Integer> notGreaterThanThirty = list.reject(Predicates.greaterThan(30))
|
MutableList<Integer> notGreaterThanThirty = list.reject(Predicates.greaterThan(30))
|
||||||
.sortThis();
|
.sortThis();
|
||||||
|
|
||||||
Assert.assertEquals(1, (int) notGreaterThanThirty.getFirst());
|
assertEquals(1, (int) notGreaterThanThirty.getFirst());
|
||||||
Assert.assertEquals(5, (int) notGreaterThanThirty.get(1));
|
assertEquals(5, (int) notGreaterThanThirty.get(1));
|
||||||
Assert.assertEquals(8, (int) notGreaterThanThirty.get(2));
|
assertEquals(8, (int) notGreaterThanThirty.get(2));
|
||||||
Assert.assertEquals(17, (int) notGreaterThanThirty.get(3));
|
assertEquals(17, (int) notGreaterThanThirty.get(3));
|
||||||
Assert.assertEquals(23, (int) notGreaterThanThirty.getLast());
|
assertEquals(23, (int) notGreaterThanThirty.getLast());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,14 +3,17 @@ package com.baeldung.eclipsecollections;
|
|||||||
import org.eclipse.collections.api.list.MutableList;
|
import org.eclipse.collections.api.list.MutableList;
|
||||||
import org.eclipse.collections.impl.block.factory.Predicates;
|
import org.eclipse.collections.impl.block.factory.Predicates;
|
||||||
import org.eclipse.collections.impl.list.mutable.FastList;
|
import org.eclipse.collections.impl.list.mutable.FastList;
|
||||||
import org.junit.Assert;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class SelectPatternTest {
|
public class SelectPatternTest {
|
||||||
|
|
||||||
@Test
|
MutableList<Integer> list;
|
||||||
public void whenSelect_thenCorrect() {
|
|
||||||
MutableList<Integer> list = new FastList<>();
|
@Before
|
||||||
|
public void getList() {
|
||||||
|
this.list = new FastList<>();
|
||||||
list.add(1);
|
list.add(1);
|
||||||
list.add(8);
|
list.add(8);
|
||||||
list.add(5);
|
list.add(5);
|
||||||
@ -19,36 +22,31 @@ public class SelectPatternTest {
|
|||||||
list.add(17);
|
list.add(17);
|
||||||
list.add(23);
|
list.add(23);
|
||||||
list.add(38);
|
list.add(38);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenListwhenSelect_thenCorrect() {
|
||||||
MutableList<Integer> greaterThanThirty = list.select(Predicates.greaterThan(30))
|
MutableList<Integer> greaterThanThirty = list.select(Predicates.greaterThan(30))
|
||||||
.sortThis();
|
.sortThis();
|
||||||
|
|
||||||
Assert.assertEquals(31, (int) greaterThanThirty.getFirst());
|
assertEquals(31, (int) greaterThanThirty.getFirst());
|
||||||
Assert.assertEquals(38, (int) greaterThanThirty.get(1));
|
assertEquals(38, (int) greaterThanThirty.get(1));
|
||||||
Assert.assertEquals(41, (int) greaterThanThirty.getLast());
|
assertEquals(41, (int) greaterThanThirty.getLast());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
public MutableList selectUsingLambda() {
|
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)
|
return list.select(each -> each > 30)
|
||||||
.sortThis();
|
.sortThis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
@Test
|
@Test
|
||||||
public void whenSelectUsingLambda_thenCorrect() {
|
public void givenListwhenSelectUsingLambda_thenCorrect() {
|
||||||
MutableList<Integer> greaterThanThirty = selectUsingLambda();
|
MutableList<Integer> greaterThanThirty = selectUsingLambda();
|
||||||
|
|
||||||
Assert.assertEquals(31, (int) greaterThanThirty.getFirst());
|
assertEquals(31, (int) greaterThanThirty.getFirst());
|
||||||
Assert.assertEquals(38, (int) greaterThanThirty.get(1));
|
assertEquals(38, (int) greaterThanThirty.get(1));
|
||||||
Assert.assertEquals(41, (int) greaterThanThirty.getLast());
|
assertEquals(41, (int) greaterThanThirty.getLast());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user