Found 2616 Articles for Java

What are the new methods added to Process API in Java 9?

raja
Updated on 21-Feb-2020 04:57:55

95 Views

Java 9 improves Process class by adding new methods and also provides a new interface: ProcessHandle and ProcessHandle.Info to get all the details about the process and its information.Below is the list of new methods added to Process in Java 9boolean supportsNormalTermination(): It can return true if the implementation of the destroy() is to normally terminate the process, else returns false.long pid(): It can return the native process ID of the process.ProcessHandle toHandle(): It can return a ProcessHandle for the Process.Stream children(): It can return a snapshot of the direct children of the process.Stream descendants(): It can return a snapshot of the descendants ... Read More

What are the advantages of private methods in an interface in Java 9?

raja
Updated on 21-Feb-2020 04:59:01

437 Views

In Java 9, an interface can also have private methods. Apart from static and default methods in Java 8, this is another significant change as it allows the re-usability of common code within the interface itself.In an interface, there is a possibility of writing common code on more than one default method that leads to code duplication. The introduction of private methods avoids this code duplication.Advantages of private methods in an interfaceAvoiding code duplication.Ensuring code re-usability.Improving code readability.Syntaxinterface interfacename {    private methodName(parameters) {       // statements     } }Exampleinterface Test {    default void m1() {       common();    }   ... Read More

Importance of iterate() method of Stream API in Java 9?

raja
Updated on 21-Feb-2020 05:11:39

356 Views

In Java 8, the iterate() method of Stream API takes the seed and the unary operator as arguments. As stream becomes infinite, it makes the developer add explicit termination conditions by using limit, findFirst, findAny and etc. In Java 9, the iterate() method of Stream API has added a new argument, a predicate that takes the condition to break the flow.Syntaxstatic Stream iterate(T seed, Predicate

What are the benefits of immutable collections in Java 9?

raja
Updated on 21-Feb-2020 10:08:12

341 Views

In Java 9, several factory methods have added to Collections API. By using these factory methods, we can create unmodifiable list, set and map collection objects to reduce the number of lines of code. The List.of(), Set.of(), Map.of() and Map.ofEntries() are the static factory methods that provide convenient way of creating immutable collections in Java 9.Benefits of Immutable CollectionsLess heap space: The space required to store a collection data is very less as compared with the traditional approach in earlier versions of java.Faster access to data: As the overhead to store data and wrap into Collections.unmodifiable is reduced, now data access becomes faster. It means that ... Read More

Differences between orTimeout() and completeOnTimeOut() methods in Java 9?

raja
Updated on 21-Feb-2020 05:13:46

1K+ Views

Both orTimeout() and completeOnTimeOut() methods are defined in CompletableFuture class and these two methods are introduced in Java 9. The orTimeout() method can be used to specify that if a given task doesn't complete within a certain period of time, the program stops execution and throws TimeoutException whereas completeOnTimeOut() method completes the CompletableFuture with the provided value. If not, it complete before the given timeout.Syntax for orTimeout()public CompletableFuture orTimeout(long timeout, TimeUnit unit)Exampleimport java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; public class OrTimeoutMethodTest {    public static void main(String args[]) throws InterruptedException {       int a = 10;       int b = 15;   ... Read More

When to use the ofNullable() method of Stream in Java 9?

raja
Updated on 21-Feb-2020 05:31:34

1K+ Views

The ofNullable() method is a static method of Stream class that returns a sequential Stream containing a single element if non-null, otherwise returns an empty. Java 9 has introduced this method to avoid NullPointerExceptions and also avoid null checks of streams. The main objective of using the ofNullable() method is to return an empty option if the value is null.Syntaxstatic Stream ofNullable(T t)Example-1import java.util.stream.Stream; public class OfNullableMethodTest1 {    public static void main(String args[]) {       System.out.println("TutorialsPoint");       int count = (int) Stream.ofNullable(5000).count();       System.out.println(count);       System.out.println("Tutorix");       count = (int) Stream.ofNullable(null).count();   ... Read More

How can we create an unmodifiable Map in Java 9?

raja
Updated on 21-Feb-2020 05:56:23

263 Views

An unmodifiable Map is one whose keys and values can't be added, removed, or updated once an unmodifiable instance of a map has created. The static factory methods: Map.of() and Map.ofEntries() from Map that provides a convenient way to create unmodifiable maps in Java 9.An instance of a map created by using Map.of() and Map.ofEntries() methods has the following characteristics.The map returned by the factory methods is conventionally immutable. It means that keys and values can't be added, removed, or updated. Calling any mutator method on the map causes an UnsupportedOperationException.If the contained keys/values of a map are themselves mutable, it may cause Map to ... Read More

How can we create an unmodifiable List in Java 9?

raja
Updated on 21-Feb-2020 06:02:16

356 Views

A list considered to be unmodifiable if the elements can't be added, removed, or replaced from a list once an unmodifiable instance of a list has created. The static factory method: List.of() provides a convenient way to create unmodifiable lists in Java 9.An instance of a list created by using the List.of() method has the following characteristics.The list returned by a factory method is conventionally immutable. It means that the elements can't be added, removed, or replaced from a list. Calling any mutator method on the List causes UnsupportedOperationException.If the contained elements of List are mutable, it may cause the List's contents to appear to ... Read More

What are new methods have added to the Arrays class in Java 9?

raja
Updated on 24-Feb-2020 06:08:30

164 Views

Arrays class can contain various methods for manipulating arrays and also contains static factory methods that allow arrays to view as a list. Java 9 has added three important methods to Arrays class: Arrays.equals(), Arrays.compare() and Arrays.mismatch().Arrays.equal() − In Java 9, few overloaded methods have added to the Arrays.equals() method. The new methods take fromIndex and toIndex parameters for the two provided arrays. These methods check the equality of the two arrays based on their relative index positions.Syntaxpublic static boolean equals(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)In the above syntax, the method returns true if two specified arrays of ... Read More

How can we create an unmodifiable Set in Java 9?

raja
Updated on 21-Feb-2020 06:23:39

143 Views

The immutable static factory method Set.of() can provide a convenient way to create unmodifiable sets in Java 9. An instance of a set created by using the Set.of() method has the following characteristics.The set returned by a factory method is conventionally immutable. It means that elements can't be added, removed, or replaced from a Set. Calling of any mutator method on the Set causes UnsupportedOperationException.If the contained elements of Set are mutable, it may cause the Set's contents to appear to change.An immutable set can be created using static factory methods that don't allow null elements. If we are trying to create with null elements, it throws NullPointerException.It ... Read More

Advertisements