Skip to content

Instantly share code, notes, and snippets.

Map then collect
+----------+-------------+-------------+-------------+
| Language | Benchmark | Score | Error |
+----------+-------------+-------------+-------------+
| Java | Collections | 1,156,656 | 18,053 | <-- (3)
| Java | Streams | 922,486 | 13,691 |
| Scala | Collections | 435,280 | 8,956 |
| Scala | Streams | 68,349,631 | 2,182,853 | <-- (2)
| Scala | Views | 141,833,408 | 1,110,245 | <-- (1)
+----------+-------------+-------------+-------------+
Group
+----------+-------------+---------+---------+
| Language | Benchmark | Score | Error |
+----------+-------------+---------+---------+
| Java | Collections | 509,416 | 9,455 | <-- (2)
| Java | Streams | 583,236 | 27,646 | <-- (1)
| Scala | Collections | 419,362 | 30,810 | <-- (3)
| Scala | Streams | 117,557 | 7,985 |
| Scala | Views | 357,861 | 4,734 |
+----------+-------------+---------+---------+
Sort then collect
+----------+-------------+---------+--------+
| Language | Benchmark | Score | Error |
+----------+-------------+---------+--------+
| Java | Collections | 173,910 | 2,738 | <-- (1)
| Java | Streams | 71,031 | 995 |
| Scala | Collections | 92,693 | 1,079 | <-- (3)
| Scala | Streams | 53,167 | 1,220 |
| Scala | Views | 108,023 | 1,595 | <-- (2)
+----------+-------------+---------+--------+
Filter Then Count
+----------+------------------------+-----------+---------+
| Language | Benchmark | Score | Error |
+----------+------------------------+-----------+---------+
| Java | Collections | 1,510,186 | 38,309 | <-- (3)
| Java | Collections (removeIf) | 1,211,592 | 122,352 |
| Java | Streams | 5,145,467 | 83,952 | <-- (1)
| Scala | Collections | 3,603,865 | 66,801 | <-- (2)
| Scala | Streams | 234,396 | 2,542 |
| Scala | Views | 1,079,969 | 23,248 |
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
/**
*
* @author Hussachai
*
def fibFrom(a: Int, b: Int): Stream[Int] = a #:: fibFrom(b, a + b)
val fib1 = fibFrom(0, 1) //0 1 1 2 3 5 8 …
val fib5 = fibFrom(0, 5) //0 5 5 10 15 …
//fib1.force //Don’t do this cause it will call the function infinitely and soon you will get the OutOfMemoryError
//fib1.size //Don’t do this too with the same reason as above.
fib1.take(10) //Do this. It will take the first 10 from the inifite Stream.
fib1.take(20).foreach(println(_)) //Prints 20 first numbers
//Java 8 - Collection
pets.removeIf(pet -> !(pet.getBirthdate().isBefore(LocalDate.of(2013, Month.JANUARY, 1))
&& pet.getWeight() > 50));
//Applying De-Morgan's law.
pets.removeIf(pet -> pets.get(0).getBirthdate().toEpochDay() >= LocalDate.of(2013, Month.JANUARY, 1).toEpochDay()
|| pet.getWeight() <= 50);
//Java 8 - Stream
pets.stream()
.filter(pet -> pet.getBirthdate().isBefore(LocalDate.of(2013, Month.JANUARY, 1)))
.filter(pet -> pet.getWeight() > 50)
.collect(toList())
//Scala - strict collection
pets.filter { pet => pet.getBirthdate.isBefore(LocalDate.of(2013, Month.JANUARY, 1))}
.filter { pet => pet.getWeight > 50 } //List[Pet]
//Scala - non-strict collection
pets.views.filter { pet => pet.getBirthdate.isBefore(LocalDate.of(2013, Month.JANUARY, 1))}
.filter { pet => pet.getWeight > 50 } //SeqView[Pet]
//Scala - stream
pets.toStream.filter { pet => pet.getBirthdate.isBefore(LocalDate.of(2013, Month.JANUARY, 1))}
.filter { pet => pet.getWeight > 50 } //Stream[Pet]
//Before Java 8
List<Pet> tmpList = new ArrayList<>();
for(Pet pet: pets){
if(pet.getBirthdate().isBefore(LocalDate.of(2013, Month.JANUARY, 1))
&& pet.getWeight() > 50){
tmpList.add(pet);
}
}