java 8 stream sorted method example

The java.util.stream is a sequence of elements supporting sequential and parallel aggregate operations. The Stream.sorted() method returns a sorted Stream. Java 8 stream sorted method example package com.w3schools; import java.util.stream.Stream; public class Test{ public static void main(String[] args) { Stream.of(“Jai”, “Mahesh”, “Vishal”, “Naren”, “Hemant”) .sorted() .forEach(System.out::println); } } Output Hemant Jai Mahesh Naren Vishal

java 8 stream distinct method example

The Stream.distinct() method returns a stream consisting of the distinct elements of this stream. It uses Object.equals() method. java 8 stream distinct method example package com.w3schools; import java.util.stream.Stream; public class Test{ public static void main(String[] args) { Stream.of(“Jai”, “Mahesh”, “Vishal”, “Naren”, “Hemant”, “Naren”, “Vishal”) .distinct() .forEach(System.out::println); } } Output Jai Mahesh Vishal Naren Hemant

java 8 stream flatmap method example

The java.util.stream is a sequence of elements supporting sequential and parallel aggregate operations. Java 8 stream flatmap method example package com.w3schools; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Arrays; public class Test{ public static void main(String[] args) { try { long noOfWords = Files.lines(Paths.get(“D:\\test.txt”)) .flatMap(l->Arrays.stream(l.split(” +”))) .distinct() .count(); System.out.println(“No Of Words: “+noOfWords); } catch (Exception e) { … Read more

java 8 stream map example

The java.util.stream is a sequence of elements supporting sequential and parallel aggregate operations. java 8 stream map example package com.w3schools; import java.util.Arrays; import java.util.List; public class Test{ public static void main(String[] args) { List names = Arrays.asList(“Jai”, “Mahesh”, “Vishal”, “Hemant”, “Naren”); names.stream().map(String::toUpperCase).forEach(System.out::println); } } Output JAI MAHESH VISHAL HEMANT NAREN

java 8 stream filter example

The java.util.stream is a sequence of elements supporting sequential and parallel aggregate operations. Java 8 stream filter example package com.w3schools; import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; class Student{ int rollNo; String name; public Student(int rollNo, String name){ super(); this.rollNo = rollNo; this.name = name; } } public class Test { public static void main(String args[]){ … Read more

java 8 create stream using list example

The java.util.stream is a sequence of elements supporting sequential and parallel aggregate operations. Java 8 create stream using list example package com.w3schools; import java.util.Arrays; import java.util.List; import java.util.stream.Stream; public class Test{ public static void main(String[] args) { List names = Arrays.asList(“Vishal”,”Naren”); Stream namesStream = names.stream(); namesStream.forEach(System.out::println); } } Output Vishal Naren

java 8 create stream example

The java.util.stream is a sequence of elements supporting sequential and parallel aggregate operations. Java 8 create stream example package com.w3schools; import java.util.stream.Stream; public class Test{ public static void main(String[] args) { //Create Stream using Stream.of Stream names = Stream.of(“Jai”,”Hemant”); names.forEach(System.out::println); System.out.println(“————–“); //Create stream using array of elements Stream intStream1 = Stream.of(new Integer[]{1,2,3,4,5}); intStream1.forEach(System.out::println); } } … Read more

java 8 stream API tutorial

Before starting Java Stream API let’s look why it was required. Suppose we want to iterate over a list of strings and want to count strings with length less than 5. package com.w3schools; import java.util.ArrayList; import java.util.List; public class Test{ public static void main(String[] args) { List names = new ArrayList(); names.add(“Jai”); names.add(“Mahesh”); names.add(“Ajay”); names.add(“Hemant”); … Read more

Java 8 Interface Changes default method and static method

Why default method? For example we have an interface which has multiple methods, and multiple classes are implementing this interface. One of the method implementation can be common across the class. We can make that method as a default method, so that the implementation is common for all classes. Second scenario where we have already … Read more

Java 8 Method References

Java 8 introduced a new feature “Method Reference” which is used to refer the methods of functional interfaces. It is a shorthand notation of a lambda expression to call a method. We can replace lambda expression with method reference (:: operator) to separate the class or object from the method name. Types of method references … Read more