MIT AITI Lecture 18 Collections - Part 1

Similar documents
Class Libraries. Readings and References. Java fundamentals. Java class libraries and data structures. Reading. Other References

The class Object. Lecture CS1122 Summer 2008

Comparing Objects 3/28/16

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

Lecture 9: Lists. MIT-AITI Kenya 2005

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

Collections, Maps and Generics

C12a: The Object Superclass and Selected Methods

Today. Book-keeping. Exceptions. Subscribe to sipb-iap-java-students. Collections. Play with problem set 1

Comparing Objects 3/14/16

Lecture 10. Overriding & Casting About

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Objects and Iterators

USAL1J: Java Collections. S. Rosmorduc

Super-Classes and sub-classes

The Collections API. Lecture Objectives. The Collections API. Mark Allen Weiss

Chapter 11: Collections and Maps

Csci 102: Sample Exam

Le L c e t c ur u e e 8 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Collections

Collections Algorithms

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

JAVA WRAPPER CLASSES

G51PRG: Introduction to Programming Second semester

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CSC 1351: Final. The code compiles, but when it runs it throws a ArrayIndexOutOfBoundsException

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

Data Structures. COMS W1007 Introduction to Computer Science. Christopher Conway 1 July 2003

Java Comparable interface

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

Error Handling. public int divide(double a, double b) { if (b==0) return -1; // error double result = a/b; return 0; // success }

Java Collection Framework

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

CIS 110: Introduction to computer programming

Domain-Driven Design Activity

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Lecture 16: Case Study: The Java Collections API

Last Name: Circle One: OCW Non-OCW

Another interface: Comparable

Review. CSE 143 Java. A Magical Strategy. Hash Function Example. Want to implement Sets of objects Want fast contains( ), add( )

Computational Expression

Lists using LinkedList

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CISC370: Inheritance

Compsci 201 Hashing. Jeff Forbes February 7, /7/18 CompSci 201, Spring 2018, Hashiing

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.

NAME: c. (true or false) The median is always stored at the root of a binary search tree.

Introduction to Computer Science I

Building Java Programs

Distributed Systems Recitation 1. Tamim Jabban

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

CSE 143 Lecture 20. Circle

Introduction to Programming Using Java (98-388)

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

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

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

CS11 Java. Winter Lecture 8

Family Name:... Other Names:... ID Number:... Signature... Model Solutions. COMP 103: Test 1. 9th August, 2013

Distributed Systems Recitation 1. Tamim Jabban

Lecture 3 Operators MIT AITI

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Test 1: CPS 100. Owen Astrachan. October 1, 2004

COM1020/COM6101: Further Java Programming

CO Java SE 8: Fundamentals

CSE 331 Software Design & Implementation

17. Java Collections. Organizing Data. Generic List in Java: java.util.list. Type Parameters ( Parameteric Polymorphism ) Data Structures that we know

Expected properties of equality

CONTAİNERS COLLECTİONS

Lecture 10: Interfaces

Overview. ITI Introduction to Computing II. Interface 1. Problem 1. Problem 1: Array sorting. Problem 1: Array sorting. Problem 1: Array sorting

Rules and syntax for inheritance. The boring stuff

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Announcements/Follow-ups

CPS122 Lecture: Detailed Design and Implementation

Model Solutions. COMP 103: Test April, 2013

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

TECHNICAL WHITEPAPER. Performance Evaluation Java Collections Framework. Performance Evaluation Java Collections. Technical Whitepaper.

CIS 265 Exam 2 First Name Last Name

CMSC 132: Object-Oriented Programming II. Interface

1 Shyam sir JAVA Notes

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

Grade Weights. Language Design and Overview of COOL. CS143 Lecture 2. Programming Language Economics 101. Lecture Outline

Java Classes. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

UMBC CMSC 331 Final Exam

CS2110: Software Development Methods. Maps and Sets in Java

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 6

CMSC 132: Object-Oriented Programming II

CSE 331 Software Design & Implementation

OOP Design Conclusions and Variations

Application Development in JAVA. Data Types, Variable, Comments & Operators. Part I: Core Java (J2SE) Getting Started

CMSC 341. Nilanjan Banerjee

Computer Science II (Spring )

CS Ananda Gunawardena

Topic 10. Abstract Classes. I prefer Agassiz in the abstract, rather than in the concrete.

Transcription:

MIT AITI 2004 - Lecture 18 Collections - Part 1

Collections API The package java.util is often called the "Collections API" Extremely useful classes that you must understand to be a competent Java programmer Divided into three main types of collections: lists, sets, and maps

Collection Definitions list an ordered collection of elements that can have duplicates set an unordered collection of elements with no duplicates map an association between keys and values. Maps each key to a single value.

Interface Hierarchy Collections API organizes its classes into a hierarchy of interfaces Collection Map List Set

Abstract Class Hierarchy Abstract collection classes provide skeletal implementations of the interfaces To implement your own collection, just subclass one of the abstract classes and implement the few abstract methods No need to implement the entire interface from scratch.

Concrete Collection Classes The non-abstract (concrete) classes in the collections API provide good implementations of the interfaces Use these unless you really need to implement your own. The most widely used are ArrayList, LinkedList, HashSet, and HashMap

Interfaces Classes Collection Abstract Collection List Abstract List Set Abstract Set HashSet ArrayList Map Abstract Map Abstract SequentialList HashMap LinkedList

Lists list an ordered collection of elements that can have duplicates The List interface specifies the behavior of a list collection. API provides two implementations of a list: ArrayList and LinkedList

Collections Quiz 1 1. What type of collection would I use to store the players on the Ghana football team? 2. The schedule of the teams opponents for the Ghana football team? 3. T/F. All classes in the Collections API implement Collection? 4. T/F. If a class does not implement Comparable the elements cannot be sorted with Collections.sort set list false, maps do not false, w/ Comparator

Collections Quiz 2 What's wrong with the equals method below? class Person { private int age; private String name; public boolean equals(object o) { if (!(o instanceof Person)) return false; Person p = (Person)o; return age == p.age && name == p.name } } name.equals(p.name)

The Equals Method: Pop Quiz What will the following code print out? Double a1 = new Double(95.0); Double a2 = new Double(95.0); ArrayList grades = new ArrayList(); grades.add(a1); System.out.println(grades.contains(a2)); a1!= a2 but true because a1.equals(a2)

Two Forms of Equality Reference Equality objects are the same exact object test with == operator Logical Equality objects are logically equivalent test with the equals method the object determines what it means for other objects to be logically equivalent to it

Equals Method Every class inherits an equals method from the class Object The Object equals method just tests for reference equality (==) For nearly all the classes you write, you should override the.equals method to provide a more useful test of logical equality

4 Steps to.equals Heaven 1. Use the instanceof keyword operator to check that the argument is the correct type. (This returns false if the argument is null.) 2. If it is not of the correct type, return false; otherwise cast the argument to that type 3. Check equality of each primitive data field of the argument using == 4. Check equality of each object data field of the argument using.equals

ColorPoint.equals public class ColorPoint { private int x, y; private Color c; } public boolean equals(object o) { if (!(o instanceof ColorPoint)) { return false; } ColorPoint cp = (ColorPoint)o; return x == cp.x && y == cp.y && c.equals(cp.c); }

Comparing Objects Sometimes we want to test not only equality, but whether one object is greater than or less than another object. If a class implements the Comparable interface, it can be compared in this way Or if there is a Comparator which compares instances of that class

Comparable The Comparable interface has only a single method compareto If the object is greater than the argument, then compareto returns a positive integer If the object is less then the argument, then compareto returns a negative integer If the argument is equal, compareto returns 0

Comparator The Comparator interface only has a single method compareto If the first argument is greater than the second, compareto returns a positive integer If the first argument is less than the second, compareto returns a negative integer If the arguments are equal, compareto returns 0

Comparable Person public class Person implements Comparable { private int age; private String name; } } // sorts people first by age, then by name public int compareto(object o) { Person p = (Person)o; if (age!= p.age) return age p.age; else return name.compareto(p.name);

Person Comparator public class PersonComparator implements Comparator { // sorts people first by age, then by name public int compareto(object o1, Object o2) { Person p1 = (Person)o1; Person p2 = (Person)o2; if (p1.age!= p2.age) return p1.age p2.age; else return p1.name.compareto(p2.name); } }

Collections Class If an class implements Comparable or has a Comparator, Lists of that class can be sorted with Collections.sort The class Collections provides a lot of useful static methods that operate on and return collections. Look there before writing your own.