Created
January 3, 2020 06:52
-
-
Save KrishnB/2118261a305ee2af2cd98bb21c84a06f to your computer and use it in GitHub Desktop.
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
// Any local variable declaration | |
List<String> list = new ArrayList<String>(); | |
Stream<String> stream = getStream() | |
// For Lambda Expression. | |
(String a, String b) -> a.concat(b) |
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
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(); |
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 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 | |
*/ |
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 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); | |
} |
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
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; | |
} | |
} |
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 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 |
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 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; | |
} |
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
./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. |
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
// 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