Java 8 Features



This content originally appeared on DEV Community and was authored by Neelakandan R

Lambda Expression:()->
It is a way to write method in short way and create anonymous function.

(a, b) -> a + b;

Functional Interfaces

It should have only one abstract method and it can have any number static and default methods.

package java8_features;
@FunctionalInterface
public interface contract {

    public int add(int a, int b);

    public static void sum()
    {}

    public static void sum22()
    {}

}
package java8_features;

public class city {
    public static void main(String[] args) {
        contract sum=(a,b)->a+b;
        System.out.println(sum.add(10, 20));

    }

}

Predicate

Predicate is a functional interface that takes an input of type T and returns a boolean value.It has methed test();

package java8_features;

import java.util.function.Predicate;

public class bredicate {
    public static void main(String[] args) {
        Predicate<Integer> p=(no)->no>50;
        System.out.println(p.test(8));
    }

}

Function:
The Function interface is a functional interface provided by Java 8 that takes one input and returns one output.It has methed apply();

Function
T – Input type
R – Return type

package java8_features;

import java.util.function.Function;

public class FunctionEXample {
    public static void main(String[] args) {
//      Function<String,Integer> p=(name)->name.length();
//      System.out.println(p.apply("Neelakandan"));

        Function<String, String> p = (name) -> name.toLowerCase();
        System.out.println(p.apply("NEELAKANDAN"));

    }
}

What is Consumer?

Consumer is a functional interface introduced in Java 8. It accepts a single input (of type T) and returns nothing (void).

package java8_features;

import java.util.List;
import java.util.function.Consumer;

public class consumenEx {
    public static void main(String[] args) {
//  Consumer<String> p=(name)->System.out.println(name.toLowerCase());
//  p.accept("NEELAKANDAN");

        Consumer<String> p = (name) -> System.out.println(name + " thanks");
        Consumer<String> p2 = (name) -> System.out.println(name + " hi");
        Consumer<String> p3 = p.andThen(p2);
        p3.accept("neel");// output :neel thanks neel hi

//      List al=List.of(10,20,30,40,50);
//      Consumer<Integer> p=(n)->System.out.println("no = "+n);
//      
//      al.forEach(p);

//  output          no = 10
//              no = 20
//              no = 30
//              no = 40
//              no = 50

    }

}


Supplier

Supplier is a functional interface that doesn’t take any input, but returns a value of type T

package java8_features;

import java.util.Random;
import java.util.function.Supplier;

public class suplier {
    public static void main(String[] args) {
        Random r = new Random();

        Supplier<Integer> s = () -> r.nextInt(100);
        System.out.println(s.get());
    }

}

Stream:is a new feauture in java8 that let you work with collection in a clear and shorted way.

Stream api:processing collection of object.

1.Arrays.stream();

package java8_features;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.stream.IntStream;

public class Streamof {
    public static void main(String[] args) {
        int[] ar= {10,20,50,1,30,40,50};
//      IntStream s=Arrays.stream(ar);
//      s=s.sorted();
//      s.forEach(System.out::println);

//      Arrays
//      .stream(ar)
//      .sorted()
//      .forEach(System.out::println);  

//      OptionalInt s=Arrays
//              .stream(ar)
//              .max();
//      System.out.println(s.getAsInt());//50


//      OptionalInt s=Arrays
//              .stream(ar)
//              .min();
//      System.out.println(s.getAsInt());//1



        OptionalDouble s=Arrays
                .stream(ar)
                .average();
        System.out.println(s.getAsDouble());//28.714285714285715


    }

}


package java8_features;

import java.util.Arrays;

public class arraystream {
    public static void main(String[] args) {
//      int[]ar={1,2,2,3,4,4,5,67,40};
//      Arrays
//      .stream(ar)
//      .distinct()//remove duplicate
//      .forEach(System.out::println);


//      long od=Arrays
//      .stream(ar)
//      .count();
//      System.out.println(od);//count find

//      Arrays
//      .stream(ar)
//      .filter(no->no%4==0)//multiple of 4
//      .forEach(System.out::println);//
    }

}


package java8_features;

import java.util.ArrayList;
import java.util.List;

public class arraystream {
    public static void main(String[] args) {
    List<Integer> ls=new ArrayList<Integer>();
    ls.add(10);
    ls.add(100);
    ls.add(140);
    ls.add(120);
    ls.add(10);
    ls.add(5);
    ls.add(11);
//    System.out.println(
//          ls
//          .stream()
//          .count());

//          ls
//          .stream()
//          .distinct()//unique
//          .sorted()//sort
//          .forEach(System.out::println);

ls
    .stream()
    .distinct()//unique
    .map(no->no%100==0)//false true false false false false
    .forEach(System.out::println);


    }

}


package java8_features;

import java.util.ArrayList;
import java.util.List;

public class arraystream {
    public static void main(String[] args) {
    List<String> ls=new ArrayList<String>();
    ls.add("ayan");
    ls.add("aya");
    ls.add("ayanan");
    ls.add("mayan");
    ls.add("nayan");


//    ls
//    .stream()
//    .distinct()//unique
//    .map(no->no.toUpperCase())
//  .forEach(System.out::println);

//    ls
//    .stream()
//    .distinct()//unique
//    .filter(no->no.startsWith("a"))//ayan//aya//ayanan
//  .forEach(System.out::println);


    }

}



This content originally appeared on DEV Community and was authored by Neelakandan R