BAEL-1035 Introduction to Eclipse Collections

This commit is contained in:
Ahmed Tawila 2017-08-11 20:40:39 +02:00
parent c8a6913956
commit 4c2447ebf2
8 changed files with 114 additions and 58 deletions

View File

@ -1,15 +1,25 @@
package com.baeldung.eclipsecollections;
import java.util.List;
public class Student {
private String firstName;
private String lastName;
private List<String> addresses;
public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public Student(String firstName, String lastName, List<String> 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<String> getAddresses() {
return addresses;
}
public void setAddresses(List<String> addresses) {
this.addresses = addresses;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

View File

@ -16,7 +16,7 @@ public class CollectPatternTest {
MutableList<String> 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));
}
}

View File

@ -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<String> addresses1;
MutableList<String> addresses2;
MutableList<String> addresses3;
MutableList<String> addresses4;
MutableList<Student> 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<String> 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));
}
}

View File

@ -1,32 +0,0 @@
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));
}
}
}

View File

@ -1,23 +0,0 @@
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);
}
}

View File

@ -28,7 +28,6 @@ public class PartitionPatternTest {
list.add(38);
}
@SuppressWarnings({ "unused" })
@Test
public void whenAnySatisfiesCondition_thenCorrect() {
MutableList<Integer> numbers = list;

View File

@ -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<String> numbers = Lists.mutable.with("1", "2", "3", "Ignored");
MutableList<String> cars = Lists.mutable.with("Porsche", "Volvo", "Toyota");
MutableList<Pair<String, String>> 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));
}
}

View File

@ -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<String> cars = FastList.newListWith("Porsche", "Volvo", "Toyota");
MutableList<Pair<String, Integer>> 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));
}
}