Data structures and algorithms in the collection framework 1 Part 2.

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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Hashing as a Dictionary Implementation
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 18: Hash Tables.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
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 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
1 Hash-Based Indexes Chapter Introduction  Hash-based indexes are best for equality selections. Cannot support range searches.  Static and dynamic.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 19 Java Data Structures
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
Collection types Collection types.
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:
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
CS 221 Analysis of Algorithms Data Structures Dictionaries, Hash Tables, Ordered Dictionary and Binary Search Trees.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
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.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
The Java Collections Framework (JCF) Introduction and review 1.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 11 Sets © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1 Concrete collections II. 2 HashSet hash codes are used to organize elements in the collections, calculated from the state of an object –hash codes are.
Big Java Chapter 16.
Not overriding equals  what happens if you do not override equals for a value type class?  all of the Java collections will fail in confusing ways 1.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Chapter 18 Java Collections Framework
Data structures and algorithms in the collection framework 1.
CIS 068 Welcome to CIS 068 ! Lesson 10: Data Structures.
Hashing Hashing is another method for sorting and searching data.
Hashing as a Dictionary Implementation Chapter 19.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Indexed Sequential Access Method.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Data Structures Systems Programming. Systems Programming: Data Structures 2 2 Systems Programming: 2 Data Structures  Queues –Queuing System Models –Queue.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Dictionaries, Hash Tables, Collisions Resolution, Sets Svetlin Nakov Telerik Corporation
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Chapter 11 Sets © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
CS 261 – Data Structures Hash Tables Part II: Using Buckets.
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
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Java Collections OOP tirgul No
Chapter 19 Java Data Structures
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
structures and their relationships." - Linus Torvalds
structures and their relationships." - Linus Torvalds
Presentation transcript:

Data structures and algorithms in the collection framework 1 Part 2

Data structures and algorithms in the collection framework 2 Interfaces Collection –Methods common to all collections (except map) List –An ordered collection. Set –No duplicate elements. –equals(Object obj) method on the elements is important. Queue –Normally implemented as FIFO (first-in-first-out) –New in Java 5.0 SortedSet –Set that guarantees that elements are traversed in order Map –Contains pairs (key, value) –Values are retrieved by the key: value = map.get(key) SortedMap –Map that guarantees that elements are sorted according to the key.

Data structures and algorithms in the collection framework 3 Implementations overview General purpose implementations Interfaces Hash table Resizable array Balanced tree Linked List SetHashSetTreeSet ListArrayListLinkedList MapHashMapTreeMap

Data structures and algorithms in the collection framework 4 Wrapper implementations The “wrapper” idea –Goals: Extend the functionality of an object transparent to its clients (i.e. the users of the class) Known examples are diff. streams Two kinds of wrappers in the collection framework –Synchronized wrappers –Unmodifiable wrappers

Data structures and algorithms in the collection framework 5 Synchronized wrappers Purpose –Collection implementations are generally unsynchronized Methods are not synchronized Exceptions to the “rule”: Vector, HashTable (old implementations) –Synchronized collections can be made using methods in the class Collections. Static methods in class Collections –Collection synchronizedCollection( Collection c ) –List synchronizedList( List l ) –Set synchronizedSet( Set s ) –Map synchronizedMap( Map m )

Data structures and algorithms in the collection framework 6 Unmodifiable wrappers Purpose –Collections are generally modifiable They have methods like add, remove, etc. –Unmodifiable collections can be made using methods in the class Collections If you attempt to call modifying methods like add, remove, etc. you will get an UnsupportedOperationException. –Not very good object-oriented design, but it works. Static methods in class Collections –unmodifiableCollection( Collection c ) –unmodifiableList( List l ) –unmodifiableSet( Set s ) –unmodifiableMap( Map m )

Data structures and algorithms in the collection framework 7 Decorator pattern Also know as “wrapper pattern”. Synchronized wrapper and unmodifiable wrapper are applications of the decorator pattern. Idea –Extend the functionality of an object transparent to its clients (i.e. the users of the class) How –A class extends another class and at the same time aggregates an object of the super class. –Methods in the subclass does something special and call the super class’ method for the basic work. Alternative to (lots of) inheritance Used intensively in java.io –BufferedReader wraps any Reader Providing buffering, for speed.

Data structures and algorithms in the collection framework 8 Layered implementation Interface –a specification Abstract class –a partly implementation of a specification –Used by one or more [concrete] classes [Concrete] class –Implementation of a specification Example –List: Interface –AbstractList: Partial implementation –ArrayList and LinkedList: [Concrete] implementations

