Giter VIP home page Giter VIP logo

learn-java's Introduction

Java

For Core Java, Check this Document

Functional Programming in Java 8

Lamda, Lists and Looping

List<Integer> numbers = Arrays.asList(1, 2, 3, 4);

Looping with for each in the outdated way

// traditional
for(int i = 0; i < numbers.size(); i++){
  System.out.println(numbers.get(i));
}
// normal foreach
for(int i : numbers){
  System.out.println(i);
}
// Internal Iterator
numbers.forEach(new Consumer<Integer>(){
  public void accept(Integer e){
    System.out.println(e);
  }
}
// In java 8
numbers.forEach(System.out::println)

Lambda expression and Stream

Functional Programming

A normal function has to have :

  • Name
  • Return Type
  • Parameters
  • Body

Lambda are anonymous function whose type is inferred which has two things

  • Parameters
  • Body

In Normal Function, we can pass objects to functions, create objects within functions and return ojects from a function. In higher order functions, we can pass functions to functions, create functions within functions and return functions from a functions.

Types of Programming

  • Imperative: what and how.
  • Declarative: what not how
  • Functional: Declarative + higher-order functions
Lambda
Thread t1 = new Thread(()->System.out.println("Hello from Lambda"));
t1.start();
numbers.forEach((Integer e)-> System.out.println(e));
// leave the type
numbers.forEach((e)->System.out.println(e));
// No parentheisis for one parameter
numbers.forEach(e->System.out.println(e));
// Method references
numbers.forEach(System.out::println)

Stream

Stream in java 8
// No final if it's immutable
int factor = 2;
Stream<Integer> strm = numbers.stream()
          .map(e -> e * factor); // e is the parameter to the labda, much like args is the parameter to main
strm.forEach(System.out::println);
// Error will throws
int factor = 2;
Stream<Integer> strm = numbers.stream()
          .map(e -> e * factor);
factor = 4;
strm.forEach(System.out::println);
Lasy Evaluation
// Tricks by using Lasy Evaluation
int[] factor = new int[]{2};
Stream<Integer> strm = numbers.stream()
          .map(e -> e * factor[0]);
factor[0] = 0;
strm.forEach(System.out::println);
// answer will be all 0, because .map() will never be executed unless strm.forEach(System.out::println) is executed


// Stream is the internal iterator
System.out.println(
  numbers.stream()
          .filter(e->e%2 == 0)
          .mapToInt(e -> e *2)
          .sum()); // the filter and mapToInt won't be invoked unless .sum() is executed
);
//lamdas are stateless
numbers.stream()
        .map(e-> e*2)
        .forEach(System.out::println);
Credits to DEVOX

learn-java's People

Contributors

chhatrachhorm avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.