So I was just learning new Java 8, specially lambdas and date and time api. I was comparing it with scala. My basic idea was to find the execution time difference between imperative, Stream and parallel stream. So I decided to create a Library application and do some operations like searching, filtering, sorting etc. I created a Library class with a list field called books and populated it with 1000 books. Then created a functional interface for search and did some operation in all three styles. It all worked fine. My code is:
// Functional Interface
interface Search<T> {
public void search(T t);
}
// Library class
final Library library = new Library();
// This just creates some random book objects.
final List<Book> books = collectBooks();
final Search<List<Book>> parallelSearch = (bks) -> library.findAndPrintBooksParallel(bks);
// Parallel Operations
private void findAndPrintBooksParallel(List<Book> books) {
books.parallelStream()
.filter(b -> b.getAuthor().equals("J.K. Rowling"))
.sorted((x,y) -> x.getAuthor().compareTo(y.getAuthor()))
.map(Book::getIsbn)
.forEach(Library::waitAndPrintRecord);
}
Now I tried to re-create the same program in scala and see whether the execution is faster or not? Surprisingly scala didn't allow me to do parallel sorting (Or may be I'm being ignorant here). My scala library is
// Again some random book objects as a list
val books = collectBooks
// Parallel operation
books.par filter(_.author == "J.K. Rowling") map (_.isdn) foreach waitAndPrint
Here the books.par
gives a ParSeq. This doesn't have a sort method. Is there any way I could create a parallel sort with my books list in scala. So I could write something like:
books.par filter(_.author == "J.K. Rowling") sortWith (_.author < _.author) map (_.isdn) foreach waitAndPrint
Your help is much appreciated. Thanks.
sorted
is provided byscala.collection.SeqLike
only, and it simply delegates tojava.util.Arrays.sort(T[] a, Comparator<? super T> c)
: GitHub.Com/Scala/Scala/blob/master/src/library/scala/collection/… So, the sort itself will never be parallel, only the conversion to and from the Java array might. – Jörg W Mittag