Data structures and algorithms in the collection framework 9 Custom implementations Reasons for writing a custom implementation –Persistency You want the collection to reside on the hard disk (file or database), not just in main memory. –High performance, special purpose You want a fast implementation for some special purpose. Use an abstract class (partial implementation) if possible –It will do most of the work.

Data structures and algorithms in the collection framework 10 Algorithms Class Collections and class Arrays –Not to be confused with the interface Collection Some static methods –Sorting lists –Searching ordered lists –Finding extreme values (max, min)

Data structures and algorithms in the collection framework 11 Sorting lists Works with lists –Not general collections, since they have no notion of a sequence. Algorithm: Merge sort –Fast: n*log(n) guaranteed, even faster on nearly sorted lists. –Stable: Doesn’t reorder equal elements. –Idea: Divide and conquer + recursion Divide the list in 2 sub-lists and sort the sub-lists. Conquer: Merge the small lists. Methods –Collections.sort( List l ) –Collections.sort( List l, Comparator c ) If you want to use your own comparator, not the natural order.

Data structures and algorithms in the collection framework 12 Searching ordered lists Works with ordered lists –Not general lists If the list is not ordered, use Collections.sort( List l ) before searching –Not general collections Algorithm: Binary search –Speed O( log n ) for random access list implementations (like ArrayList) O( n ) for iterator-based list implementations (like LinkedList) –Idea: Divide-and-conquer + recursion Find the middle element. If middle element < searchingFor –Search in the left hand part of the list Else –Search the right hand part of the list Methods –Collections.binarySearch( List l, Object searchingFor ) –Collections.binarySearch( List l, Object searchingFor, Comparator c ) If the list is not ordered by the natural order, but by the specified comparator.

Data structures and algorithms in the collection framework 13 Finding extreme values Finds min, max in any collection –Algorithm is iterator-based. –If the collection is known to be ordered don’t used min, max. Simply call get(0) or get( size() ) Methods in Collections –Object min( Collection c ) –Object max( Collection c ) –Object min( Collection c, Comparator c ) –Object max( Collection c, Comparator c ) If you prefer your own comparator to the natural order.

Data structures and algorithms in the collection framework 14 Arrays Algorithms similar to Collections Working on arrays of different types –int, double, etc. and Object Convenience methods –List view of arrays List list = Arrays.asList( someArray ) Useful for printing an array –System.out.println( list ) –System.out.println( Arrays.asList(someArray) ) –Java 5.0 System.out.println( Arrays.toString(someArray) )

Data structures and algorithms in the collection framework 15 The term “framework” The term “framework” denotes a set of classes that can be extended … Examples –Collections framework You can extend the framework creating your own implementations –Swing You can extends the framework extending JPanel and many other classes. –java.io Some frameworks can be used as is – others need custom extensions before they are useful.

Data structures and algorithms in the collection framework 16 Hashing Binary search is O( log n ) We want something better: O(1) Idea: –Compute a number (called “hash value”) from the data we are searching for. –Use the hash value as an index in an array (called “hash table”) –Every element in the array holds a “bucket” of elements. –If every bucket holds few elements (preferably 1) then hashing is O(1)

Data structures and algorithms in the collection framework 17 Hash function A good hash function should distribute elements evenly in the hash table –The worst hash function always returns 0 Example –Hash table with 10 slots –hash( int i ) { return i % 10; } % is the remainder operator. –More generally Hash table with N slots hash( T t ) { return operation( t ) % N; } –The operation should be fast and distribute elements well. Java, class Object –int hashCode() is a hash function Your classes should override hashCode() hashCode() and equals() –a.equals(b) is true ⇒ a.hashCode() == b.hashCode() –a.hashCode() == b.hashCode() ⇒ a.equals(b) is truenot necessarily! –a.hashCode() != b.hashCode() ⇒ a.equals(b) is false

Data structures and algorithms in the collection framework 18 Hash table A hash table is basically an array. What if 2 elements computes the same hash value (i.e. same array index)? –Two solutions Linear probing: 1 element in every array cell –Try the next empty slot in the hash table –Mark slots as “here was once an element” Chaining: A list of elements in every array cell –Add the element to the list In any case searching can degenerate if the hash function does not distribute elements evenly. Problem –If a hash table is almost full searching degenerates Solution –Rehashing: Create a larger hash table + update hash function + move elements to new hash table.

Data structures and algorithms in the collection framework 19 Binary search tree Basic tree terms –Node, descendant, root, leaf –A tree has 1 root Binary tree –A node has at most 2 (bi-) descendants. Search tree –Nodes are ordered. Small values to the left and large values to the right. –Makes searching fast.

Data structures and algorithms in the collection framework 20 Balanced search trees A binary search tree might degenerate into a list. –Searching is no longer fast We want the search tree to be balanced. –Without having to completely reorganize the tree at every insert / delete.