Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java streams

the Stream API is used to process collections
of objects. A stream is a sequence of objects
that supports various methods which can be
pipelined to produce the desired result.
  It is very similar to build().perform() 
  method in Actions class. It is chaining
  some operations in an order.

The features of Java stream are:
- A stream is not a data structure instead
it takes input from the Collections, 
Arrays or I/O channels.
- Streams don’t change the original data
structure, they only provide the result
as per the pipelined methods.
- Each intermediate operation is lazily
executed and returns a stream as a result,
hence various intermediate operations
can be pipelined. Terminal operations mark
the end of the stream and return the result.

Different Operations On Streams:

map ==> The map method is used to return a
stream consisting of the results of applying
the given function to the elements of this stream.
List number = Arrays.asList(2,3,4,5);
List square = number.stream().map(x->x*x).collect(Collectors.toList());

filter ==> The filter method is used to
select elements as per the Predicate passed as argument.
List names = Arrays.asList("Reflection","Collection","Stream");
List result = names.stream().filter(s->s.startsWith("S")).collect(Collectors.toList());

sorted ==> The sorted method is used to sort the stream.
List names = Arrays.asList("Reflection","Collection","Stream");
List result = names.stream().sorted().collect(Collectors.toList());

ArrayList<String> list3 = new ArrayList(Arrays.asList("monkey", "donkey","onion"));
List<String> list4 = list3.stream().filter(each->!"onion".equals(each)).collect(Collectors.toList());
System.out.println(list4);

static String reverse(String word){
    return Arrays.stream(word.split("")).reduce("", (x,y) -> y+x );
}
Comment

stream java example

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));
     }
}
Comment

Java Stream

// Creates a list of ProgrammingLanguage objects,
// maps it with a method of ProgrammingLanguage to get the name
// and filters them for programming languages, which are not starting with C.


import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;


class ProgrammingLanguage {

    private String name = "";

    public static void main(final String args[]) {
        List<ProgrammingLanguage> programmingLanguages = Arrays.asList(
                new ProgrammingLanguage("Java"),
                new ProgrammingLanguage("Python"),
                new ProgrammingLanguage("C#"),
                new ProgrammingLanguage("JS"),
                new ProgrammingLanguage("C++"),
                new ProgrammingLanguage("C"));
        List<String> nonCProgrammingLanguages = programmingLanguages.stream()  // creates stream from list
                .map(ProgrammingLanguage::getName)
                .filter(name -> !name.startsWith("C"))
                .collect(Collectors.toList());  // convert stream back to list
        System.out.println("Names not starting with C: " + nonCProgrammingLanguages);
    }


    public ProgrammingLanguage(final String name) {
        setName(name);
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }
}
Comment

java streams

Arrays.asList("a1", "a2", "a3")
    .stream()
    .findFirst()
    .ifPresent(System.out::println);  // a1
Comment

Streams in java

//a simple program to demonstrate the use of stream in java
import java.util.*;
import java.util.stream.*;
  
class Demo
{
  public static void main(String args[])
  {
  
    // create a list of integers
    List<Integer> number = Arrays.asList(2,3,4,5);
  
    // demonstration of map method
    List<Integer> square = number.stream().map(x -> x*x).
                           collect(Collectors.toList());
    System.out.println(square);
  
    // create a list of String
    List<String> names =
                Arrays.asList("Reflection","Collection","Stream");
  
    // demonstration of filter method
    List<String> result = names.stream().filter(s->s.startsWith("S")).
                          collect(Collectors.toList());
    System.out.println(result);
  
    // demonstration of sorted method
    List<String> show =
            names.stream().sorted().collect(Collectors.toList());
    System.out.println(show);
  
    // create a list of integers
    List<Integer> numbers = Arrays.asList(2,3,4,5,2);
  
    // collect method returns a set
    Set<Integer> squareSet =
         numbers.stream().map(x->x*x).collect(Collectors.toSet());
    System.out.println(squareSet);
  
    // demonstration of forEach method
    number.stream().map(x->x*x).forEach(y->System.out.println(y));
  
    // demonstration of reduce method
    int even =
       number.stream().filter(x->x%2==0).reduce(0,(ans,i)-> ans+i);
  
    System.out.println(even);
  }
}
Comment

Streams in java

[4, 9, 16, 25]
[Stream]
[Collection, Reflection, Stream]
[16, 4, 9, 25]
4
9
16
25
6
Comment

Stream example in java

import java.util.*; 
public class Main
{
    public static void main(String[] args)
    {  
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); 
        list.stream().map(x -> x + 1).forEach(System.out::println);
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: in java write a code that suppose the following input is supplied to the program: 9 Then, the output should be: 12096 (99+999+9999+999) 
Sql :: sql developer search all packages for text 
Sql :: delete all data in neo4j 
Sql :: PROSYS SQL BKP 
Sql :: mysql how to truncate table with foreign keys 
Sql :: mysql create user with remote access 
Sql :: select not matching data with join table 
Sql :: cannot truncate a table referenced in a foreign key constraint 
Sql :: Find all triggers in database 
Sql :: postgres get all tables in schema 
Sql :: drop stored procedure mysql 
Sql :: uninstall mysql on ubuntu 
Sql :: cambiar nombre tabla mysql 
Sql :: mysql dump all databases 
Sql :: this month mysql where 
Sql :: install mysql ubuntu 18.04 
Sql :: postgres remove database 
Sql :: mysql update field from one table to another 
Sql :: oracle list procedures 
Sql :: sql server list table sizes 
Sql :: sql concate two columns first and last 
Sql :: how to export db from mysql 
Sql :: oracle table statistics 
Sql :: mysql history command 
Sql :: how to update date add hours in postgresql 
Sql :: ora-01950 no privileges on tablespace 
Sql :: find column name in database 
Sql :: drop db syntax 
Sql :: mysql show column data types 
Sql :: set statiscis on in ssms 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =