Skip to content

Instantly share code, notes, and snippets.

@dlhartveld
dlhartveld / NondeterministicStream.java
Last active December 15, 2015 06:49
Stateful lambdas lead to non-deterministic stream behavior.
List<String> ss = ...;
List<String> result = ...;
Stream<String> stream = ss.stream();
stream.map(s -> {
synchronized (result) {
if (result.size() < 10) {
result.add(s);
}
@dlhartveld
dlhartveld / IteratingBlocks.java
Last active December 15, 2015 03:59
Iterating (for-loop) version of the Stream API example. See also: https://gist.github.com/dlhartveld/5198058
List<Block> blocks = /* ... */;
int sumOfWeights = 0;
for (Block block : blocks) {
if (block.getColor() == Color.RED) {
sumOfWeights += block.getWeight();
}
}
@dlhartveld
dlhartveld / StreamingBlocks.java
Last active December 15, 2015 03:59
Simple stream API example from the JDK 8 javadocs (see http://lambdadoc.net/api/index.html?java/util/stream/package-summary.html, b79, retrieved 2013-03-19). See for a traditional for-loop-based example: https://gist.github.com/dlhartveld/5198093
List<Block> blocks = /* ... */;
int sumOfWeights = blocks.stream()
.filter(b -> b.getColor() == Color.RED)
.map(b -> b.getWeight())
.sum();
@dlhartveld
dlhartveld / AnonymousInnerClass.java
Last active December 14, 2015 08:29
Example of interface implementation as anonymous inner class instatiation in Java 7.
java.util.function.BiFunction<String, String, String> concat
= new java.util.function.BiFunction<String, String, String>() {
@Override
public String apply(String t, String u) {
return t + u;
}
};
@dlhartveld
dlhartveld / MethodReferences.java
Last active December 14, 2015 08:29
Simple example of method references with JDK8.
public class MethodReferences {
public void f() { }
public void g() {
Runnable r = this::f;
r.run(); // f() is called.
}
}
@dlhartveld
dlhartveld / Streams.java
Last active December 14, 2015 08:29
Example usage of the Stream API from JDK8.
List<String> ss = new ArrayList<>();
ss.add("x,y,z");
ss.add("x.y.z");
ss.add("a,b,c");
ss.add("i,j,k");
String result = ss.stream()
.map(s -> s.replace(",", "."))
.filter(s -> !s.contains("b"))
.distinct()
@dlhartveld
dlhartveld / ObservableTest.java
Created February 28, 2013 15:16
A few simple test cases for the Observable from https://gist.github.com/dlhartveld/5057313. Uses JUnit and Mockito.
package com.hartveld.examples;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import java.util.function.Consumer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
@dlhartveld
dlhartveld / Observable.java
Last active December 14, 2015 08:19
Implementation of an Observable with data stream methods, similar in its idea to .NET's reactive extensions. Note: this example only demonstrates the expressive power of Java 8's functional types, default methods and lambda expressions - it does not demonstrate a pattern that should be used in general.
package com.hartveld.examples;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
public interface Observable<T> {
void subscribe(Consumer<T> onNext);
@dlhartveld
dlhartveld / LambdaIterable.java
Last active December 14, 2015 08:19
JDK8 example: implementing an empty Iterable<Object> (functional interface with default method) with a lambda expression.
interface java.lang.Iterable<T> {
abstract Iterator<T> iterator();
default void forEach(Consumer<? super T> consumer) {
for (T t : this) {
consumer.accept(t);
}
}
}
@dlhartveld
dlhartveld / LambdaConcat.java
Last active July 17, 2022 18:53
Example Java 8 lambda expression: concatenation of two strings. The first version explicitly defines types and a method body. The second version omits these. It uses implicit typing for its parameters, and lacks the return statement.
java.util.function.BiFunction<String, String, String> concat1
= (String s, String t) -> {
return s + t;
};
java.util.function.BiFunction<String, String, String> concat2
= (s, t) -> s + t;