Today's Agenda. > To give a practical introduction to data structures. > To look specifically at Lists, Sets, and Maps

Similar documents
CS 146 Spring 2017 Homework 4 Sections 5 and 6. The homework assignment is available at:

Java Collections Framework

Collections Questions

Topic 10: The Java Collections Framework (and Iterators)

CS61B Lecture #24. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

CMSC 202H. Containers and Iterators

//instance variables //methods. Foo x = new Foo(); Interface: also a type of objects public interface Bar {

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Collections, Maps and Generics

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

Abstract data types (again) Announcements. Example ADT an integer bag (next) The Java Collections Framework

Adam Blank Lecture 5 Winter 2019 CS 2. Introduction to Programming Methods

Software 1 with Java. Recitation No. 6 (Collections)

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

Lecture 9: Lists. MIT-AITI Kenya 2005

Java Collections Framework. 24 April 2013 OSU CSE 1

COMP-202 Unit 7: More Advanced OOP. CONTENTS: ArrayList HashSet (Optional) HashMap (Optional)

Garbage Collection (1)

CSE 143. Computer Programming II

CS11 Java. Winter Lecture 8

CS61B Lecture #23. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

ABSTRACT DATA TYPES: COLLECTIONS, LISTS, SETS, MAP, QUEUES. Thursday, June 30, 2011

11/27/12. CS202 Fall 2012 Lecture 11/15. Hashing. What: WiCS CS Courses: Inside Scoop When: Monday, Nov 19th from 5-7pm Where: SEO 1000

COMP 103 : Test. 2019, Jan 9 ** WITH SOLUTIONS **

תוכנה 1 מבני נתונים גנריים

COMP 103 : Test. 2018, Sept 12//(Corrected) ** WITH SOLUTIONS

Advanced Programming Generics Collections

Model Solutions. COMP 103: Test May, 2013

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

Final Exam CS 251, Intermediate Programming December 13, 2017

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Software 1 with Java. Java Collections Framework. Collection Interfaces. Online Resources. The Collection Interface

Model Solutions. COMP 103: Mid-term Test. 19th of August, 2016

U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2018 TRIMESTER 2 COMP 103 PRACTICE EXAM

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

תוכנה 1 מבני נתונים גנריים

Tutorial #11 SFWR ENG / COMP SCI 2S03. Interfaces and Java Collections. Week of November 17, 2014

CS 455 Midterm 2 Spring 2018 [Bono] Apr. 3, 2018

Collections. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Tony Valderrama, SIPB IAP 2010

CS2110: Software Development Methods. Maps and Sets in Java

CS314 Exam 1 - Fall Suggested Solution and Criteria 1

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

MIT AITI Lecture 18 Collections - Part 1

Algorithms. Produced by. Eamonn de Leastar

COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

Computational Expression

[Ref: Core Java Chp 13, Intro to Java Programming [Liang] Chp 22, Absolute Java Chp 16, docs.oracle.com/javase/tutorial/collections/toc.

EXAMINATIONS 2010 END YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

CS61BL Summer 2013 Midterm 2

EXAMINATIONS 2016 TRIMESTER 2

CONTAİNERS COLLECTİONS

Lecture Outline. Parametric Polymorphism and Java Generics. Polymorphism. Polymorphism

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering

Interfaces. An interface defines a set of methods. An interface declaration contains signatures, but no implementations.

CS 314 Midterm 2 Fall 2012

Prelim 2. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Introduction to Computer Science I

COMP 103 : Test. 2018, Sept 12//(Corrected)

Today: Java Library Classes for lists. Iterators, ListIterators. CS61B Lecture #7. Last modified: Fri Sep 12 14:41: CS61B: Lecture #7 1

Points off Total off Net Score. CS 314 Final Exam Spring Your Name Your UTEID

Java HashMap Interview Questions

Building Java Programs

CMPT 126: Lecture 11 Collections: Abstract Data Types and Dynamic Representation (Chapt. 12)

Binghamton University. CS-140 Fall Interfaces

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering

COMP6700/2140 Abstract Data Types: Queue, Set, Map

Unit5: Packages and abstraction. Prepared by: Dr. Abdallah Mohamed, AOU-KW Updated by Mrs. Malak EL-Amir AOU SAB Fall 14-15

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

Question 0. (1 point) Write the correct ID of the section you normally attend on the cover page of this exam if you have not already done so.

INTRODUCTION TO DATA AND PROCEDURE

Announcements/Follow-ups

Interfaces and Collections

EXAMINATIONS 2017 TRIMESTER 2

PIC 20A Collections and Data Structures

CS 310: Maps and Sets and Trees

Advanced Java Concepts Unit 9: Graph Exercises

Outline. iterator review iterator implementation the Java foreach statement testing

Collections and Maps

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology

Practical Session 3 Java Collections

DIT960 Datastrukturer

USAL1J: Java Collections. S. Rosmorduc

Advanced Programming Languages Effective Java Item 1. Spring 2015 Chungnam National Univ Eun-Sun Cho

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

Model Solutions. COMP 103: Test April, 2013

More on collec)ons and sor)ng. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

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

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

