1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.

Slides:



Advertisements
Similar presentations
Introduction to Computation and Problem Solving Class 32: The Java® Collections Framework Prof. Steven R. Lerman and Dr. V. Judson Harward.
Advertisements

Java Programming: Advanced Topics 1 Collections and Utilities.
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.
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L16 (Chapter 22) Java Collections.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
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.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
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.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Set, TreeSet, TreeMap, Comparable, Comparator. Def: The abstract data type set is a structure that holds objects and satifies ARC: Objects can be added.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities Chapter 4.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
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.
1 Java's Collection Framework By Rick Mercer with help from The Java Tutorial, The Collections Trail, by Joshua BlockThe Collections Trail.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
Big Java Chapter 16.
111 © 2002, Cisco Systems, Inc. All rights reserved.
 Pearson Education, Inc. All rights reserved Collections.
Chapter 18 Java Collections Framework
Computer Science 209 Software Development Java Collections.
תוכנה 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.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
1 Interfaces in Java’s Collection Framework Rick Mercer.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Set and Map IS 313, Skeletons  Useful coding patterns  Should understand how and why they work how to use  possible quiz material.
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.
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.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
1 Collections. 2 Concept A collection is a data structure – actually, an object – to hold other objects, which let you store and organize objects in useful.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
Collections Dwight Deugo Nesa Matic
CSE 143 Lecture 11: Sets and Maps reading:
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
©Karsten Lundqvist Example: Array and Arrays 1 import java.util.*; public class Example { private int intValues[] = { 1, 2, 3, 4, 5, 6 }; private double.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Sets and Maps Chapter 9.
Chapter 19 Java Data Structures
Software Development Java Collections
Standard Template Library (STL)
Road Map CS Concepts Data Structures Java Language Java Collections
19 Collections.
Java Collections Overview
CS313D: Advanced Programming Language
Lecture 6: Collections.
Practical Session 3 Java Collections
Sets and Maps Chapter 9.
Hashing in java.util
Set and Map IS 313,
Presentation transcript:

1 L43 Collections (3)

2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk through” a collection.  To use persistent hash tables manipulated with objects of class Properties.  To use synchronization and modifiability wrappers.

Stack Class of Package java.util Stack – Implements stack data structure – Extends class Vector – Stores references to objects

4 Outline StackTest.java (1 of 4) Line 10 Lines 19, 21, 23, 25 Create an empty Stack of type Number Stack method push adds object to top of Stack

5 Outline StackTest.java (2 of 4) Line 36 Line 49 Stack method pop removes element from top of Stack Stack method isEmpty returns true if Stack is empty

6 Outline StackTest.java (3 of 4)

7 Outline StackTest.java (4 of 4) Program output

Class PriorityQueue and Interface Queue Interface Queue – New collection interface introduced in J2SE 5.0 – Extends interface Collection – Provides additional operations for inserting, removing and inspecting elements in a queue Class PriorityQueue – Implements the Queue interface – Orders elements by their natural ordering Specified by Comparable elements’ compareTo method Comparator object supplied through constructor

9 Outline PriorityQueueTest.java Line 10 Lines Line 20 Line 22 Line 23 Program output Create a PriorityQueue that stores Double s with an initial capacity of 11 elements and orders the elements according to the object’s natural ordering Use method offer to add elements to the priority queue Use method size to determine whether the priority queue is empty Use method peek to retrieve the highest-priority element in the queue Use method pool to remove the highest-priority element from the queue

Sets Set – Collection that contains unique elements – HashSet Stores elements in hash table – TreeSet Stores elements in tree

11 Outline SetTest.java (1 of 2) Line 18 Create a List that contains String objects

12 Outline SetTest.java (2 of 2) Line 24 Line 27 Program output Method printNonDuplicates accepts a Collection of type String Construct a HashSet from the Collection argument

13 Outline SortedSetTest.java (1 of 3) Lines Create TreeSet from names array

14 Outline SortedSetTest.java (2 of 3) Line 24 Line 28 Line 31 Line 32 Use TreeSet method headSet to get TreeSet subset less than "orange" Use TreeSet method tailSet to get TreeSet subset greater than "orange" Methods first and last obtain smallest and largest TreeSet elements, respectively

15 Outline SortedSetTest.java (3 of 3) Program output

Maps Map – Associates keys to values – Cannot contain duplicate keys Called one-to-one mapping – Implementation classes Hashtable, HashMap – Store elements in hash tables TreeMap – Store elements in trees – Interface SortedMap Extends Map Maintains its keys in sorted order

Maps (Cont.) Map implementation with hash tables – Hash tables Data structure that use hashing – Algorithm for determining a key in table Keys in tables have associated values (data) Each table cell is a hash “bucket” – Linked list of all key-value pairs that hash to that cell – Minimizes collisions

18 Outline WordTypeCount.java (1 of 4) Line 17 Create an empty HashMap with a default capacity 16 and a default load factor The keys are of type String and the values are of type Integer

19 Outline WordTypeCount.java (2 of 4) Line 30 Line 33 Line 35 Line 38 Line 40 Line 41 Line 44 Map method containsKey determines whether the key specified as an argument is in the hash table Create a StringTokenizer to break the input string argument into its component individual words Use method get to obtain the key’s associated value in the map Use StringTokenizer method hasMoreTokens to determine whether there are more tokens in the string Use StringTokenizer method nextToken to obtain the next token Increment the value and use method put to replace the key’s associated value Create a new entry in the map, with the word as the key and an Integer object containing 1 as the value

20 Outline WordTypeCount.java (3 of 4) Line 51 Lines Line 63 Use HashMap method keySet to obtain a set of the keys Access each key and its value in the map Call Map method size to get the number of key-value pairs in the Map Call Map method isEmpty to determine whether the Map is empty

21 Outline WordTypeCount.java (4 of 4) Program output

Properties Class Properties – Persistent Hashtable Can be written to output stream Can be read from input stream – Provides methods setProperty and getProperty Store/obtain key-value pairs of String s Preferences API – Replace Properties – More robust mechanism

23 Outline PropertiesTest.java (1 of 5) Line 16 Lines Line 26 Create empty Properties Properties method setProperty stores value for the specified key

24 Outline PropertiesTest.java (2 of 5) Line 33 Line 41 Use Properties method clear to empty the hash table Use Properties method getProperty to locate the value associated with the specified key

25 Outline PropertiesTest.java (3 of 5) Line 57 Properties method store saves Properties contents to FileOutputStream

26 Outline PropertiesTest.java (4 of 5) Line 75 Line 89 Line 95 Properties method load restores Properties contents from FileInputStream Use Properties method keySet to obtain a Set of the property names Obtain the value of a property by passing a key to method getProperty

27 Outline PropertiesTest.java (5 of 5) Program output

Synchronized Collections Built-in collections are unsynchronized – Concurrent access to a Collection can cause errors – Java provides synchronization wrappers to avoid this Via set of public static methods

29 Fig | Synchronization wrapper methods.

Unmodifiable Collections Unmodifiable wrapper – Converting collections to unmodifiable collections – Throw UnsorrtedOperationException if attempts are made to modify the collection

31 Fig | Unmodifiable wrapper methods.

Abstract Implementations Abstract implementations – Offer “bare bones” implementation of collection interfaces Programmers can “flesh out” customizable implementations – AbstractCollection – AbstractList – AbstractMap – AbstractSequentialList – AbstractSet – AbstractQueue