Skip to content

Instantly share code, notes, and snippets.

package org.openjdk.jmh.samples;
import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class Pet {
public class Pet {
public static enum Type {
CAT, DOG
}
public static enum Color {
BLACK, WHITE, BROWN, GREEN
}
private String name;
private Type type;
private LocalDate birthdate;
//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);
}
}
//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]
//Java 8 - Stream
pets.stream()
.filter(pet -> pet.getBirthdate().isBefore(LocalDate.of(2013, Month.JANUARY, 1)))
.filter(pet -> pet.getWeight() > 50)
.collect(toList())
//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);
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
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
/**
*
* @author Hussachai
*
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 |
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)
+----------+-------------+---------+--------+