Skip to content

Instantly share code, notes, and snippets.

View stuart-marks's full-sized avatar

Stuart Marks stuart-marks

View GitHub Profile
@stuart-marks
stuart-marks / FizzBuzz.java
Created March 20, 2014 04:11
Yet another FizzBuzz, using Java 8 lambda and streams.
import java.util.function.IntFunction;
import java.util.stream.IntStream;
public class FizzBuzz {
static <R> IntFunction<R> ifmod(int m, R r, IntFunction<R> f) {
return (int i) -> (i % m == 0) ? r : f.apply(i);
}
public static void main(String[] args) {
IntStream.rangeClosed(1, 100)
@stuart-marks
stuart-marks / DynamicFiltering.java
Created April 8, 2014 00:12
Dynamic filtering using Java 8 streams.
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
import static java.util.stream.Collectors.*;
import static java.util.Comparator.*;
/**
* http://stackoverflow.com/questions/22845574/how-to-dynamically-do-filtering-in-java-8
*
@stuart-marks
stuart-marks / SequenceTests.java
Last active March 8, 2021 21:25
Ken Fogel's collections benchmarks rewritten in JMH
package com.kenfogel.performance.loaders;
import java.util.*;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.*;
/**
* Performs a set of tests to determine the Big-O performance of an array list, array deque,
* and linked list.
*
$ cat Printf.java
public class Printf {
public static void main(String[] args) {
System.out.println("Locale.getDefault()=" + java.util.Locale.getDefault());
System.out.printf("%.3f%n", 123.45);
}
}
$ javac Printf.java
$ java -version
@stuart-marks
stuart-marks / FlatMapping.java
Created April 11, 2018 21:28
Utilities for streams flatmapping from collections, arrays, and optionals.
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;
// Suppose we have an API where:
// - a Clazz has a List of Methods,
@stuart-marks
stuart-marks / MasterClass.java
Created November 10, 2018 06:29
data and utilities for Lambda/Streams Master Class
package masterclass;
import java.util.*;
import java.util.stream.*;
import java.util.Map.Entry;
import static java.util.Map.entry;
import static java.util.stream.Collectors.*;
public class MasterClass {
@stuart-marks
stuart-marks / ReadFileJavaApplicationBufferedReader7.java
Last active May 3, 2021 10:39
Processing Large Files in Java, Variation 7
package com.example.readFile.readFileJava;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
@stuart-marks
stuart-marks / ReadFileJavaApplicationBufferedReader.diff12.java
Last active January 7, 2019 00:06
Processing Large Files in Java, Variation 2
--- ReadFileJavaApplicationBufferedReader1.java
+++ ReadFileJavaApplicationBufferedReader2.java
@@ -29,17 +29,12 @@
// get total line count
Instant lineCountStart = Instant.now();
- int lines = 0;
Instant namesStart = Instant.now();
ArrayList<String> names = new ArrayList<>();
@stuart-marks
stuart-marks / ReadFileJavaApplicationBufferedReader.diff01.java
Last active January 7, 2019 00:06
Processing Large Files in Java, Variation 1
--- ReadFileJavaApplicationBufferedReader0.java
+++ ReadFileJavaApplicationBufferedReader1.java
@@ -57,8 +57,8 @@
// System.out.println(readLine);
// get all the names
- String array1[] = readLine.split("\\s*\\|\\s*");
- String name = array1[7];
+ String array1[] = readLine.split("\\|", 9);
+ String name = array1[7].strip();
@stuart-marks
stuart-marks / ReadFileJavaApplicationBufferedReader.diff23.java
Last active January 7, 2019 00:06
Processing Large Files in Java, Variation 3
--- ReadFileJavaApplicationBufferedReader2.java
+++ ReadFileJavaApplicationBufferedReader3.java
@@ -44,6 +45,7 @@
Instant commonNameStart = Instant.now();
ArrayList<String> firstNames = new ArrayList<>();
+ var namePat = Pattern.compile(", \\s*(([^ ]*), |([^ ]+))");
System.out.println("Reading file using " + Caller.getName());