Data Structures & Java Generics Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.

Slides:



Advertisements
Similar presentations
EXAMPLE: X Y Z T 5 VERTICES 6 EDGES S Collection List Set ArrayList LinkedList SortedSet HashSet TreeSet.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
JAVA Programming (Session 7) When you are willing to make sacrifices for a great cause, you will never be alone. Instructor:
Computer Science 112 Fundamentals of Programming II Overview of Collections.
CS 171: Introduction to Computer Science II
Trees Chapter 8.
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.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS102 – Data Structures Lists, Stacks, Queues, Trees, Hash Collections Framework David Davenport Spring 2002.
Graphs & Graph Algorithms 2
Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on Collections.
Java 1.5 & Effective Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Abstractions, Collections & Data Structures Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
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.
Java Review Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Tree Traversals & Maps Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 19 Java Data Structures
Java Collections. Collections, Iterators, Algorithms CollectionsIteratorsAlgorithms.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
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.
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.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Chapter 18 Java Collections Framework
Computer Science 209 Software Development Java Collections.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
Understanding Data Types and Collections Lesson 2.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
CS102 – Data Structures Lists, Stacks, Queues, Trees & HashTables. David Davenport.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
CPS120: Introduction to Computer Science Nell Dale John Lewis Abstract Data Types.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Slides by Donald W. Smith
Sixth Lecture ArrayList Abstract Class and Interface
Fundamentals of Programming II Overview of Collections
Unit 1 Hadoop and big data
Software Development Java Collections
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Chapter 10: Non-linear Data Structures
structures and their relationships." - Linus Torvalds
Java Collections Overview
Arrays and Linked Lists
Graphs Chapter 11 Objectives Upon completion you will be able to:
Graphs.
structures and their relationships." - Linus Torvalds
Cs212: Data Structures Lecture 7: Tree_Part1
Presentation transcript:

Data Structures & Java Generics Nelson Padua-Perez 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

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

New Features in Java 1.5 Enumerated types Enhanced for loop Autoboxing & unboxing Scanner Generic types Variable number of arguments (varargs) Static imports Annotations

Generics – Motivating Example Problem Utility classes handle arguments as Objects Objects must be cast back to actual class Casting can only be checked at runtime Example class A { … } class B { … } List myL = new List(); myL.add(new A()); // Add an object of type A … B b = (B) myL.get(0);// throws runtime exception // java.lang.ClassCastException

Solution – Generic Types Generic types Provides abstraction over types Can parameterize classes, interfaces, methods Parameters defined using notation Examples public class foo { … } public class List { … } Improves Readability & robustness Used in Java Collections Framework

Generics – Usage Using generic types Specify for utility class Automatically performs casts Can check class at compile time Example class A { … } class B { … } List myL = new List (); myL.add(new A()); // Add an object of type A A a = myL.get(0); // myL element  class A … B b = (B) myL.get(0); // causes compile time error

Generics – Issues Generics and subtyping Even if class A extends class B List does not extend List Example class B { … } class A extends B { … } // A is subtype of B B b = new A();// A used in place of B List myL = new List (); // compile time error // List used in place of List // List is not subtype of List