Data structures and algorithms in the collection framework 1.

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 Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 21 Generics.
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.
© 2006 Pearson Addison-Wesley. All rights reserved16-1 Methods in the List Interface (Part 1 of 16)
Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on Collections.
Java Collection Framework. Interface Collection add(o) Add a new element clear() Remove all elements contains(o) Membership checking. IsEmpty() Whether.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
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.
Java Collections. Collections, Iterators, Algorithms CollectionsIteratorsAlgorithms.
Java's Collection Framework
Collections. Why collections? Collections are used to hold a collection of objects. List holds objects based on order of insertion and can hold non unique.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
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.
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.
(c) University of Washington14-1 CSC 143 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.
Data structures and algorithms in the collection framework 1 Part 2.
Big Java Chapter 16.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Object Oriented Programming Ders 10: Data Structures Mustafa Emre İlal
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.
Collections –data structures and Algorithms L. Grewe.
1.0tCopyright © 1998 Purple Technology, Inc. 1 Java Collections Framework Authored by Alex Chaffee Copyright © 1998 Purple Technology, Inc. All rights.
Arrays, ArrayLists, and Collections. Rationale Suppose we have a program to compute the average of three numbers. We could write a simple little method.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 6 Data Structures.
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
13 Collections Framework. 2 Contents What is Collection? Collections Framework Collections Hierarchy Collections Implementations Set List Map.
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.
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.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
AD Lecture #3 Java Collection Framework 1.
1 Copyright © 2011 Tata Consultancy Services Limited COLLECTIONS By TEAM 5 Rajendhiran Sivan Christi Yashwanth Bijay Smruthi Satyajit.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
4-Mar-16 Introduction to Collections. Revision questions True false questions 0 for False 1 for True Please do not answer anything other than the above.
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Java Collections Framework The client view. The Big Picture.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
©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.
Java Collections CHAPTER 3-February-2003
Chapter 19 Java Data Structures
University of Central Florida COP 3330 Object Oriented Programming
Road Map CS Concepts Data Structures Java Language Java Collections
Java Collections Overview
CSE 1020: The Collection Framework
CS 240 – Advanced Programming Concepts
Introduction to Java Collection
Presentation transcript:

Data structures and algorithms in the collection framework 1

2 Elements of the collection framework Interfaces: Abstract data types –Specifications Classes: Data types –Implementations Exceptions: A few exceptions Algorithms –Working on the abstract data types.

Data structures and algorithms in the collection framework 3 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 4 Object ordering SortedSet and SortedMap rely on object ordering. –The elements in SortedSet (keys in SortedMap) must implement the Comparable interface public interface Comparable { public int compareTo(Object o); } –Or, you must specify a Comparator when constructing the Sorted collection public interface Comparator { int compare(Object o1, Object o2); }

Data structures and algorithms in the collection framework 5 Some implementations List –LinkedList, ArrayList –Vector (old implementation) Set –HashSet, TreeSet (TreeSet is an OrderedSet) Queue –LinkedList, PriorityQueue Map –HashMap, TreeMap (TreeMap is an OrderedMap) –Hashtable (old implementation) The above shows only a few implementations. More (special purpose) implementations exist in Java 5.0.

Data structures and algorithms in the collection framework 6 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 7 List implementations LinkedList –Performance: Delete operation is fast O(1) if you are iterating through the List ArrayList –Performance: Good get(index)O(1) –Generally faster than LinkedList –Tuning parameter: Initial capacity of array. Set in constructor. Vector –Old implementation (before the collection framework was introduced in Java) –Has been retrofitted to suite the collection framework. –Synchronized

Data structures and algorithms in the collection framework 8 Set and Map implementations HashSet –Is fastest –Tuning parameter: Initial capacity Set in constructor TreeSet –Offers ordering HashMap –Fastest –Tuning parameter: Initial capacity Set in constructor TreeMap –Offers ordering by key

Data structures and algorithms in the collection framework 9 Queue implementations LinkedList –implements the Queue interface PriorityQueue –Orders elements according to the natural order (Comparable) or a specific Comparator. –Head of queue is the “smallest” element. –Internal data structure is a “heap”.

Data structures and algorithms in the collection framework 10 Iterator You want to access the individual data items in your collections –List: no problem get(int index) –Collection and Set has no order Get-by-index does not make sense –You access the data items using an iterator Collection set = new HashSet (); Iterator iterator = set.iterator(); while (iterator.hasNext()) { Integer i = iterator.next(); doSomething(i); }

Data structures and algorithms in the collection framework 11 Iterator pattern Iterator is an interface –You don’t need to know the name the implementing class, since you will never need “new SomeIterator()”. Design pattern: Iterator –Iterator is such a general phenomenon that is has been termed a “design pattern” –Collection creates an iterator (known only by interface) –The iterator fetches the individual objects from the collection. –You may use the iterator pattern with you own classes Idea: BorrowerCatalog creates a BorrowerIterator

Data structures and algorithms in the collection framework 12 For-each loop in Java 5.0 –List myList = new ArrayList (); –for (String str : myList) { doSomething(str); } –You probably don’t need to use iterators very often. –Works with any class implementing interface java.lang.Iterable. Your collection/catalog classes might implement Iterable. Works with arrays as well –int[] numbers = new int[10]; –for (int number : numbers) { doSomething(number); } For-each loop is just syntactic sugar –It adds nothing new to the Java language, it just makes things a little easier for the programmer (and program reader).

Data structures and algorithms in the collection framework 13 Iterating a map The general Map interface cannot create an iterator. If you want to iterate a map, you must get a set of keys, and then iterate the keys Set keys = map.keySet(); for (int key : keys) { System.out.println(key + ": " + map.get(key)); } Or use the entry set of the map For (Map.Entry entry : map.entrySet()) { System.out.println(“entry.getKey + “: “ + entry.getValue(); }