Computer Science 312 What’s New in Java 8? 1.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
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.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 6 Object Oriented Programming in Java Language Basics Objects.
Written by: Dr. JJ Shepherd
CSE341: Programming Languages Lecture 11 Closures-ish Java & C Dan Grossman Fall 2011.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Lecture 2: Design and Implementation of Lambda Expressions in Java 8 Alfred V. Aho CS E6998-1: Advanced Topics in Programming Languages.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Computer Science 209 The Strategy Pattern II: Emulating Higher-Order Functions.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Computer Science 209 The Strategy Pattern I: Comparisons and Layouts.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Chapter 3.5 Memory and I/O Systems. 2 Memory Management Memory problems are one of the leading causes of bugs in programs (60-80%) MUCH worse in languages.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Mohsen Zainalpour What`s New in Java 8 JUG.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CE Operating Systems Lecture 13 Linux/Unix interprocess communication.
FEN 2014UCN Teknologi/act2learn1 Higher order functions Observer Pattern Delegates Events Visitor Pattern Lambdas and closures Lambdas in libraries.
Java8 Released: March 18, Lambda Expressions.
Written by: Dr. JJ Shepherd
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Lambdas and Streams. Stream manipulation Class Employee represents an employee with a first name, last name, salary and department and provides methods.
Functional Processing of Collections (Advanced) 6.0.
CS314 – Section 5 Recitation 9
Modern Programming Tools And Techniques-I
CS 326 Programming Languages, Concepts and Implementation
SOFTWARE DESIGN AND ARCHITECTURE
Taking Java from purely OOP by adding “functional level Programming”
Methods Chapter 6.
CS 326 Programming Languages, Concepts and Implementation
The Strategy Pattern II: Emulating Higher-Order Functions
Functional Processing of Collections (Advanced)
Java Algorithms.
Functional Programming with Java
Java 8 Java 8 is the biggest change to Java since the inception of the language Lambdas are the most important new addition Java is playing catch-up: most.
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Streams.
Chapter 2: System Structures
COS 260 DAY 10 Tony Gauvin.
Object Oriented Programming
FP Foundations, Scheme In Text: Chapter 14.
Object Oriented Programming in java
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects – Exercises UTPA – Fall 2012 This set of slides is revised.
Java Programming Course
Functional interface.
Effective Java, 3rd Edition Chapter 7: Lambdas and Streams
A few of the more significant changes and additions. James Brucker
Computer Science 312 Composing Functions Streams in Java 1.
MAPREDUCE TYPES, FORMATS AND FEATURES
Chapter 4: Threads & Concurrency
Computer Science 312 Lambdas in Java 1.
Collectors and Terminal Operations
Abstract Classes and Interfaces
Records and Type Classes
Chap 2. Identifiers, Keywords, and Types
Hans Zaunere, Managing Member
Review for Midterm 3.
„Lambda expressions, Optional”
Generics, Lambdas and Reflection
Records and Type Classes
Presentation transcript:

Computer Science 312 What’s New in Java 8? 1

What Is Pipelining? Unix (or Linux) allows you to “pipe” data through a series of transformations using the | operator Transformations: Glue two files together Convert the result to lowercase Sort the resulting lines Return the last three of those lines cat file1 file2 | tr "[A-Z]" "[a-z]" | sort | tail -3

21st Century Computing Enormous amounts of data to be processed (“Big Data”) Many CPUs are available, either via multicore or on a network Would like a way of harnessing multiple processors to massage these data in a pipelined fashion, while using a simple, declarative style in our code

21st Century Computing In functional languages, data can be pipelined through a series of transformations, using a high- level, declarative style Essential elements: Map Filter Reduce (fold) Lambda expressions

21st Century Computing Because functional languages forbid side effects, the work of these transformations can be dispatched to multiple processors Essential elements: Map Filter Reduce (fold) Lambda expressions

What about the Rest of Us? In most conventional languages, massaging data involves iterating through various types of collections to modify their elements; the style of this is anything but declarative Although object orientation mitigates the potential for harmful side effects, programs in most conventional languages suffer the pitfalls of managing shared memory when resorting to concurrency

Java 8 to the Rescue! Java 8, released in 2014, includes new features that allow programs to process data in a pipelined manner, across multiple CPUs, with a declarative style of code The new features are Streams (including parallel streams) Methods (including lambdas) as arguments Higher-order functions for mapping and filtering Higher-order functions for collecting results

Sorting the Old-Fashioned Way // java.util.Comparator public interface Comparator<T> { public int compare(T o1, T o2); } // java.util.Collections.sort static <T> void sort(List<T> list, Comparator<? super T> c) Collections.sort supports the strategy pattern Define a class that implements Comparator and pass an instance of this class to sort, for a particular type of comparison

Sorting the Old-Fashioned Way // java.util.Comparator public interface Comparator<T> { public int compare(T o1, T o2); } // java.util.Collections.sort static <T> void sort(List<T> list, Comparator<? super T> c) Collections.sort(inventory, new Comparator<Apple>() { public int compare(Apple a1, Apple a2){ return a1.getWeight().compareTo(a2.getWeight()); } }); An instance of the strategy pattern Clunky: must create an instance of an anonymous class

Generalized Sorting with a lambda // java.util.Comparator public interface Comparator<T> { public int compare(T o1, T o2); } // java.util.Collections.sort static <T> void sort(List<T> list, Comparator<? super T> c) inventory.sort( (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight())); Still a strategy, but we now pass an explicit behavior, without the extra fluff of a class definition Much more declarative in style!

Filtering the Old-Fashioned Way

Filtering the Java 8 Way

Pass Method References filterApples(inventory, Apple::isGreenApple); filterApples(inventory, Apple::isHeavyApple);

Or Pass Lambda Expressions filterApples(inventory, (Apple a) -> "green".equals(a.getColor()) ); filterApples(inventory, (Apple a) -> a.getWeight() > 150 );

Generalize to a filter Method static <T> Collection<T> filter(Collection<T> c, Predicate<T> p){ // Build and return a collection of filtered results } filter(inventory, (Apple a) -> "green".equals(a.getColor()) ); filter(inventory, (Apple a) -> a.getWeight() > 150 ); Could do this, but we won’t Instead, we’ll create a stream from the collection, use the filter method in the java.util.stream API on this stream, and collect the results

Stream, Filter, and Collect import static java.util.stream.Collectors.toList; List<Apple> greenApples = inventory.stream().filter((Apple a) -> "green".equals(a.getColor()) .collect(toList()); List<Apple> heavyApples = inventory.stream().filter((Apple a) -> a.getWeight() > 150) Instead, we’ll create a stream from the collection use the filter method in the java.util.stream API on this stream collect the results

Filter and Group Transactions Lots of control-flow (for and if statements)

Filter and Group Transactions Drop control, use declarative style!

To Stream or to Parallel Stream? Sequential stream: import static java.util.stream.Collectors.toList; List<Apple> heavyApples = inventory.stream().filter((Apple a) -> a.getWeight() > 150) .collect(toList());

To Stream or to Parallel Stream? Sequential stream: import static java.util.stream.Collectors.toList; List<Apple> heavyApples = inventory.stream().filter((Apple a) -> a.getWeight() > 150) .collect(toList()); Parallel stream: import static java.util.stream.Collectors.toList; List<Apple> heavyApples = inventory.parallelStream().filter((Apple a) -> a.getWeight() > 150) .collect(toList());

Other Cool Stuff in Java 8 Default methods – can include method implementations in an interface, so implementers of its classes won’t go crazy when new methods are added Optional types – no more null pointer exceptions, due to having to return either null or some full- fledged object; specifies the Optional<T> type with accessor methods

Lambda expressions in Java Chapter 3 For next time Lambda expressions in Java Chapter 3