BBM 102 Introduction to Programming II Spring 2017

Similar documents
BBM 102 Introduction to Programming II Spring 2017

BBM 102 Introduction to Programming II Spring 2017

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

36. Collections. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Computational Expression

Introduction to Computer Science I

Important Dates. Game State and Tree. Today s topics. Game Tree and Mini-Max. Games and Mini-Max 3/20/14

Algorithms. Produced by. Eamonn de Leastar

Questions and Answers. Q.3) asses is not part of Java's collection framework?explanation:

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

Collections Framework: Part 2

CONTAİNERS COLLECTİONS

MODULE 6q - Exceptions

CSC 1214: Object-Oriented Programming

Overview of Java ArrayList, HashTable, HashMap, Hashet,LinkedList

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

Java Collections Framework reloaded

Chapter 5 Java Collection. Wang Yang

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

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

CS Ananda Gunawardena

CS11 Java. Winter Lecture 8

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

BBM 102 Introduction to Programming II Spring 2017

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

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

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

Lecture 6 Collections

Standard ADTs. Lecture 19 CS2110 Summer 2009

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

BBM 102 Introduction to Programming II Spring Inheritance

Collections Questions

BBM 102 Introduction to Programming II Spring 2017

MIT AITI Lecture 18 Collections - Part 1

Java Collections Framework

Appendix A: Interfaces and Classes in the AP Java Subset (AB)

BBM 102 Introduction to Programming II Spring 2017

BBM 102 Introduction to Programming II Spring 2017

CS 310: Maps and Sets

9/16/2010 CS Ananda Gunawardena

An Interface with Generics

Java Data Structures Collections Framework BY ASIF AHMED CSI-211 (OBJECT ORIENTED PROGRAMMING)

Get started with the Java Collections Framework By Dan Becker

CS 106A, Lecture 19 ArrayLists

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

INTRODUCTION TO DATA AND PROCEDURE

Abstract Data Types (ADTs) Queues & Priority Queues. Sets. Dictionaries. Stacks 6/15/2011

COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE

BBM 102 Introduction to Programming II Spring 2017

Generics. IRS W-9 Form

Collections (Collection Framework) Sang Shin Java Technology Architect Sun Microsystems, Inc.

Generic Collections CSC Spring 2019 Howard Rosenthal

CS 106A, Lecture 27 Final Exam Review 1

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

More sophisticated behaviour Lecture 09

Collection Framework Collection, Set, Queue, List, Map 3

Topic 10: The Java Collections Framework (and Iterators)

Lecture 9: Lists. MIT-AITI Kenya 2005

CITS1001 week 4 Grouping objects

1. Suppose you are using a HashMap<String,Integer>. What is the runtime of put and get assuming you have a reasonable hash function?

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 7. (A subset of) the Collections Interface. The Java Collections Interfaces

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

EECS 2011 M: Fundamentals of Data Structures

Introduction to Collections

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

23 Sorting a text file using an ArrayList 24 The SortList class? 26 The SortList class? 27 Collections API: Collections class 30 The SortList class? 3

Getting Started with JDO

School of Computing and Information Sciences. Course Title: Data Structures Date: 3/30/2010 Course Number: COP 3530 Number of Credits: 3

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

CSC 321: Data Structures. Fall 2016

CSC 321: Data Structures. Fall 2017

What is the Java Collections Framework?

CS 106A, Lecture 20 ArrayLists and HashMaps

Java 2. Course Outcome Summary. Western Technical College. Course Information. Course History. Course Competencies

27/04/2012. Objectives. Collection. Collections Framework. "Collection" Interface. Collection algorithm. Legacy collection

Computer Science II (Spring )

STANDARD ADTS Lecture 17 CS2110 Spring 2013

Highlights of Previous Lecture

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

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

Sets and Maps. Part of the Collections Framework

COMP6700/2140 Implementation of ADT

Hashing as a Dictionary Implementation

SUMMARY INTRODUCTION COLLECTIONS FRAMEWORK. Introduction Collections and iterators Linked list Array list Hash set Tree set Maps Collections framework

Model 4.2 Faculty member + student Course syllabus for Advanced programming language - CS313D

CS 310: Maps and Sets and Trees

Java Collections Framework: Interfaces

CISC 3115 TY3. C24a: Lists. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/15/2018 CUNY Brooklyn College

Java Collection Framework

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

Type Parameters: E - the type of elements returned by this iterator Methods Modifier and Type Method and Description

Notes on access restrictions

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

CS 106A, Lecture 27 Final Exam Review 1

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

From Java Code to Java Heap Understanding the Memory Usage of Your Application

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

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

