mathajax

Java 8 Search on Integer Array Dataset using Intstream object

The java program search on dataset using java 8 Instream Arrays based on conditional values
The class Data3 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)  
   
and Object of class Data3 is initialized with 20 randomly integers generated from random object with limit range of value [0-1000]. In above all member function, we have used java 8 version of array search by Intstream class in java.util.stream package.

greater function The function greater is invoked by object of Data3 with one integer argument and returns list (array) of integer which are greater than given argument value.
lesser function The function greater is invoked by object of Data3 with one integer argument and returns list (array) of integer which are lesser than given argument value.
range function The function greater invoked is by object of Data3 with two integer arguments. The second argument is greater than first one. The function returns list of integers which are greater than the first argument value and lesser than the second argument value.
lsrequ function The function lesser and equal is invoked by object of Data3 with one integer argument and returns list (array) of integer which are lesser than or equal to given argument value.
grtequ function The function lesser and equal is invoked by object of Data3 with one integer argument and returns list (array) of integer which are greater than or equal to given argument value.




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


public class  Data3 {

 
 int elem[]; 
 public Data3(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);
        
          Data3 data = new Data3(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("n values <300   in Data :" 
                    + Arrays.toString(lval));
     
  System.out.println("\nSearch integer (300>values<600) 
               list on Array Dataset ");
  System.out.println("n values <300   in 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]

Comments

Popular posts from this blog

Matrix Forward and Back Substitution

Chebyshev distance between two points

Solve System of Linear Equations by LU Decompose

Complex number Multiplication and Division

Matrix Determinant, Matrix Adjoint and Matrix Inverse