Data Structures in the Java API



Similar documents
Class 32: The Java Collections Framework

Collections in Java. Arrays. Iterators. Collections (also called containers) Has special language support. Iterator (i) Collection (i) Set (i),

JAVA COLLECTIONS FRAMEWORK

Working with Java Collections

The Java Collections Framework

Java Software Structures

Generic Types in Java. Example. Another Example. Type Casting. An Aside: Autoboxing 4/16/2013 GENERIC TYPES AND THE JAVA COLLECTIONS FRAMEWORK.

CSE 2123 Collections: Sets and Iterators (Hash functions and Trees) Jeremy Morris

Lecture 2: Data Structures Steven Skiena. skiena

D06 PROGRAMMING with JAVA

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Practical Session 4 Java Collections

Java Collection Framework hierarchy. What is Data Structure? Chapter 20 Lists, Stacks, Queues, and Priority Queues

Data Structures and Algorithms

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

Big O and Limits Abstract Data Types Data Structure Grand Tour.

DATA STRUCTURES USING C

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Basic Programming and PC Skills: Basic Programming and PC Skills:

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Introduction to Programming System Design. CSCI 455x (4 Units)

Java Coding Practices for Improved Application Performance

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

CMSC 202H. ArrayList, Multidimensional Arrays

core 2 Basic Java Syntax

Per imparare a programmare bisogna programmare

STACKS,QUEUES, AND LINKED LISTS

STORM. Simulation TOol for Real-time Multiprocessor scheduling. Designer Guide V3.3.1 September 2009

CompSci-61B, Data Structures Final Exam

Universidad Carlos III de Madrid

2 Collections Framework Overview

DATA STRUCTURE - STACK

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Chapter 8: Bags and Sets

Analysis of a Search Algorithm

Software Engineering Techniques

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

Introduction to Stacks

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Java SE 8 Programming

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Data Structures in Java. Session 15 Instructor: Bert Huang

Java Map and Set collections

Data Structures and Algorithms Lists

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Chapter 3: Restricted Structures Page 1

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

dictionary find definition word definition book index find relevant pages term list of page numbers

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

An Empirical Analysis of the Java Collections Framework Versus the C++ Standard Template Library

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Binary Heap Algorithms

Principles of Software Construction: Objects, Design and Concurrency. Java Collections. toad Fall 2013

Collections Classes for Real-Time and High-Performance Applications. July 4, 2005 Jean-Marie Dautelle

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Why you shouldn't use set (and what you should use instead) Matt Austern

DATA STRUCTURE - QUEUE

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

cs2010: algorithms and data structures

A Comparison of Dictionary Implementations

LINKED DATA STRUCTURES

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Introduction to Data Structures

7.1 Our Current Model

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

Stacks and queues. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

Computer. Course Description

Java Interview Questions and Answers

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

BCS2B02: OOP Concepts and Data Structures Using C++

Heaps & Priority Queues in the C++ STL 2-3 Trees

CSC 551: Web Programming. Fall 2001

Ordered Lists and Binary Trees

The Interface Concept

Android Application Development Course Program

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Data Structures and Data Manipulation

Java the UML Way: Integrating Object-Oriented Design and Programming

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

IMPROVING PERFORMANCE OF RANDOMIZED SIGNATURE SORT USING HASHING AND BITWISE OPERATORS

Algorithms and Data Structures Written Exam Proposed SOLUTION

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

16 Collection Classes

DATABASE DESIGN - 1DL400

Transcription:

Data Structures in the Java API

Vector From the java.util package. Vectors can resize themselves dynamically. Inserting elements into a Vector whose current size is less than its capacity is a relatively fast operation. Inserting an element into a full Vector is a slow operation since the Vector must be resized. When a Vector is resized, the default new size is twice the original size. A Vector can be trimmed to size but this leaves no room for inserts without resizing again.

Vector (cont d) Vectors store references to Objects. To store values of a primitive data type in a Vector, use the type-wrapper classes from the java.lang package. The Vector methods contains and indexof perform linear searches of a Vector s contents.

Stack The class Stack extends the class Vector. Additional methods provided: public boolean push(object obj); public Object pop(); public Object peek(); public boolean empty(); public int search(object obj); *NOTE: Top of stack is position 1! The user of Stack may perform operations on a stack that are not necessarily typical of a stack since Stack inherits methods from Vector. This may corrupt the stack if used incorrectly.

