The Goldman Sachs Collections framework (AKA GS Collections) provides us with an alternative implementation for the JDK collections framework.
GS Collections provides us with capabilities similar to those we find in programming languages such as Scala, C# and Samlltalk. Using this framework together with lambda expressions allows us writing significantly shorter code. The following code sample is a simple demo for showing the power of this framework while using lambda expressions.
package com.lifemichael.samples; import com.gs.collections.api.list.MutableList; import com.gs.collections.impl.list.mutable.FastList; public class JumpStartDemo { public static void main(String args[]) { MutableList<Student> students = FastList.newList(); students.add(new Student("moshe", 12312, 99)); students.add(new Student("david", 52423, 89)); students.add(new Student("ron", 54332, 94)); MutableList<Student> bestStudents = students.select(ob->ob.getAverage()>90); System.out.println(bestStudents); } }
The following video clip overviews this code sample, shows its execution and explains each and every part of it.
We can easily filter a collection that already exists and get a new one that includes just those elements that pass the criteria we set. The following code sample shows that.
package com.lifemichael.samples; import com.gs.collections.api.list.MutableList; import com.gs.collections.api.partition.list.PartitionMutableList; import com.gs.collections.impl.list.mutable.FastList; public class FilteringPatternsDemo { public static void main(String args[]) { MutableList<Student> students = FastList.newList(); students.add(new Student("moshe", 12312, 99)); students.add(new Student("david", 52423, 89)); students.add(new Student("ron", 54332, 94)); students.add(new Student("jane",98798, 93)); students.add(new Student("rami",42333,76)); students.add(new Student("john",83423,92)); students.add(new Student("tal",42323,54)); //select pattern MutableList<Student> bestStudents = students.select(ob->ob.getAverage()>90); System.out.println(bestStudents); System.out.println(); //reject pattern MutableList<Student> studentsWhoPass = students.reject(ob -> ob.getAverage() < 60); System.out.println(studentsWhoPass); System.out.println(); //partition pattern PartitionMutableList<Student> partitions = students.partition(ob -> ob.getAverage() > 90); MutableList<Student> excellentStudents = partitions.getSelected(); MutableList<Student> simpleStudents = partitions.getRejected(); System.out.println(excellentStudents); System.out.println(simpleStudents); } }
The following video clip overviews this code sample, shows its execution and explains each and every part of it.
We can easily create a new collection based on a collection that already exist by specifying the transformation we want to take place. The following code sample shows how to do it.
package com.lifemichael.samples; import com.gs.collections.api.list.MutableList; import com.gs.collections.impl.list.mutable.FastList; import com.gs.collections.api.multimap.Multimap; public class TransformingPatternsDemo { public static void main(String args[]) { MutableList<Student> students = FastList.newList(); students.add(new Student("moshe", 12312, 99)); students.add(new Student("david", 52423, 85)); students.add(new Student("ron", 54332, 94)); students.add(new Student("jane",98798, 94)); students.add(new Student("rami",42333,76)); students.add(new Student("john",83423,85)); students.add(new Student("tal",42323,54)); //collect pattern MutableList<String> names = students.collect(ob->ob.getName()); System.out.println(names); System.out.println(); //collect if pattern MutableList<String> namesOfBestStudents = students.collectIf(ob->ob.getAverage()>90,ob->ob.getName()); System.out.println(namesOfBestStudents); System.out.println(); //group by pattern Multimap<Double,Student> map = students.groupBy(ob->ob.getAverage()); System.out.println(map); System.out.println(); } }
The following video clip overviews this code sample, explains each and every part of it and shows its exectuion.
You can find more video clips, code samples and training material for learning how to use this framework in my new Goldman Sachs Collections course. You can find its community free version at http://abelski.lifemichael.com.