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.w3schools; import java.util.HashMap; import java.util.Map; … 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.w3schools; import java.util.Arrays; import java.util.List; public class Test{ public static void main(String[] args) { List names … 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.w3schools; 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); } } Output Jai … Read more

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