Building Java Programs

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 19 19:36: CS61B: Lecture #25 1

Java Collections Framework reloaded

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

CMSC 202. Containers

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Fundamental language mechanisms

Prelim 2 SOLUTION. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

COMP 250 Midterm #2 March 11 th 2013

Transcription:

Today's Agenda > To give a practical introduction to data structures > To look specifically at Lists, Sets, and Maps > To talk briefly about Generics in Java > To talk about interfaces in Java

Data Structures Data structures are objects of data stored in memory in a particular way along with accompanying methods to access them in various ways. Different data structures are appropriate for different tasks, and identifying the appropriate data structure can speed up your code immensely. Some of the general questions you would ask to determine an appropriate data structure include: Does the data need to be sorted? Will the data generally accessed in order? Or will I need "random access" to the data? Will I need to multiple copies of the same item? Can I look up my data with a unique key? How will I most often traverse my data?

Interfaces An interface is set of methods that provides a "contract" for any classes that implement it to fulfill. It provides no actual code, but simply guarantees that the implementing code must include those methods. A class can implement more than one interface at a time. public interface FlyingBehavior { public void flyalgorithm(); public interface ChirpingBehavior { public void chirpalgorithm(); public class BaldEagle implements FlyingBehavior, ChirpingBehavior { public void flyalgorithm() { swoopthendive(); public void chirpingalogrithm() { println("ca- caaaw!"); private void swoopthendive() { //do swooping and diving stuff...

Collec?ons Java includes a number of common data structures within the Collections Framework All Java data structures implement either the "collection" interface or the "map" interface. The collection interface requires all implementing classes to include a few basic methods universal to any collection of elements, including the ability to add and remove an object to the data structure the ability to get the number of elements stored in the data structure the ability to iterate through the elements in order Additionally, a number of basic algorithms can be run on these collections: sorting, searching, shuffling, etc (see the javadocs for java.util.collections)

Lists The most common collection is a List. A List is a sub- interface of Collection which requires some extra functionality, such as: the ability to get or set an object at a particular index the ability to grab a slice of the list There are lots of different types of Lists which have specialized functionality, but the most common are ArrayList and LinkedList. An ArrayList uses a native "primitive" array which makes it easy to grab things instantly at any position, but is slower to add things to it (unless you are added to the end of it). A LinkedList provides links between each element and its neighbors. It is slower to grab something in the middle of the list (as you have to traverse the List until you find it), but much faster to manipulate. If you add something to it you simply update the links, rather than shifting all of the elements. (draw on board)

Lists public class ListTest { public ListTest(){ List<Integer> ints = new ArrayList<Integer>(); for (int i = 10; i >= 0; i- - ) { ints.add(i); Collections.sort(ints); for (Integer i : ints) { System.out.println(i);

Maps A Map is a super fast collection which using a hashing function to quickly store and retrieve elements. Also known as a hash table or a dictionary. The trade- off is that the elements are not ordered in a meaningful way, and that iterating through the collection may not be as fast as iterating through a List. Also, you need to keep track of a "key" that maps to your object. This key must be unique. (draw on board) There are two main flavors, a HashMap and a TreeMap. The TreeMap guarantees that the keys are always sorted.

Maps public class MapTest { public MapTest() { Map<String, Integer> grades = new HashMap<String, Integer>(); grades.put("james", 93); grades.put("javier", 99); grades.put("miles", 95); Integer gradeforjavier = grades.get("javier");

Sets A Set is an iterable collection with no positional indexing that contains unique elements. It is backed by a Map. public class SetTest { public SetTest() { Set<String> people = new HashSet<String>(); people.add("mike"); people.add("gates"); people.add("xarene"); people.add("mike"); //warning! returns false because Mike is already in this Set! people.add(99); //error! This is Set of Strings! boolean gatespresent = people.contains("gates"); //returns true boolean gustavopresent = people.contains("gustavo"); //returns false

Collec?ons It is easy to transform one collection into another. example: Set to List for (String person : people) { System.out.println(person) ; //not guarenteed to be in order! Collections.sort(people); //error! can't sort a HashSet! List<String> peoplelist = new ArrayList<String>(people); //now we have a List of people which we can sort Collections.sort(peopleList); for (String person : peoplelist) { System.out.println(person) ; //okay this is in order!

Equality see code example (SetTest.java)