Lambdas and Streams. Stream manipulation Class Employee represents an employee with a first name, last name, salary and department and provides methods.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Generics, Lists, Interfaces
Java 8 Stream API Raj Thavamani Application Developer / Java Group Biomedical Informatics.
Singleton vs utility class  at first glance, the singleton pattern does not seem to offer any advantages to using a utility class  i.e., a utility class.
C# and LINQ Yuan Yu Microsoft Research Silicon Valley.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
1 Fall 2009ACS-1903 The break And continue Statements a break statement can be used to abnormally terminate a loop. use of the break statement in loops.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Collections. 2 Objectives Explore collections in System.Collections namespace –memory management –containment testing –sorting –traversal.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
Math class methods & User defined methods Math class methods Math.sqrt(4.0) Math.random() java.lang is the library/package that provides Math class methods.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 24 : Collections King Fahd University of Petroleum & Minerals College of Computer.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
 2006 Pearson Education, Inc. All rights reserved Generics.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
Chapter 17 Java SE 8 Lambdas and Streams
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Data Structures Using C++ 2E
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Lambdas and Streams. Functional interfaces Functional interfaces are also known as single abstract method (SAM) interfaces. Package java.util.function.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Chapter 8: Arrays.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Introduction to LINQ Chapter 11. Introduction Large amounts of data are often stored in a database—an organized collection of data. A database management.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
FEN 2014UCN Teknologi/act2learn1 Higher order functions Observer Pattern Delegates Events Visitor Pattern Lambdas and closures Lambdas in libraries.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke.
By Mr. Muhammad Pervez Akhtar
By Mr. Muhammad Pervez Akhtar
Java8 Released: March 18, Lambda Expressions.
Review for Nested loops & Math class methods & User defined methods.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
05 Method Calling. 2 What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value Outline.
JAVA 8 AND FUNCTIONAL PROGRAMMING. What is Functional Programming?  Focus on Mathematical function computations  Generally don’t think about “state”
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
Software Development Java Classes and Methods
Java Primer 1: Types, Classes and Operators
Methods Chapter 6.
Chapter 20 Generic Classes and Methods
Function Overloading.
Chapter 3: Using Methods, Classes, and Objects
Java Review: Reference Types
Java Algorithms.
Functional Programming with Java
Streams.
14 The Stream API.
Introduction to LINQ Chapter 11 10/28/2015 Lect 4 CT1411.
Introduction to LINQ Chapter 11.
Functional interface.
A few of the more significant changes and additions. James Brucker
Lambda Expressions.
Predefined Functions Revisited
Unit-1 Introduction to Java
Review for Midterm 3.
Presentation transcript:

Lambdas and Streams

Stream manipulation Class Employee represents an employee with a first name, last name, salary and department and provides methods for manipulating these values. Refer to StreamEmployeeDemo When the instance method reference System.out::println is passed to Stream method forEach, it’s converted into an object that implements the Consumer functional interface. This interface’s accept method receives one argument and returns void. In this case, the accept method passes the argument to the  System.out object’s println instance method.

Stream manipulation (Cont’d) The Comparator interface’s static method comparing receives a Function that’s used to extract a value from an object in the stream for use in comparisons and returns a Comparator object. A nice performance feature of lazy evaluation is the ability to perform short circuit evaluation—that is, to stop processing the stream pipeline as soon as the desired result is available. Stream method findFirst is a short-circuiting terminal operation that processes the stream pipeline terminates processing as soon as the first object is found. and returns an Optional containing the object that was found, if any.

Stream manipulation (Cont’d) To sort objects by two fields, you create a Comparator that uses two Function s. First you call Comparator method comparing to create a Comparator with the first Function. On the resulting Comparator, you call method thenComparing with the second Function. The resulting Comparator compares objects using the first Function then, for objects that are equal, compares them by the second Function.

Stream manipulation (Cont’d) You can map objects in a stream to different types to produce another stream with the same number of elements as the original stream. Stream method distinct eliminates duplicate objects in a stream. Collectors static method groupingBy with one argument receives a Function that classifies objects in the stream—the values returned by this function are used as the keys in a Map. The corresponding values, by default, are List s containing the stream elements  in a given category.

Stream manipulation (Cont’d) Map method forEach performs an operation on each key–value pair. Receives an object that implements functional interface BiConsumer. BiConsumer ’s accept method has two parameters. For Map s, the first represents the key and the second the corresponding value. Collectors static method groupingBy with two arguments receives a Function that classifies the objects in the stream and another Collector (known as the downstream Collector ). Collectors static method counting returns a Collector that counts the number of objects in a given classification, rather than collecting them into a List.

Stream manipulation (Cont’d) mapToDouble, maps objects to double values and returns a DoubleStream. Stream method mapToDouble maps objects to double values and returns a DoubleStream. receives an object that implements the functional interface ToDoubleFunction This interface’s applyAsDouble method invokes an instance method on an object and returns a double value.

Generating Streams of Random Values Class SecureRandom ’s methods ints, longs and doubles (inherited from class Random ) return IntStream, LongStream and DoubleStream, respectively,  for streams of random numbers. Method ints with no arguments creates an IntStream for an infinite stream of random int values. An infinite- stream is a stream with an unknown number of elements you use a short-circuiting terminal operation to  complete processing on an infinite stream.

Generating Streams of Random Values (Cont’d) Method ints with a long argument creates an IntStream with the specified number of random int values. Method ints with two int arguments creates an IntStream for an infinite stream of random int values in the range starting with the first argument and up to, but not including, the second. ints with a long & 2 int arguments creates an IntStream with the specified number of random int values in the range starting with the first argument and up to, but not including, the second.

Generating Streams of Random Values (Cont’d) To convert an IntStream to a Stream call IntStream method boxed. Function static method identity creates a Function that simply returns its argument.