Java Algorithms.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 The Collections API.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Comparable and Comparator Interfaces Ben Rodes. Disclaimer.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CSE 115 Week 11 March , Announcements March 26 – Exam 7 March 26 – Exam 7 March 28 – Resign Deadline March 28 – Resign Deadline Lab 7 turned.
Generic Programming Amit Shabtay. March 3rd, 2004 Object Oriented Design Course 2 The Problem Assume we have a nice Stack implementation. Our stack receives.
Data Structures Using C++1 Chapter 13 Standard Template Library (STL) II.
Chapter 17 Java SE 8 Lambdas and Streams
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Lambdas and Streams. Functional interfaces Functional interfaces are also known as single abstract method (SAM) interfaces. Package java.util.function.
Input & Output In Java. Input & Output It is very complicated for a computer to show how information is processed. Although a computer is very good at.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Java.util.Vector Brian Toone 10/3/07 Updated 10/10/07.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
Java8 Released: March 18, Lambda Expressions.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
JAVA 8 AND FUNCTIONAL PROGRAMMING. What is Functional Programming?  Focus on Mathematical function computations  Generally don’t think about “state”
Chapter 9 Introduction to Arrays Fundamentals of Java.
Comp1004: Environments The Java Library. Coming up Recap – Encapsulation – Constructors – Loops – Arrays – ArrayList – Iterators The Java Library – Implementation.
Functional Processing of Collections (Advanced) 6.0.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
Lambdas & Streams Laboratory
Functional Programming
CSE 413, Autumn 2002 Programming Languages
CSC 222: Object-Oriented Programming
CSC 222: Object-Oriented Programming
CSC 222: Computer Programming II
Chapter 7 User-Defined Methods.
Functional Programming and Stream API
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
Taking Java from purely OOP by adding “functional level Programming”
Arrays in PHP are quite versatile
Software Development Java Collections
Chapter 20 Generic Classes and Methods
Chapter 7 Part 1 Edited by JJ Shepherd
CSC 222: Object-Oriented Programming
The Strategy Pattern II: Emulating Higher-Order Functions
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Generics, Lambdas, Reflections
Functional Processing of Collections (Advanced)
Functional Programming with Java
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
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.
Building Java Programs
Generics and Type Parameters
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
COS 260 DAY 10 Tony Gauvin.
Unit 6 Working with files. Unit 6 Working with files.
Chapter 10 ArrayList reading: 10.1
Generic programming in Java
Object Oriented Programming in java
Welcome to CSE 143! Go to pollev.com/cse143.
Functional interface.
Building Java Programs
A few of the more significant changes and additions. James Brucker
Generics, Lambdas, Reflections
Computer Science 312 Lambdas in Java 1.
Java Priority Queue.
Java Lists.
Computer Science 312 What’s New in Java 8? 1.
Lambda Expressions.
Suggested self-checks: Section 7.11 #1-11
Chapter 11 Inheritance and Polymorphism Part 2
Creating and Modifying Text part 3
„Lambda expressions, Optional”
Introduction to Java Collection
Presentation transcript:

Java Algorithms

Summary of Utilities java.util.Arrays – static utilities for raw arrays Searching and sorting Equality comparisons and hash codes Fill Copy java.util.Collections – similar items for Lists. Also includes: Min, max, counts Reverse, shuffle java.util.streams.* - automatic data processing library (with parallelism included) There are many more algorithms and utilities in the java library! To find how to use them, go to the Java API!

Example of using Sort Scanner s = new Scanner(new File(“numbers.txt”)); ArrayList<Integer> numbers = new ArrayList<>(); while(s.hasNextInt()) numbers.add(s.nextInt()); …elsewhere… Collections.sort(numbers);

Advanced Algorithms with lambda expressions (Java 8) In java.util.Collection provides a function stream(). A stream() allows you to perform functions over the data in the collection. Examples: filter – create a stream based on a predicate forEach – apply an action to each element map – create a new stream after applying an action to each element Many, many more You can always use the classic method of having a specialized file implement the required interface. OR you can use anonymous classes – nameless classes OR you can use a lambda expression A lambda is an anonymous single method class, but defined with extremely terse syntax Can also loosly define them as nameless methods

Advanced Algorithms with lambda expressions (Java 8) Take the following example function public static void printIntegersInRange( List<Integer> nums, Integer low, Integer high) { for(Integer i : nums) if(i >= low && i <= high) System.out.println(i); } We should be able to generalize this. We already know how, use interfaces public interface CheckInteger { boolean test(Integer n); } Then our function becomes public static void printIntegersIf( List<Integer> nums, CheckInteger tester) { for(Integer i : nums) if(tester.test(i)) System.out.println(i);

Advanced Algorithms with lambda expressions (Java 8) Now with a class public class CheckRange0To100 implements CheckInteger { public static Boolean test(Integer n) { return n >= 0 && n <=100; } printIntegersIf(nums, new CheckRange0To100()); However, this seems really extensive for a one off class, right? Of course, so Java also has the ability to write things with anonymous classes…

Advanced Algorithms with lambda expressions (Java 8) Now with an anonymous class printIntegersIf(nums, new CheckInteger() { public boolean test(Integer i) { return i >= 0 && i <= 100; } ); However, this still seems really extensive for a one off class, right? Of course, so Java 8 introduced the widely known concept of lambda functions

Advanced Algorithms with lambda expressions (Java 8) Now with a lambda expression printIntegersIf(nums, (Integer i) -> i >= 0 && i <= 100 ); Short and sweet! This allows us to write generic algorithms with functions as parameters easily!

Advanced Algorithms with lambda expressions (Java 8) Now with the standard Java provided functionals found in the package java.util.function public static void printIntegersInRange( List<Integer> nums, Predicate<Integer> tester) { for(Integer i : nums) if(tester.test(i)) System.out.println(i); } And our lambda can become even shorter! printIntegersIf(nums, i -> i >= 0 && i <= 100 ); Sort example Collections.sort(nums, (i1, i2) -> -i1.compareTo(i2)); Full tutorial

Problem Lets explore the power of streams and lambdas by bulk processing and "corrupting" some data! (obviously can be used to "clean" data as well) Download the source code from online. Change the TODO statements to perform the required actions to bulk process data. You can only use lambda expressions in this file (except when you read from the file) Work in pairs. After completing, show me the source file and then work on the next programming assignment.