Created
November 10, 2020 09:11
-
-
Save tommorris/af4d89da7e752acd215814e69d5ae054 to your computer and use it in GitHub Desktop.
A minimal example of modern Java: type inference, streams, static methods as arguments etc.
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 org.example; | |
import java.util.Arrays; | |
import java.util.Random; | |
import java.util.function.Function; | |
import java.util.stream.Collectors; | |
public class LetterIntersperser { | |
private boolean def; | |
private boolean current; | |
private LetterIntersperser() { | |
this.def = (new Random().nextInt(1)) == 1; | |
this.current = this.def; | |
} | |
private Function<String, String> next() { | |
this.current = !this.current; | |
return this.current ? String::toLowerCase : String::toUpperCase ; | |
} | |
public static String call(String str) { | |
var intersperser = new LetterIntersperser(); | |
return Arrays.stream(str.split("")) | |
.map(i -> intersperser.next().apply(i) ) | |
.collect(Collectors.joining("")); | |
} | |
} |
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 org.example; | |
import java.util.Arrays; | |
public class Main { | |
public static void main(String[] args) { | |
var myList = "foo bar baz flannel"; | |
Arrays.stream(myList.split(" ")) | |
.sorted() | |
.map(LetterIntersperser::call) | |
.forEach(str -> System.out.println(str)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment