servlets struts hibernate jsf spring maven
 

Java 8 - Streams


A Stream in Java can be characterized as a grouping of components from a source that underpins total tasks on them.

you can process information decisively like SQL statements.

What is Stream?

Stream speaks to a grouping of articles from a source, which bolsters total activities. Following are the attributes of a Stream −

  • Sequence of elements − A stream gives a lot of components of explicit kind in a consecutive way. A stream gets/figures components on interest. It never stores the elements.

  • Source − Stream takes Collections, Arrays, or I/O assets as info source.

  • Aggregate operations − Stream bolsters total tasks like channel, map, limit, decrease, discover, coordinate, thus on.

  • Pipelining − Most of the stream operations return stream itself so that their result can be pipelined. These operations are called intermediate operations and their function is to take input, process them, and return output to the target. collect() method is a terminal operation which is normally present at the end of the pipelining operation to mark the end of the stream.

  • Automatic iterations − Stream operations do the iterations internally over the source elements provided, in contrast to Collections where explicit iteration is required.

Different ways to create stream

Stream.of(val1, val2, val3….)
public class StreamBuilders
{
     public static void main(String[] args)
     {
         Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9);
         stream.forEach(p -> System.out.println(p));
     }
}

2.2. Stream.of(arrayOfElements)

public class StreamBuilders
{
     public static void main(String[] args)
     {
         Stream<Integer> stream = Stream.of( new Integer[]{1,2,3,4,5,6,7,8,9} );
         stream.forEach(p -> System.out.println(p));
     }
}

2.3. List.stream()

public class StreamBuilders
{
     public static void main(String[] args)
     {
         List<Integer> list = new ArrayList<Integer>();
 
         for(int i = 1; i< 10; i++){
             list.add(i);
         }
 
         Stream<Integer> stream = list.stream();
         stream.forEach(p -> System.out.println(p));
     }
}

2.4. Stream.generate() or Stream.iterate()

public class StreamBuilders
{
     public static void main(String[] args)
     {
         Stream<Date> stream = Stream.generate(() -> { return new Date(); });
         stream.forEach(p -> System.out.println(p));
     }
}

2.5. String chars or String tokens

public class StreamBuilders
{
     public static void main(String[] args)
     {
        IntStream stream = "12345_abcdefg".chars();
        stream.forEach(p -> System.out.println(p));
         
        //OR
         
        Stream<String> stream = Stream.of("A$B$C".split("\\$"));
        stream.forEach(p -> System.out.println(p));
     }
}

Stream Example

Create the following Java program using any editor of your choice in, say, C:\> JAVA.

import java.util.ArrayList; 

import java.util.Arrays; 

import java.util.IntSummaryStatistics; 

import java.util.List; 

import java.util.Random; 

import java.util.stream.Collectors; 

import java.util.Map; 

public class Java8Tester { 

public static void main(String args[]) { 

System.out.println("Using Java 7: "); 

//Count void strings 

List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); 

System.out.println("List: " +strings); 

long check = getCountEmptyStringUsingJava7(strings); 

System.out.println("Empty Strings: " + count); 

check = getCountLength3UsingJava7(strings); 

System.out.println("Strings of length 3: " + count); 

//Eliminate void string 

List<String> separated = deleteEmptyStringsUsingJava7(strings); 

System.out.println("Filtered List: " + filtered); 

//Eliminate void string and join utilizing comma. 

String mergedString = getMergedStringUsingJava7(strings,", "); 

System.out.println("Merged String: " + mergedString); 

List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5); 

//get rundown of square of particular numbers 

List<Integer> squaresList = getSquares(numbers); 

System.out.println("Squares List: " + squaresList); 

List<Integer> whole numbers = Arrays.asList(1,2,13,4,15,6,17,8,19); 

System.out.println("List: " +integers); 

System.out.println("Highest number in List : " + getMax(integers)); 

System.}

Verify the Result

Compile the class utilizing javac compiler as pursues −

C:\JAVA>javac Java8Tester.java 

Now run the Java8Tester as pursues −

C:\JAVA>java Java8Tester 

It should create the accompanying outcome −

Using Java 7: 

Rundown: [abc, , bc, efg, abcd, , jkl] 

Void Strings: 2 

Strings of length 3: 3 

Separated List: [abc, bc, efg, abcd, jkl] 

Blended String: abc, bc, efg, abcd, jkl 

Squares List: [9, 4, 49, 25] 

Rundown: [1, 2, 13, 4, 15, 6, 17, 8, 19] 

Most elevated number in List : 19 

Least number in List : 1 

Whole of all numbers : 85 

Normal of all numbers : 9 

Arbitrary Numbers: 

- 1279735475 

903418352 

- 1133928044 

- 1571118911 

628530462 

18407523 

- 881538250 

- 718932165 

270259229 

421676854 

Utilizing Java 8: 

Rundown: [abc, , bc, efg, abcd, , jkl] 

Void Strings: 2 

Strings of length 3: 3 

Sifted List: [abc, bc, efg, abcd, jkl] 

Consolidated String: abc, bc, efg, abcd, jkl 

Squares List: [9, 4, 49, 25] 

Rundown: [1, 2, 13, 4, 15, 6, 17, 8, 19] 

Most noteworthy number in List : 19 

Most reduced number in List : 1 

Entirety of all numbers : 85 

Normal of all numbers : 9.444444444444445 

Arbitrary Numbers: 

- 1009474951 

- 551240647 

- 2484714 

181614550 

933444268 

1227850416 

1579250773 

1627454872 

1683033687 

1798939493 

Void Strings: 2 

Run precedent »





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition

JAVACODEGEEKS
EmberJS Tutorial