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
Latency Comparison Numbers | |
-------------------------- | |
L1 cache reference 0.5 ns | |
Branch mispredict 5 ns | |
L2 cache reference 7 ns 14x L1 cache | |
Mutex lock/unlock 25 ns | |
Main memory reference 100 ns 20x L2 cache, 200x L1 cache | |
Compress 1K bytes with Zippy 3,000 ns | |
Send 1K bytes over 1 Gbps network 10,000 ns 0.01 ms | |
Read 4K randomly from SSD* 150,000 ns 0.15 ms |
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 java.util.ArrayList; | |
import java.util.List; | |
public class LINQQueryExample { | |
public static class Student { | |
public String firstName; | |
public String lastName; | |
public int[] scores; |
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
object TweetUtil { | |
def main(args: Array[String]) { | |
val keywords = List("brown", "fox", "dog", "pangram") | |
val tweet = "The quick brown fox jumps over a lazy dog. #pangram http://www.rinkworks.com/words/pangrams.shtml" | |
keywords.exists(tweet.contains) | |
(keywords.foldLeft(false)( _ || tweet.contains(_) )) | |
} | |
} |
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
// take 1 | |
keywords.stream().anyMatch(keyword -> tweet.contains(keyword)); | |
// take 2 using method reference | |
keywords.stream().anyMatch(tweet::contains) | |
// take 3 using reduce() | |
keywords.stream().reduce(false, (Boolean b, String keyword) -> b || tweet.contains(keyword), (l, r) -> l | r); |
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
package java.util.stream; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collection; | |
public class MyList<E> extends ArrayList<E> { | |
public MyList(int initialCapacity) { | |
super(initialCapacity); |
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
package java.util.stream; | |
import java.util.function.Predicate; | |
public interface MyStream<T> extends Stream<T> { | |
default boolean exists(Predicate<? super T> predicate) { | |
return anyMatch(predicate); | |
} | |
} |
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 TweetUtil { | |
public static void main(String[] args) { | |
final List<String> keywords = Arrays.asList("brown", "fox", "dog", "pangram"); | |
final String tweet = "The quick brown fox jumps over a lazy dog. #pangram http://www.rinkworks.com/words/pangrams.shtml"; | |
exists(keywords, tweet); | |
} | |
public static boolean exists(List<String> keywords, String tweet) { |
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
package java.util.stream; | |
import java.util.Spliterator; | |
import java.util.function.Predicate; | |
import java.util.function.Supplier; | |
public class MyReferencePipeline<T, U> extends ReferencePipeline<T, U> implements MyStream<U> { | |
public MyReferencePipeline(Supplier<? extends Spliterator<?>> source, int sourceFlags, boolean parallel) { | |
super(source, sourceFlags, parallel); |
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
List<String> google = Arrays.asList("android", "Android", "galaxy", "Galaxy", "nexus", "Nexus"); | |
List<String> apple = Arrays.asList("ios", "iOS", "iphone", "iPhone", "ipad", "iPad"); | |
List<Tweet> tweets = TweetReader.fromTweetSet(TweetReader.allTweets); | |
// Using anyMatch and method reference | |
List<Tweet> googleTweets = tweets.stream().filter(t -> (google.stream().anyMatch(t.text::contains))).collect(toList()); | |
List<Tweet> appleTweets = tweets.stream().filter(t -> (apple.stream().anyMatch(t.text::contains))).collect(toList()); |
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
// Run with -Xbootclasspath/p:target/classes as ReferencePipeline access is now package local, so use this trick | |
List<Tweet> googleTweets3 = tweets.stream().filter(tweet -> (new MyList<>(google).stream().exists(tweet.text::contains))).collect(toList()); | |
List<Tweet> appleTweets3 = tweets.stream().filter(tweet -> (new MyList<>(apple).stream().exists(tweet.text::contains))).collect(toList()); |