Data Structures & Java Collections Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

JAVA Programming (Session 7) When you are willing to make sacrifices for a great cause, you will never be alone. Instructor:
Collections CS3250. Sources  Slides by Professor Chuck Allison  Core Java, by Cay S. Horstmann and Gary Cornell  The Java Tutorial 
Data Structures & Java Generics Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Working With Collections in the AP ™ Java Subset 2003 ACTE Convention © 2003 by Kenneth A. Lambert.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Department of Computer Science University of Maryland, College Park
Midterm 2 Overview Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
Maps & Hashing Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
15-Jun-15 Lists in Java Part of the Collections Framework.
Professor Evan Korth (adapted from Sun’s collections documentation)
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Collections A First Glimpse. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector is a kind of collection.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
Java Review Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
12-Jul-15 Lists in Java Part of the Collections Framework.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.
Tree Traversals & Maps Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 19 Java Data Structures
Java's Collection Framework
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
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.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities Chapter 4.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
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.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
Computer Science 209 Software Development Java Collections.
Data structures Abstract data types Java classes for Data structures and ADTs.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
1.0tCopyright © 1998 Purple Technology, Inc. 1 Java Collections Framework Authored by Alex Chaffee Copyright © 1998 Purple Technology, Inc. All rights.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
The Java Collections Framework Based on
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.
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.
Heaps & Priority Queues
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
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.
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Using the Java Collection Libraries COMP 103 # T2
Efficiency of in Binary Trees
structures and their relationships." - Linus Torvalds
Introduction to Collections
JAVA Collections Framework Set Interfaces & Implementations
Introduction to Collections
Part of the Collections Framework
Introduction to Collections
Introduction to Collections
Data Structures II AP Computer Science
structures and their relationships." - Linus Torvalds
Presentation transcript:

Data Structures & Java Collections Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park

Algorithms and Data Structures Algorithm Sequence of steps used to solve a problem Operates on collection of data Each element of collection  data structure Data structure Combination of simple / composite data types Design  information stored for each element Choice affects characteristic & behavior of algorithm May severely impact efficiency of algorithm

Data Structures Taxonomy Classification scheme Based on relationships between element CategoryRelationship Linearone  one Hierarchicalone  many Graphmany  many Setnone  none

Data Structures Core operations Add element Remove element Iterate through all elements Compare elements

Linear Data Structures One-to-one relationship between elements Each element has unique predecessor Each element has unique successor

Linear Data Structures Core operations Find first element (head) Find next element (successor) Find last element (tail) Terminology Head  no predecessor Tail  no successor

Example Linear Data Structures List Collection of elements in order Queue Elements removed in order of insertion First-in, First-out (FIFO) Stack Elements removed in opposite order of insertion First-in, Last-out (FILO)

Hierarchical Data Structures One-to-many relationship between elements Each element has unique predecessor Each element has multiple successors

Hierarchical Data Structures Core operations Find first element (root) Find successor elements (children) Find predecessor element (parent) Terminology Root  no predecessor Leaf  no successor Interior  non-leaf Children  successors Parent  predecessor

Example Hierarchical Data Structures Tree Single root Forest Multiple roots Binary tree Tree with 0–2 children per node TreeBinary Tree

Graph Data Structures Many-to-many relationship between elements Each element has multiple predecessors Each element has multiple successors

Graph Data Structures Core operations Find successor nodes Find predecessor nodes Find adjacent nodes (neighbors) Terminology Directed  traverse edges in one direction Undirected  traverse edges in both directions Neighbor  adjacent node Path  sequence of edges Cycle  path returning to same node Acyclic  no cycles

Example Graph Data Structures Undirected graph Undirected edges Directed graph Directed edges Directed acyclic graph (DAG) Directed edges, no cycles UndirectedDirectedDAG

Set Data Structures No relationship between elements Elements have no predecessor / successor Only one copy of element allowed in set Set B Set C Set A

Set Data Structures Core operations Set versions of core operations Add set, remove set, compare set Terminology Subset  elements contained by set Union  select elements in either set Intersection  select elements in both sets Set difference  select elements in one set only

Example Set Data Structures Set Basic set Map Map value to element in set Hash Table Maps value to element in set using hash function SetMapHash Table h(n)

Java Collections Framework Collection Object that groups multiple elements into one unit Also called container Collection framework consists of Interfaces Abstract data type Implementations Reusable data structures Algorithms Reusable functionality

Java Collections Framework Goals Reduce programming effort Make APIs easier to learn Make APIs easier to design and implement Reuse software Increase performance

Core Collection Interfaces Collection Group of elements Set No duplicate elements List Ordered collection Map Maps keys to elements SortedSet, SortedMap Sorted ordering of elements

Core Collection Hierarchy

Collections Interface Implementations General implementations Primary public implementation Example List – ArrayList, LinkedList Set – TreeSet, HashSet Map – TreeMap, HashMap Wrapper implementations Combined with other interfaces Example synchronizedArrayList, unmodifiableHashMap

Collections Interface Methods boolean add(Object o) Add specified element boolean contains(Object o) True if collection contains specified element boolean remove(Object o) Removes specified element from collection boolean equals(Object o) Compares object with collection for equality Iterator iterator() Returns an iterator over the elements in collection

Collections Interface Methods boolean addAll(Collection c) Adds all elements in specified collection boolean containsAll(Collection c) True if collection contains all elements in collection boolean removeAll(Collection c) Removes all elements in specified collection boolean retainAll(Collection c) Retains only elements contained in specified collection

Collections Interface Methods void clear() Removes all elements from collection boolean isEmpty() True if collection contains no elements int size() Returns number of elements in collection Object[] toArray() Returns array containing all elements in collection

Iterator Interface Iterator Common interface for all Collection classes Used to examine all elements in collection Properties Order of elements is unspecified (may change) Can remove current element during iteration Works for any collection

Iterator Interface Interface public interface Iterator { boolean hasNext(); Object next(); void remove(); // optional, called once per next() } Example usage Iterator i = myCollection.iterator(); while (i.hasNext()) { myCollectionElem x = (myCollectionElem) i.next(); }