Skip to content

Instantly share code, notes, and snippets.

public Optional<String> getEmail() {
return Optional.ofNullable(email);
}
public Optional<String> getPhone() {
return Optional.ofNullable(phone);
}
//Address has priority , first home and then Office
if (p.home != null) {
System.out.println("Contacted at home address " + p.home.address);
return; // Magical return for early exit
}
if (p.office != null) {
System.out.println("Contacted at office address " + p.office.address);
return; // Magical return for early exit
//Nested Property
if (p.getHome() != null) {
System.out.println("Sending Postal mail " + p.getHome().address);
}
if (p.getHome() != null && p.getHome().getInsurance() != null) {
System.out.println("Sending Notification to insurance " + p.getHome().getInsurance().getAgency());
}
//Always Happy
Optional<String> phone = contactNumber(p);
Optional<String> email = email(p);
System.out.println("Calling Phone " + phone.get());
System.out.println("Sending Email " + email.get());
Optional<String> phone = contactNumber(p);
Optional<String> email = email(p);
if (phone.isPresent()) {
System.out.println("Calling Phone " + phone.get());
}
if (email.isPresent()) {
System.out.println("Sending Email " + email.get());
}
//Not using optional
if (p.email != null) {
System.out.println("Sending email to " + p.email);
}
if (p.phone != null) {
System.out.println("Calling " + p.phone);
}
public class Person {
final String firstName;
final String lastName;
final String email; // This can be null
final String phone; //This can be null
}
public class VerifyCurrentGC {
public static void main(String... args) {
var gcBeans = ManagementFactory.getGarbageCollectorMXBeans();
gcBeans.stream().forEach(gc -> {
out.println(format("GC Name : %s", gc.getName()));
var poolNames = gc.getMemoryPoolNames();
public class MemoryAllocator {
public static final int KB = 1024;
static int mbToAllocate = Integer.getInteger("mb", 1000);
public static void main(String[] args) {
System.out.println(String.format("Start allocation of %s MBs", mbToAllocate));
for (var i = 0; i < mbToAllocate; i++) {
var garbage = new byte[KB * KB];
listener, listenError := net.ListenTCP("tcp", addr)
if listenError != nil {
return nil, fmt.Errorf("Listen: %s", listenError)
}