Queues The standard Java library does not have an explicit class for the queue abstraction. But: The class LinkedList does provide the ability to add and remove elements to or from either end of a collection. The standard Java library does not have an explicit class for the priority queue abstraction. But: The class TreeSet can be used as a priority queue.

Dictionary A Dictionary maps keys to values. Dictionary is an abstract class. Dictionary provides the public interface methods required to maintain a table of key-value pairs, where each key in the table is unique. Hashtable is a subclass of Dictionary.

Dictionary (cont d) Dictionary abstract methods: boolean isempty(); Object get(object key); Object put(object key, Object value); Object remove(object key);

Hashtable Hashtable is a subclass of Dictionary. Hashtable uses chaining to resolve collisions. Hashtable s default load factor is.75 When the number of entries in the table exceeds the product of the load factor and the current capacity (number of chains), the capacity is increased by calling the rehash method. Additional methods: public boolean containskey(object key); public boolean contains(object value);

Arrays Arrays provides static methods for array manipulation. (extends Object) public static int binarysearch (Object[] a, Object key); The array a must be sorted first before use of this method. If the array is not sorted, the results are undefined. public static void sort(object[] a); The sorting algorithm used is a tuned version of quick sort adapted from Bentley and McIlroy ( Engineering a Sort Function, 1993). The Arrays methods are heavily overloaded!

Collection Collection is the interface from which specific collections are derived. Collection contains bulk operations for adding, clearing, comparing and retaining objects in the collection. Collection provides a method that returns an Iterator to examine all elements in the collection. An Iterator can remove an element from a collection. Iterator specifies the methods hasnext, next and remove.

List List is the interface that defines an ordered collection that can contain duplicate elements. List is zero-based (the first element is at index 0). Interface List is implemented by the classes: - ArrayList (resizable array implementation) - LinkedList (linked-list implementation) - Vector LinkedList can be used to implement stacks, queues, and double-ended queues (dequeues).

LinkedList public void add(int index, Object obj); public void addfirst(object obj); public void addlast(object obj); public Object get(int index); public Object getfirst(); public Object getlast(); public boolean remove(object obj); public Object removefirst(); public Object removelast(); public int size(); STACKS QUEUES

Set Set is the interface that defines an unordered collection that cannot contain duplicate elements. Interface SortedSet extends Set and maintains its elements in sorted order. Interface Set is implemented by the classes: - HashSet (hash table implementation) - TreeSet (red-black tree implementation) Class TreeSet implements SortedSet. TreeSet can be used to implement priority queues (heaps) with log n time cost for basic operations(add, remove and contains).

TreeSet public boolean add(object obj); public boolean contains(object obj); public Object first(); public SortedSet headset(object obj); public boolean isempty(); public Iterator iterator(); public Object last(); public SortedSet tailset(object obj); public boolean remove(object obj); public SortedSet subset(object o1, Object o2); PRIORITY QUEUES

Map Map is the interface that defines a collection that associates keys to values and cannot contain duplicate keys. (one-to-one mapping) Interface SortedMap extends Map and maintains its keys in sorted order. Interface Map is implemented by the classes: - HashMap (hash table implementation) - TreeMap (red-black tree implementation) Class TreeMap implements SortedMap. Class HashMap is roughly equivalent to Hashtable except that it permits null as a key or value.

TreeMap public Object put(object key, Object value); public boolean containskey(object key); public Object firstkey(); public SortedMap headmap(object key); public Object lastkey(); public SortedMap tailmap(object key); public boolean remove(object key); public SortedMap submap(object key1, Object key2); public Collection values();

Collections Collections provides static methods for manipulation of collections. public static int binarysearch (List list, Object key); public static void sort(list list); The sort is guaranteed to be stable (equal elements will not be reordered as a result). The sort is a modified version of merge sort. public static void shuffle(list list); public static void reverse(list list); public static Object max(collection c); public static Object min(collection c);

Should I use the API? In general, use of the API will result in code that runs efficiently and uses memory wisely and make coding much faster assuming you know the API well. Some uses of the API will violate the standard definitions of data structures and may result in corrupt data if used incorrectly. For time-critical or data-critical applications, user-defined data structures are preferable.