Collections Algorithms

Similar documents
Comparing Objects 3/28/16

Comparing Objects 3/14/16

Generics method and class definitions which involve type parameters.

CS 61B Discussion 5: Inheritance II Fall 2018

Object Oriented Programming: Based on slides from Skrien Chapter 2

Today. Reading. Homework. Lecture Notes CPSC 224 (Spring 2012) hashcode() method. Collections class. Ch 9: hw 7 out (due in a week)

Software and Programming I. Classes and Arrays. Roman Kontchakov / Carsten Fuhs. Birkbeck, University of London

CIS 110: Introduction to computer programming

Fundamental Java Methods

CS 1331 Fall 2016 Exam 3 Part 1 ANSWER KEY

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Introduction to Object-Oriented Programming

Exercise 8 Parametric polymorphism November 18, 2016

Outline. applications of hashing equality and comparisons in Java sorting selection sort bubble sort insertion sort

MIT AITI Lecture 18 Collections - Part 1

Distributed Systems Recitation 1. Tamim Jabban

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

CMSC 341. Nilanjan Banerjee

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

Collections class Comparable and Comparator. Slides by Mark Hancock (adapted from notes by Craig Schock)

Lecture 15 Summary. Collections Framework. Collections class Comparable and Comparator. Iterable, Collections List, Set Map

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

CMSC 132: Object-Oriented Programming II. Interface

Practice for Chapter 11

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Inheritance, Polymorphism, and Interfaces

Collections, Maps and Generics

Last class. -More on polymorphism -super -Introduction to interfaces

COE318 Lecture Notes Week 9 (Oct 31, 2011)

Distributed Systems Recitation 1. Tamim Jabban

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2013

UMBC CMSC 331 Final Exam

EECS2030 Week 7 worksheet Tue Feb 28, 2017

Super-Classes and sub-classes

CS 211: Binary Search and Sorting

CS-202 Introduction to Object Oriented Programming

CS 2102 Exam 2 D-Term 2013

Inheritance. Transitivity

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #9. Review is-a versus has-a. Lecture #9 Inheritance I

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

On the Algorithm for Specializing Java Programs with Generic Types

C12a: The Object Superclass and Selected Methods

QUEEN MARY, UNIVERSITY OF LONDON DCS128 ALGORITHMS AND DATA STRUCTURES Class Test Monday 27 th March

Type Hierarchy. Lecture 6: OOP, autumn 2003

Java Brand Generics. Advanced Topics in Java. Khalid Azim Mughal Version date:

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Generics Collection Framework

Rules and syntax for inheritance. The boring stuff

CSE 331. Generics (Parametric Polymorphism)

Introduction to Inheritance

CS 251 Intermediate Programming Inheritance

Generics and Type Parameters

Parametric polymorphism and Generics

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Lists using LinkedList

PowerPoint Slides. Object-Oriented Design Using JAVA. Chapter 2. by Dale Skrien

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Polymorphism. return a.doublevalue() + b.doublevalue();

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

COURSE 5 PROGRAMMING III OOP. JAVA LANGUAGE

Exercise 8 Parametric polymorphism November 17, 2017

INF3110 Programming Languages Runtime Organization part II

Generics. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 10

Lecture 15 Summary 3/11/2009. By the end of this lecture, you will be able to use different types of Collections and Maps in your Java code.

Introduction to Object-Oriented Programming

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

Generic types. Announcements. Raw ArrayLists. Generic types (cont.) Creating a raw ArrayList: Accessing a raw ArrayList:

Closed book but one sheet, both sides, of A4 paper is allowed. Section 2.5 of the text Generics in the Java Programming Languages by Gilad Bracha

Notable Enhancements in Java 8. Functional Programming with Java. Lambda Expressions in Java. How to Define a Lambda Expression? Lambda expressions

Write the code to create an enhanced for loop that will go through every member of an ArrayList <String> myarray and print it out to the console.

CompuScholar, Inc. 9th - 12th grades

Language Features. 1. The primitive types int, double, and boolean are part of the AP

An example from Zoology

+ Abstract Data Types

Advanced Topics in Java and on Security in Systems Effective Generics. Eirik Eltvik 26 th of February 2007

Inheritance (Part 2) Notes Chapter 6

CO Java SE 8: Fundamentals

Primitive Java Generic Class

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

Java Collection Framework

Java Fundamentals (II)

Model Solutions. COMP 103: Test May, 2013

TeenCoder : Java Programming (ISBN )

Object-oriented programming

COMP1008 An overview of Polymorphism, Types, Interfaces and Generics

CSE 331 Software Design & Implementation

Some examples and/or figures were borrowed (with permission) from slides prepared by Prof. H. Roumani. The Collection Framework

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

Introducing Generics

CS 200 More Classes Jim Williams, PhD

