javacodegeeks.com

Using Java 12 in Eclipse

1. Install JDK 12Link: https://www.oracle.com/technetwork/java/javase/downloads/jdk12-downloads-5295953.html2. Install Eclipse 4.11Link: https://download.eclipse.org/eclipse/downloads/drops4/R-4.11-2019030705003. Install Eclipse Java 12 SupportStart Eclipse and go to Help > Install New Software. Add Update Site: https://download.eclipse.org/eclipse/updates/4.11-P-builds. Install Eclipse Java 12 support for 2019-03 development stream from the list of available software4. Add Java 12

javacodegeeks.com

Java 12: Switch Expressions

In Java 12, the switch statement has been enhanced so that it can be used as an expression. It is now also possible to switch on multiple constants in a single case, resulting in code that is more concise and readable. These enhancements are a preview language feature, which means that they must be explicitly enabled in the Java compiler and runtime using the --enable-preview flag.Consider the following switch

javacodegeeks.com

Java 11: Converting a Collection to an Array

In Java 11, a new default method, toArray(IntFunction), has been added to the java.util.Collection interface, which allows the collection’s elements to be transferred to a newly created array of a desired runtime type.For example:// Java 11 List<String> list = Arrays.asList("foo","bar","baz"); String[] array = list.toArray(String[]::new); // The above is equivalent to: String[] array2 = list.toArray(new String[0]

javacodegeeks.com

Java 11: Running single-file programs and “shebang” scripts

In Java 11, the java launcher has been enhanced to run single-file source code programs directly, without having to compile them first.For example, consider the following class that simply adds its arguments:import java.util.*; public class Add { public static void main(String[] args) { System.out.println(Arrays.stream(args) .mapToInt(Integer::parseInt) .sum()); } }In previous versions of Java, you would first have to compile the source file and

javacodegeeks.com

Java: Streaming a JDBC ResultSet as CSV

In my previous post, I showed how to convert a java.sql.ResultSet to JSON and stream it back to the caller. This post, is about streaming it in CSV format instead. Streaming allows you to transfer the data, little by little, without having to load it all into the server’s memory.For example, consider the following ResultSet:+---------+-----+ | Name | Age | +---------+-----+ | Alice | 20 | | Bob | 35

javacodegeeks.com

Java 11: New HTTP Client API

In Java 11, the incubated HTTP Client API first introduced in Java 9, has been standardised. It makes it easier to connect to a URL, manage request parameters, cookies and sessions, and even supports asynchronous requests and websockets.To recap, this is how you would read from a URL using the traditional URLConnection approach:var url = new URL("http://www.google.com"); var conn = url.openConnection(); try (var in = new

javacodegeeks.com

Java: Mocking a ResultSet using Mockito

This post shows how you can mock a java.sql.ResultSet using Mockito. It can be used to help unit test code which performs operations on ResultSets (such as a ResultSetExtractor) without relying on an external datasource.You can create a MockResultSet by providing a list of column names and a 2D array of data. For example:var rs = MockResultSet.create( new String[] { "name", "age" }, //columns new Object[][] { // data { "Alice"

javacodegeeks.com

Java 10: Collecting a Stream into an Unmodifiable Collection

Java 10 introduces several new methods to facilitate the creation of unmodifiable collections.The List.copyOf, Set.copyOf, and Map.copyOf methods create new collection instances from existing instances. For example:List<String> modifiable = Arrays.asList("foo", "bar"); List<String> unmodifiableCopy = List.copyOf(list); // Note that since Java 9, you can also use "of" to create // unmodifiable collections List<String> unmodifiable = List.of("foo"

javacodegeeks.com

Java 9: Enhancements to the Process API

Java 9 brings various improvements to the Process API, used for controlling and managing operating system processes.Getting information about a processThere is a new ProcessHandle class which provides the process’s pid, parent and descendants, as well as information about the start time and accumulated CPU time.jshell> Process p = new ProcessBuilder("stress", "--cpu", "4", "--timeout", "5").start(); p ==> Process[pid=5572, exitValue="not exited"] jshell&