Abstract Data Types (ADTs) Example ADTs. Using an Abstract Data Type. Class #08: Linear Data Structures

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

Transcription:

BBM 102 Introduction to Programming II Spring 2017 Collections Framework Today The java.util.arrays class Java Collection Framework java.util.collection interface java.util.list interface java.util.arraylist class java.util.set interface java.util.hashset class java.util.map interface java.util.hashmap class java.util.hashtable class java.util.properties class Instructors: Ayça Tarhan, Fuat Akal, Gönenç Ercan, Vahid Garousi 1 2 java.util.arrays Arrays Example Provides high-level static methods for manipulating arrays, such as: sort for sorting an array, binarysearch for searching a sorted array, equals for comparing arrays fill for placing values into an array 3 4

Arrays Example (continued) Arrays Example (continued) 5 6 Arrays Example (continued) Java Collections Framework Group of objects can be kept in an array, but arrays are not feasible when number of objects increase or decrease during the execution of the program The Java Collections Framework is a collection of interfaces and classes that may be used to manipulate groups of objects The classes implemented in the Java Collections Framework serve as reusable data structures and include algorithms for common tasks such as sorting or searching The framework uses parameterized classes so you can use them with the classes of your choice The framework is largely contained in the package java.util 7 8

Basic collections The three major collections in Java: Set: A collection of elements that is guaranteed to contain no duplicates. List: An ordered collection of elements, often accessed by integer indexes or by iteration. Map: A collection of key/value pairs in which each key is associated with a corresponding value. java.util.collection interface It is the highest level of Java s framework for collection classes It describes the basic operations that all collection classes (except Map) should implement (listed in the next page) The JDK does not provide any direct implementations of this interface: it provides implementations of more specific subinterfaces like Set and List as in the example diagram below: 9 10 java.util.set interface What is a Set? A Set is a Collection that contains unique elements (i.e., no duplicate elements). The collections framework contains several Set implementations, including HashSet Major operations are: Adding an element Removing an element Checking whether an element exists Useful for answering questions of the form have I seen this before? 11 12

13 14 15 16

17 18 Basic Set Operations HashSet - Example To insert an item: set.add(value) To check whether a value exists set.contains(value) To remove an item set.remove(value) 19 20

HashSet Example (continued) What is a List? 21 22 java.util.list interface A List (sometimes called a sequence) is an ordered Collection It can contain duplicate elements Like array indices, List indices are zero based (i.e., the first element's index is zero) In addition to the methods inherited from Collection, List provides methods for; manipulating elements via their indices manipulating a specified range of elements searching for elements getting a ListIterator to access the elements. Iterators To visit every element of a collection, you can use the for each loop: for(elemtype elem: collection){ } Alternatively, you can use an iterator, an object whose job is to walk over the elements of a collection. The iterator has two commands: hasnext() returns true if there are more items to visit. next() returns the next item and moves the iterator to the next position. 23 24

ArrayList and Iterator - Example ArrayList and Iterator Example (continued) 25 26 ArrayList and Iterator - Example (continued) What is a Map? 27 28

java.util.map interface HashMap - Example Maps associate keys to values and cannot contain duplicate keys (i.e., each key can map to only one value) Maps differ from Sets in that Maps contain keys and values, whereas Sets contain only values 29 30 HashMap Example (continued) HashMap Example (continued) 31 32

What is a Hashtable? 33 34 java.util.properties class A Properties object is a persistent Hashtable that stores key-value pairs of strings assumes that you use methods setproperty and getproperty to manipulate the table rather than inherited Hashtable methods put and get. A common use of Properties objects is to maintain applicationconfiguration data or user preferences for applications 35 36

Properties - Example Properties Example (continued) 37 38 Properties Example (continued) Properties Example (continued) 39 40

Properties Example (continued) Properties Example (continued) 41 42 Summary Arrays are used for a group of objects, but maintanence is difficult when the size of the group changes during the execution of the program Java Collection Framework includes many interfaces and classes to easily manage groups of objects The interfaces and classes of the framework are mostly in java.util package Various interfaces include methods for special algorithms of various data structures Framework includes various implementations of these interfaces It is possible to implement or extend the interfaces/classes of the framework for new/different implementations of Acknowledgements The course material used to prepare this presentation is mostly taken/adopted from the list below: Java - How to Program, Paul Deitel and Harvey Deitel, Prentice Hall, 2012 Building Java Programs A Back to Basics Approach, Stuart Reges and Marty Stepp, Addison Wesley, 2011 Stanford, Collections lecture notes new/different data structures 43 44