Created
July 13, 2012 10:33
-
-
Save pbzdyl/3104147 to your computer and use it in GitHub Desktop.
Code for my Parallel Collections post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Set<Customer> customers = ...; | |
display(new ParallelSet<Customer>(customers).filter(...).map(...).sort(...)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovyx.gpars.GParsPool | |
class Person { | |
String name; | |
int age; | |
String city; | |
} | |
def population = ...; | |
GParsPool.withPool { | |
println population | |
.findAllParallel{ it.age >= 18 } | |
.groupByParallel{ it.city } | |
.collectParallel{ "${it.key}: ${it.value.size()}" } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import extra166y; | |
// our plain data | |
Person[] personsArray = ...; | |
// converting to a parallel array | |
ParallelArray<Person> persons = | |
ParallelArray.createFromCopy(personsArray, ParallelArray.defaultExecutor()); | |
// we need a predicate to filter rich persons with salary > 1M | |
Ops.Predicate<Person> isRich = new Ops.Predicate<Person>() { | |
public boolean op(Person p) { | |
return p.getSalary() > 1_000_000; | |
} | |
}; | |
// and for those older than 45 | |
Ops.Predicate<Person> isOver45 = new Ops.Predicate<Person>() { | |
public boolean op(Person p) { | |
return p.getAge() > 45; | |
} | |
}; | |
// mapper mapping Person to its city | |
Ops.Op<Person, String> toCity = new Ops.Op<Person, String>() { | |
public String op(Person p) { | |
return p.getCity(); | |
} | |
}; | |
// finally, let's filter, map and filter out duplicates | |
String[] cities = persons | |
.withFilter(CommonOps.andPredicate(isRich, isOver45)) | |
.withMapping(toCity) | |
.all() | |
.allUniqueElements(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import extra166y; | |
// our plain data | |
Person[] personsArray = ...; | |
// converting to a parallel array | |
ParallelArray<Person> persons = | |
ParallelArray.createFromCopy(personsArray, ParallelArray.defaultExecutor()); | |
// with lambdas we can have a nice short and consice expression | |
String[] cities = persons | |
.withFilter(p -> p.getSalary() > 1_000_000 && p.getAge() > 45) | |
.withMapping(p -> p.getCity()) | |
.all() | |
.allUniqueElements(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Person { | |
private final String name; | |
private final int age; | |
private final String city; | |
private final int salary; | |
public Person(String name, int age, String city, int salary) { | |
this.name = name; | |
this.age = age; | |
this.city = city; | |
this.salary = salary; | |
} | |
public String getName() { | |
return name; | |
} | |
public int getAge() { | |
return age; | |
} | |
public String getCity() { | |
return city; | |
} | |
public int getSalary() { | |
return salary; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Person(name: String, age: Int, city: String) | |
val population: Set[Person] = ... | |
println(population | |
.par | |
.filter( _.age >= 18 ) | |
.groupBy(_.city) | |
.mapValues(_.size)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// some imports | |
import scala.Function1; | |
import scala.runtime.AbstractFunction1; | |
import scala.collection.JavaConversions; | |
import scala.collection.parallel.ParMap; | |
import scala.collection.parallel.ParSet; | |
// our predicates and mapper | |
Function1<Person, Object> isAdult = new AbstractFunction1<Person, Object>() { | |
public Object apply(Person p) { | |
return p.age >= 18; | |
} | |
}; | |
Function1<Person, String> cityField = new AbstractFunction1<Person, String>() { | |
public String apply(Person p) { | |
return p.city; | |
} | |
}; | |
Function1<ParSet, Integer> mapper = new AbstractFunction1<ParSet, Integer>() { | |
public Integer apply(ParSet ps) { | |
return ps.size(); | |
} | |
}; | |
// First we need to convert our java.util.Set to its Scala version | |
java.util.Set<Person> population = ...; | |
scala.collection.mutable.Set<Person> spopulation = JavaConversions.asScalaSet(population); | |
// and finally convert Scala set to a parallel version and run the query | |
ParMap groups = spopulation.par().filter(isAdult).groupBy(cityField); | |
System.out.println(groups.mapValues(mapper)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment