Java 8 features with examples

Java Platform, Standard Edition 8 is a major feature release of Java programming language development. Its initial version was released on 18 March 2014. Here is the list of Java 8 features with examples. Java 8 features Java 8 functional interface. Java lambda expression. Java lambda expression hello world. Java lambda expression multiple parameters. Java … Read more

Java 8 Type Inference improvements

Type Interface feature was introduced in Java 7 which provides ability to compiler to infer the type of generic instance. We can replace the type arguments with an empty set of type parameters () diamond. Before Java 7 following approach was used: List<Integer> list = new List<Integer>();List<Integer> list = new List<Integer>(); We can use following … Read more

Java 8 Base64 example

Java 8 provides a new feature Base64 to deal with decryption and encryption. Java 8 Base64 class provides three different encoders and decoders to encrypt information at each level. Simple Encoding and Decoding: It uses the Base64 alphabet specified by Java in RFC 4648 and RFC 2045 for encoding and decoding operations. The encoder does … Read more

Java Parallel Array Sorting

Java 8 provides a new additional method parallelSort() in the Arrays class of java.util package. It is introduced to support the parallel sorting of array elements. The parallelSort() method uses the concept of multithreading which makes it much faster compared to the normal sort when there are lot of elements. Java parallel array sorting example … Read more

Java 8 Optional Class

Java 8 introduced a new public final class Optional in java.util package. It is used to deal with NullPointerException in java application. It provides the methods to easily check whether a variable has null value or not. Commonly used methods of Java Optional class: Optional.ofNullable(): It returns a Non-empty Optional if the given object has … Read more

Java 8 StringJoiner class

Java 8 introduced a new class StringJoiner in the java.util package which provides the facility to join more than one strings with the specified delimiter. We can also use prefix and suffix to the final string while joining multiple strings. Joining strings by specifying delimiter package com.w3spoint;   import java.util.StringJoiner;     public class TestExample … Read more

Java 8 collectors class

Collectors is a final class that extends the Object class which provides reduction operations, such as accumulating elements into collections, summarizing elements according to various criteria, grouping etc. Java Stream Collectors groupingBy and counting example package com.w3spoint;   import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors;   public class Test{ public static void … Read more

Java 8 forEach example

Java 8 introduced forEach method to iterate over the collections and Streams in Java. It is defined in Iterable and Stream interface. It is a default method defined in the Iterable interface. Collection classes which extends Iterable interface can use forEach loop to iterate elements. Java 8 forEach example package com.w3spoint;   import java.util.HashMap; import … Read more

java 8 stream forEach method example

The java.util.stream is a sequence of elements supporting sequential and parallel aggregate operations. The Stream.forEach() method works same as of for loop. It iterates through each element and performs the specified operations. Java 8 stream forEach method example package com.w3spoint;   import java.util.Arrays; import java.util.List;   public class Test{ public static void main(String[] args) { … Read more

java 8 stream limit method example

The java.util.stream is a sequence of elements supporting sequential and parallel aggregate operations. The Stream.limit() method returns a Stream with elements of the source stream. Java 8 stream limit method example package com.w3spoint;   import java.util.stream.Stream;   public class Test{ public static void main(String[] args) { Stream.of("Jai", "Mahesh", "Vishal", "Naren", "Hemant") .limit(3) .forEach(System.out::println); } }package … Read more