mathajax

Java Lambda expression - Filtering



A lambda expression is an anonymous function which performs arithmetic or relational operation over an array of data in sequential or parallel manner. It replaces for-loop like operation over array objects.

java lambda function
lambda expression


Stream Object The array of data might be a java primitive data type (integer or long or double) or a java collection object (ArrayList). In order to execute the lambda expression over array of data, we need a Stream object (IntStream, LongStream or DoubleStream).


The Java program test Java-8 lambda expression filtering operation over array of integers randomly generated in the range [0 -1000]



The class Filtering defined the following five member function for search on dataset,integer arrays. they are,
   
     1. greater
     2. lesser
     3. range
     4. grtequ (greater than and equal)
     5. lerequ(lesser than and equal)
 
An Object of class Filtering is initialized with 20 randomly integers generated from random object with limit range of value [0-1000] to test filtering operation using Lambda expression by a Stream object obtained from arrays of integers.




import java.util.Arrays;
import java.util.Random;
import java.util.stream.IntStream;

public class  Filtering {

 int elem[]; 
 public Filtering(int elem[]) {
  this.elem = elem;
 }
 
  public int[] greater(int gvalue) 
   {     
     IntStream ins1=Arrays.stream(elem);
  IntStream ins2 = ins1.filter(val -> (val>gvalue) );
  return ins2.toArray();    
 }
   
  public  int[] lesser(int lvalue) 
   { 
    IntStream ins1=Arrays.stream(elem);
    IntStream ins2 = ins1.filter(val -> (val<lvalue) );
    return ins2.toArray();
   }
      
  public int[] grtequ(int gvalue) 
   {   
    IntStream ins1=Arrays.stream(elem);
    IntStream ins2 = ins1.filter(val -> (val>=gvalue) );
    return ins2.toArray();    
 }
   
  public  int[] lsrequ(int lvalue) 
   { 
    IntStream ins1=Arrays.stream(elem);
    IntStream ins2 = ins1.filter(val -> (val<=lvalue) );
    return ins2.toArray();
   }


   public  int[] range(int lvalue,int gvalue) 
   {  
    IntStream ins1=Arrays.stream(elem);
    IntStream ins2 = ins1.filter(val->(val>=lvalue && val<gvalue));
    return ins2.toArray();
   }
   
   
 public static void main(String[] args) 
 {
    int dsize=20;
    int ds[] =new int[dsize];
    
    Random rd =new Random();
    rd.setSeed(0);
    
    for(int n=0; n<dsize ; n++)
          ds[n] = rd .nextInt(1000);
        
          Filtering data = new Filtering(ds);
          int gval[] = data.greater(300);
          int lval[] = data.lesser(300);
          
          int rval[] = data.range(300,600);
                    
       
  System.out.println("\nSearch on Integer Array Dataset ");          
  System.out.println("Random Data :" + Arrays.toString(ds));
        
  System.out.println("\nSearch integer (values>300) 
                    list on Dataset  ");
  System.out.println(Arrays.toString(gval));
     
  System.out.println("\nSearch integer (values<300) 
                   list on Array Dataset ");
  System.out.println("Data :" 
                    + Arrays.toString(lval));
     
  System.out.println("\nSearch integer (300>values<600) 
               list on Array Dataset ");
  System.out.println("Data :" 
              + Arrays.toString(rval));
       
     
       
 }

}


Output
Search on Integer Array Dataset 

Random Data :[360, 948, 29, 447, 515, 53, 491, 761, 719, 854, 
             77, 677, 473, 262, 95, 844, 84, 875, 241, 320]

Search integer (values>300) list on Dataset  
[360, 948, 447, 515, 491, 761, 719, 854, 677, 473, 844, 875, 320]

Search integer (values<300) list on Array Dataset 

  Data =[29, 53, 77, 262, 95, 84, 241]

Search integer (300>values<600) list on Array Dataset 

    Data =[360, 447, 515, 491, 473, 320]

Comments

Popular posts from this blog

Solving System of Linear Equations by Gauss Jordan Elimination

Matrix Forward and Back Substitution

Solve System of Linear Equations by LU Decompose

Chebyshev distance between two points

Binary 1's and 2's Complement