Last active
February 23, 2022 05:26
-
-
Save shelajev/c58afed75aea3387fcac to your computer and use it in GitHub Desktop.
Java 8 cheat sheet code
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 interface Debuggable { | |
default String debug() { | |
StringBuilder sb = new StringBuilder(this.getClass().getName()); | |
sb.append(" [ "); | |
Field[] fields = this.getClass().getDeclaredFields(); | |
for(Field f: fields) { | |
f.setAccessible(true); | |
try { | |
sb.append(f.getName() + " = " + f.get(this)); | |
sb.append(", "); | |
} catch (IllegalArgumentException | IllegalAccessException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
sb.append("]"); | |
return sb.toString(); | |
} | |
} |
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
// takes a Long, returns a String | |
Function<Long, String> f = l -> l.toString(); | |
// takes nothing gives you Threads | |
Supplier<Thread> s =Thread::currentThread; | |
// takes a string as the parameter | |
Consumer<String> c = System.out::println; |
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
// use them in with streams | |
new ArrayList<String>().stream(). | |
// peek: debug streams without changes | |
peek(e -> System.out.println(e)). | |
// map: convert every element into something | |
map(e -> e.hashCode()). | |
// filter: pass some elements through | |
filter(e -> ((e.hashCode() % 2) == 0)). | |
// collect the stream into a collection | |
collect(Collectors.toCollection(TreeSet::new)) |
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 Main implements Debuggable { | |
int a = 100; | |
String b = "Home"; | |
public static void main(String[] args) { | |
Main m = new Main(); | |
System.out.println(m.debug()); | |
} | |
} |
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
User user = getUser(name); // might return null | |
Location location = null; | |
if(user != null) { | |
location = getLocation(user); | |
} |
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
Optional<User> user = Optional.ofNullable(getUser(user)); | |
Optional<Location> location = user.map(u -> getLocation(user)); |
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 <U> Optional<U> flatMap(Function<? super T,Optional<U>> mapper) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On line Lambdas1.java, line 1 I think should be:
// takes a Long, return a String