1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.

Slides:



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

Introduction to Computation and Problem Solving Class 32: The Java® Collections Framework Prof. Steven R. Lerman and Dr. V. Judson Harward.
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
JAVA Programming (Session 7) When you are willing to make sacrifices for a great cause, you will never be alone. Instructor:
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Data Structures & Java Collections Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
Chapter 19 Java Data Structures
Java Collections. Collections, Iterators, Algorithms CollectionsIteratorsAlgorithms.
Java's Collection Framework
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
Sets and Maps Part of the Collections Framework. The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
1 Java's Collection Framework By Rick Mercer with help from The Java Tutorial, The Collections Trail, by Joshua BlockThe Collections Trail.
3-1 Polymorphism with Java Interfaces Rick Mercer.
Big Java Chapter 16.
111 © 2002, Cisco Systems, Inc. All rights reserved.
COLLECTIONS Byju Veedu s/Collection.html.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Object Oriented Programming Ders 10: Data Structures Mustafa Emre İlal
Chapter 18 Java Collections Framework
תוכנה 1 תרגול 8 – מבני נתונים גנריים. 2 Java Collections Framework Collection: a group of elements Interface Based Design: Java Collections Framework.
Data structures and algorithms in the collection framework 1.
Java.util.Vector Brian Toone 10/3/07 Updated 10/10/07.
Arrays, ArrayLists, and Collections. Rationale Suppose we have a program to compute the average of three numbers. We could write a simple little method.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
1 Interfaces in Java’s Collection Framework Rick Mercer.
Interface: (e.g. IDictionary) Specification class Appl{ ---- IDictionary dic; dic= new XXX(); application class: Dictionary SortedDictionary ----
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
SETS AND MAPS Collections of Data. Advanced Data Structures Often referred to as the Java Collections Framework…. Set and map data types Hash tables Binary.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
13 Collections Framework. 2 Contents What is Collection? Collections Framework Collections Hierarchy Collections Implementations Set List Map.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
Priority Queues. Priority Queue ADT A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the Priority Queue.
Nov 22, 2010IAT 2651 Java Collections. Nov 22, 2010IAT 2652 Data Structures  With a collection of data, we often want to do many things –Organize –Iterate.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
1 Copyright © 2011 Tata Consultancy Services Limited COLLECTIONS By TEAM 5 Rajendhiran Sivan Christi Yashwanth Bijay Smruthi Satyajit.
University of Limerick1 Collections The Collection Framework.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
©Karsten Lundqvist Example: Array and Arrays 1 import java.util.*; public class Example { private int intValues[] = { 1, 2, 3, 4, 5, 6 }; private double.
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Using the Java Collection Libraries COMP 103 # T2
Chapter 19 Java Data Structures
University of Central Florida COP 3330 Object Oriented Programming
Interfaces in Java’s Collection Framework
Road Map CS Concepts Data Structures Java Language Java Collections
Java Collections Overview
Môn: Lập trình Hướng đối tượng (Object Oriented Programming)
Programming Languages
CSE 1020: The Collection Framework
1.4 ทบทวน JAVA OO Programming Concepts Declaring and Creating Objects
CS 240 – Advanced Programming Concepts
Introduction to Java Collection
Presentation transcript:

1 Java's Collection Framework Map and Sets

2 Collection Framework  A collections framework is a unified architecture for representing and manipulating collections. It has: –Interfaces: abstract data types representing collections –Implementations: concrete implementations of the collection interfaces –Algorithms: methods that perform useful computations, such as searching and sorting These algorithms are said to be polymorphic: the same method can be used on different implementations

3 Interfaces  An interface describes a set of methods: –no constructors or instance variables  Interfaces must be implemented by classes –646 java classes implement >= 1 interfaces (‘02)  2 or more classes implement an interface –Classes guaranteed to have the same methods –Objects can be treated as the same type –Can use different algorithms / instance variables

4 Collection interfaces Queue since Java 5

5 Implementations  A collection class –implements an ADT as a Java class –implements all methods of the interface –selects appropriate instance variables –can be instantiated  Java implements interfaces with – List: ArrayList, LinkedList, Vector – Map: HashMap, TreeMap – Set: TreeSet, HashSet

6 Algorithms  Java has polymorphic algorithms to provide functionality for different types of collections –Sorting (e.g. sort)Sorting –Shuffling (e.g. shuffle)Shuffling –Routine Data Manipulation (e.g. reverse, addAll)Routine Data Manipulation –Searching (e.g. binarySearch)Searching –Composition (e.g. frequency)Composition –Finding Extreme Values (e.g. max)Finding Extreme Values

7 Two Useful ADTs  List: a collection with a first element, a last element, distinct predecessors and successors –duplicates that "equals" each other are allowed  Map: maps keys to values Map –Maps cannot contain duplicate keys –Each key maps at most one value

8 Map and SortedMap  The Map interface defines methodsMap interface – get, put, contains, keySet, values, entrySet  TreeMap implements Map –put, get, remove: O(log n)  HashMap implements Map –put, get, remove: O(1)

9 Set and SortedSet  Some Map methods return Set  The Set interface – add, addAll, remove, size, but no get !  Some implementations – TreeSet : values stored in order, O(log n) – HashSet : values in a hash table, no order, O(1)

10 Let's play  Set up a small collection of mappings Map tm = new TreeMap (); tm.put("M", new BankAccount("Michel", )); tm.put("G", new BankAccount("Georgie", )); tm.put("S", new BankAccount("Sam", )); tm.put("P", new BankAccount("Pei", )); tm.put("Z", new BankAccount("Zac", )); // Coming up: Some Map methods

11 keySet() // Get the set of keys in the Map. // With TreeMap, the keys are ordered. Set keys = tm.keySet(); assertEquals("[G, M, P, S, Z]", keys.toString());

12 values() // The values Collection accountCollection = tm.values(); Iterator itr = accountCollection.iterator(); double allTheMoney = 0.0; while (itr.hasNext()) { allTheMoney += itr.next().getBalance(); } assertEquals( , allTheMoney, 0.01);

13 entrySet() // The mappings Collection > mapping = tm.entrySet(); Iterator > itr2 = mapping.iterator(); while (itr2.hasNext()) { System.out.print("<" +itr2.next().toString() + "> "); }

14 Maps do not have an iterator  Code Demo: Sum up all balances in tm –There is no iterator method in TreeMap

15 Thank You