Lambdas

 Functional and Object Oriented Language

Programming Paradigms

  • Imperative 
    • programming using variables, list, loops 
    • eg: C, 
  • Functional
    • Functions calling functions and so.. 
    • Eg: Excel,
  • Object Oriented
    •  Programming with basic building block as Class
    • eg: Java, C++

Java recently added support for Functional Programming Features for the following reasons

  • Creating class is overkill for quick-and-dirty code for one-off use.
  • Lambda Functions/Lambda Expressions
    • Lambdas are anonymous functions 
    • An alternate to anonymous classes with a single method
    • Useful for quick and dirty bits of coding that don't need a class
    • Anonymous function Objects are called Closures 
  • Lambda Functions are powerful when the output of one is fed as input of another. Similar to assembly line. Java has added Aggregate functions to achieve the same.
  • Java provides simple way to use these standard functional programming operations to Collections like Lists, Sets, Maps etc
  • Aggregate operators
    • Filter
      • Condition that is applied to every input stream element and returns to output stream only if condition is satisfied. 
    • Map
      • a function to be applied to every object in stream and produce Output stream
    • Foreach
      • Applies to every element in input stream. It doesn't produce any output stream. It is usually terminating condition in the flow.
 Stream()
  • Method on any collection to get an Object of type Stream, on which Aggregate functions can be applied in sequenc
  • Stream can be imagined as a Stream of Values where each value is subjected to operation like Map, Foreach, Filter

Example

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Example2 {

    public static void main(String[] args) {
        List<String> myList = new ArrayList<>();
        myList.add("Vijayawada");
        myList.add("Hyderabad");
        myList.add("Guntur");
        myList.add("Vijag");
        lambda(myList);
        
        Map<String, String> states = new HashMap<>();
        states.put("Vijayawada","AP");
        states.put("Hyderabad","TS");
        states.put("Guntur","AP");
        states.put("Vijag","AP");
        lambda2(myList,states);

        
    }
    public static void lambda(List<String> myList) {
        myList.stream().filter(name -> name.startsWith("V")).map(name -> name+" is a City in Andhra").forEach(x -> System.out.println(x));
        System.out.println("done");
        
        //myList.stream().filter(name -> name.startsWith("V")).map(name -> name+" is a City in Andhra").forEach(x -> System.out.println(x));

        
    }
    public static void lambda2(List<String> myList, Map<String, String> myMap) {
        Collections.sort(myList,(str1,str2)->{
            if (str1.equals("Hyderabad") && !str2.equals("Hyderabad")) return -1;
            if (str2.equals("Hyderabad") && !str1.equals("Hyderabad")) return 1;
            return str1.compareTo(str2);
            });
        
        myList.stream().filter(name->myMap.get(name).equals("TS")).map(name->name+" is in state " + myMap.get(name)).forEach(name->System.out.println(name));
        
    }
}
 

Comments

Popular posts from this blog

OCI