Java Generics -- an introduction. Based on

[TAP:xxxxx] Search. Which of the following is the efficient way to search for an element in an unsorted list?

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

Project ABP-light. MIDO Mathématiques et Informatique de la décision et des organisations

ITI Introduction to Computing II

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Computer Science II (Spring )

Transcription:

Collections Algorithms 1 / 11

The Collections Framework A collection is an object that represents a group of objects. The collections framework allows different kinds of collections to be dealt with in an implementation-independent manner. 2 / 11

Collections.sort(List<T> list) The collections framework includes algorithms that operate on collections. These algorithms are implemented as static methods of the Collections class. A good example is the (overloaded) sort method: public static <T extends Comparable<? super T>> void sort(list<t> list) This method signature demonstrates how to declare a generic method (so far we ve seen only generic classs): put a type parameter before the return type. This sort uses the "natural ordering" of the list, that is, the ordering defined by Comparable. <? super T> is a type bound. It means "some superclass of T." For now just think of it this way: the type parameter <T extends Comparable<? super T>> means that the element type T or some superclass of T must implement Comparable. 3 / 11

The java.lang.comparable Interface public interface Comparable<T> { public int compareto(t o); compareto(t o) Compares this object with the specified object for order. Returns a negative integer if this object is less than the other object, zero if this object is equal to the other object, or a positive integer if this object is greater than the other object. 4 / 11

Implementing java.lang.comparable<t> Here s a Person class whose natural ordering is based on age: public class Person implements Comparable<Person> { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; public String tostring() { return name; public int compareto(person other) { return this.age - other.age; 5 / 11

Analyzing <T extends Comparable<? super T» Given the Collections static method: public static <T extends Comparable<? super T>> void sort(list<t> list) And the classes: public class Person implements Comparable<Person> public class GtStudent extends Person {... Can we sort a List<GtStudent>? Type checker "proves" that a type argument satisfies a type bound. Prove by substituting without causing contradictions: [GtStudent/T, Person/?]<T extends Comparable<? super T>> <GtPerson extends Comparable<Person super GtStudent> Yes, we can sort a List<GtStudent> because GtStudent extends Person, Person implements Comparable<Person>, so GtStudent is a subtype of Comparable<Person> and Person is a supertype of GtStudent 6 / 11

Using Collections.sort(List<T> list) Given the Collections static method: public static <T extends Comparable<? super T>> void sort(list<t> list) We could sort a List<Person> because Person implements Comparable<Person>: List<Person> peeps = new ArrayList<>(); peeps.add(new Person(...));... Collections.sort(peeps); And if we have a class: public class GtStudent extends Person {... We could also sort a List<GtStudent> because GtStudent extends Person, Person implements Comparable<Person> and Person is a supertype of GtStudent 7 / 11

Using Collections.sort(List<T>) on Raw Lists Java uses type erasure to implement generics, meaning that the compiled code is nearly identical to non-generic code. Type erasure allows for compile-time type checking while preserving the ability to work with legacy code. So you can sort a raw List of Person using the compareto(person) method: List rawpeeps = new ArrayList(); rawpeeps.add(new Person(...));... Collections.sort(rawPeeps); 8 / 11

Using Collections.sort(List<T>) on Raw Lists Overriding only happens when methods have identical signatures. To allow generic classes to work in non-generic settings, the compiler inserts bridge methods. So Person looks like: public class Person implements Comparable<Person> { //... // This is a bridge method inserted by the compiler to allow this // class to work with legacy non-generic code public int compareto(object other) { return compareto((person) other); public int compareto(person other) { return this.age - other.age; 9 / 11

Using java.util.comparator<t> public interface Comparator<T> { int compare(t o1, T o2); boolean equals(object obj); Comparator<T> is an interface with two methods: int compare(t o1, T o2) - same contract as o1.compareto(o2) boolean equals(object obj) It s always safe to use the inherited equals method, so the one you need to implement is compare. See SortTroopers.java and Trooper.java for examples using Comparable, Comparator and Collections.sort(...). 10 / 11

Programming Exercise Write a class to represent Georgia Tech students called, say, GtStudent. Give GtStudent name, major, GPA, and year fields/properties. Have GtStudent implement Comparable<T> with some ordering that makes sense to you perhaps some majors are harder than others, so GPAs are adjusted in comparisons. Add instances of GtStudents to an ArrayList<E>. Sort the ArrayList of GtStudent~s using ~Collections.sort(List<E>). Write a Comparator<GtStudent> and sort your list with Collections.sort(List<E>, Comparator<E>). Extra: add thousands of randomly-gnerated GtStudent~s to an ~ArrayList and a LinkedList and time Collections.sort(List<E>) method invocations for each of them. Is one faster? Why (or why not)? 11 / 11