Skip to content

Instantly share code, notes, and snippets.

@KrishnB
Created January 3, 2020 06:52
Show Gist options
  • Save KrishnB/2118261a305ee2af2cd98bb21c84a06f to your computer and use it in GitHub Desktop.
Save KrishnB/2118261a305ee2af2cd98bb21c84a06f to your computer and use it in GitHub Desktop.
// Any local variable declaration
List<String> list = new ArrayList<String>();
Stream<String> stream = getStream()
// For Lambda Expression.
(String a, String b) -> a.concat(b)
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://openjdk.java.net/"))
.build();
client.sendAsync(request, BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
public class HelloUniverse{
public static void main(String[] args) {
System.out.println("Hello InfoQ Universe");
}
}
/**
* Before Java 11:
* Compile:$javac HelloUniverse.java
* Run:$java HelloUniverse
* O/p:Hello InfoQ Universe
* In Java 11:
* Run:$java HelloUniverse.java
* O/p:Hello InfoQ Universe
*/
public static void main(String[] args) {
List<Employee> employeeList = Arrays.asList(
new Employee(1, "A", 100),
new Employee(2, "B", 200),
new Employee(3, "C", 300),
new Employee(4, "D", 400));
HashMap<String, Employee> result = employeeList.stream().collect(
Collectors.teeing(
Collectors.maxBy(Comparator.comparing(Employee::getSalary)),
Collectors.minBy(Comparator.comparing(Employee::getSalary)),
(e1, e2) -> {
HashMap<String, Employee> map = new HashMap();
map.put("MAX", e1.get());
map.put("MIN", e2.get());
return map;
}
));
System.out.println(result);
}
import org.openjdk.jmh.annotations.Benchmark;
public class MyBenchmark {
@Benchmark
public void testMethod() {
// This is a demo/sample template for building your JMH benchmarks. Edit as needed.
// Put your benchmark code here.
int a = 1;
int b = 2;
int sum = a + b;
}
}
public String getTextViaArrow(int number) {
String result = switch (number) {
case 1, 2 -> "one or two";
case 3 -> "three";
case 4, 5, 6 -> "four or five or six";
default -> "unknown";
};
return result;
}
# Note: Its preview feature and not going to run in your ide directly.
# Follow below steps to run.
# $javac --enable-preview --release 13 SwitchExpression.java
# $ java --ennable-preview SwitchExpression
public String getTextViaBreak(int number) {
String result = switch (number) {
case 1:
case 2:
break "one or two";
case 3:
break "three";
case 4:
case 5:
case 6:
break "four or five or six";
default:
break "unknown";
};
return result;
}
./listallfiles
#!/usr/bin/java --source 11
import java.nio.file.*;
public class ListFiles {
public static void main (String ... args) throws Exception
{
Files.walk(Path.get(args[0]))
.forEaach(System.out::println);
}
}
How to Run:
$chmod +x listallfiles
$./listallfiles . // Here "." is a current directory.
// Any local variable declaration.
var list = new ArrayList<String>();
var stream = getStream();
// For Lambda Expression.
(var a, var b) -> a.concat(b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment