111 © 2002, Cisco Systems, Inc. All rights reserved.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 The Collections API.
Advertisements

Java Programming: Advanced Topics 1 Collections and Utilities.
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.
Collections Chapter Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with.
Processing Data in Collections Chapter Object Wrappers Collections can only hold objects. Primitive types ( int, double, float, etc.) are not objects.
Map Collections and Custom Collection Classes Chapter 14.
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.
Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on Collections.
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.
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
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 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'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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities Chapter 4.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
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.
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.
Big Java Chapter 16.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Object Oriented Programming Ders 10: Data Structures Mustafa Emre İlal
Chapter 18 Java Collections Framework
Data structures Abstract data types Java classes for Data structures and ADTs.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
Data structures and algorithms in the collection framework 1.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
1 Interfaces in Java’s Collection Framework Rick Mercer.
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
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.
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,
University of Limerick1 Collections The Collection Framework.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
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
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
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.
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Java Collections CHAPTER 3-February-2003
Slides by Donald W. Smith
Chapter 19 Java Data Structures
Chapter 20 Lists, Stacks, Queues, and Priority Queues
14.1 The java.util Package.
CSE 1020: The Collection Framework
Introduction to Java Collection
Presentation transcript:

111 © 2002, Cisco Systems, Inc. All rights reserved.

2 Collections Chapter 14

333 © 2002, Cisco Systems, Inc. All rights reserved. Learning Objectives The java.util package Collections The collections framework

444 © 2002, Cisco Systems, Inc. All rights reserved. Overview The array has been explored as a structure to manage many objects. Arrays are limited due to a fixed size and ability to store one type of object. Many other collection classes offer more flexibility in managing multiple objects. These objects are dynamic, and elements of the collection can be manipulated with built-in methods.

555 © 2002, Cisco Systems, Inc. All rights reserved. Overview (continued) The four storage technologies are array, linked-list, tree, and hash table. Each of these has unique properties for storing and accessing collection elements The collections framework consists of many classes that implement these storage technologies. Choosing the correct collection is crucial to designing efficient programs.

666 © 2002, Cisco Systems, Inc. All rights reserved. Key Terms Collections framework Elements Collection Array LinkedList Doubly linked Tree Hash table Bag, multiset, sequence Ordered, unordered

777 © 2002, Cisco Systems, Inc. All rights reserved. Key Terms (continued) Set Map Dictionaries Collection interface ArrayList Vector Treeset Hashset Iterator ListIterator

888 © 2002, Cisco Systems, Inc. All rights reserved. Key Terms (continued) Enumeration Property System properties Key-value pairs Sorting Shuffling

999 © 2002, Cisco Systems, Inc. All rights reserved. The java.util Package The java.util package contains classes: To manage collections of objects To manage messaging between objects when changes occur in an object To manipulate date information This chapter focuses on the collection classes.

10 © 2002, Cisco Systems, Inc. All rights reserved. Collections A collection is a single object that represents a group of objects. Unlike arrays, collection objects cannot reference primitive types. Collection elements are object references. Collection objects can grow dynamically, sort objects, move through objects, and add or remove objects.

11 © 2002, Cisco Systems, Inc. All rights reserved. Collections (continued) Three important concepts relate to understanding collections: Storage technologies available to store and manage a collection of objects Properties of collections Types of collection objects

12 © 2002, Cisco Systems, Inc. All rights reserved. Storage Technologies The four major storage technologies available for collections are: Array Linked-list Tree Hash table

13 © 2002, Cisco Systems, Inc. All rights reserved. Array Arrays are used to store unique values. The array is most efficient for a fixed number of elements. Adding or removing array elements is considered an expensive operation.

14 © 2002, Cisco Systems, Inc. All rights reserved. Linked-List Items are stored in order. Duplicates are allowed. Each object has a link to the object before and after it.

15 © 2002, Cisco Systems, Inc. All rights reserved. Linked-List (continued) Linked lists are doubly linked and can be traversed forward and backward. Adding and removing elements only changes the links of the objects next to the elements.

16 © 2002, Cisco Systems, Inc. All rights reserved. Tree Elements in a tree structure are sorted in ascending order. An index is used to search for objects.

17 © 2002, Cisco Systems, Inc. All rights reserved. Hash Table Each item is assigned a key value. The item and key represent Key-value pairs. Accessing items is fast but memory intensive.

18 © 2002, Cisco Systems, Inc. All rights reserved. Properties of Storage Technologies

19 © 2002, Cisco Systems, Inc. All rights reserved. Collection The Collection interface represents a simple container. It includes many methods for adding, removing, and sorting elements. The interface has concrete implementations in lists, sets, and maps.

20 © 2002, Cisco Systems, Inc. All rights reserved. List Lists are ordered collections that allow duplicates Elements are indexed. Lists are also known as bags or multi-sets

21 © 2002, Cisco Systems, Inc. All rights reserved. Set An unordered collection of objects. Duplicates are allowed. The size is dynamic and allows objects to be added or removed.

22 © 2002, Cisco Systems, Inc. All rights reserved. Maps A collection of arbitrary associations between key-value pairs. Only one entry allowed for a given key. Elements searchable by key value only. The key value is used to generate a hash code, which determines the memory location.

23 © 2002, Cisco Systems, Inc. All rights reserved. The Collections Framework A set of classes and interfaces that enable programmers to manage larger numbers of objects as collections. Defines a set of behaviors for collection objects. Although there are many, this chapter focuses one implementation of each storage technology.

24 © 2002, Cisco Systems, Inc. All rights reserved. Collection Interfaces These interfaces represent abstract guidelines to be used in concrete implementations

25 © 2002, Cisco Systems, Inc. All rights reserved. Collection Interfaces (continued) Collection is used for any collection that is simple, unordered, and has duplicates. List is an ordered collection that is indexed and has duplicates. Sets are finite collections that do not allow duplicates. SortedSet is for sets with elements in ascending order.

26 © 2002, Cisco Systems, Inc. All rights reserved. Collection Interfaces (continued) Map provides basic methods for storing and retrieving key values. SortedMap is implemented to create a map with values in ascending order.

27 © 2002, Cisco Systems, Inc. All rights reserved. Helper Interfaces There are two helper interfaces that do not represent collections but are used with collection objects. Iterator is used to traverse the elements of a collection. It traverses forward only. ListIterator is used to traverse the elements of a list both forward and backward.

28 © 2002, Cisco Systems, Inc. All rights reserved. Set Objects HashSet implements the Set interface and uses hash table storage. Duplicates are not allowed in HashSet objects. TreeSet implements SortedSet and sorts elements in ascending order. Duplicates are not allowed in TreeSet objects.

29 © 2002, Cisco Systems, Inc. All rights reserved. Vector Vector implements the List interface. The Vector object can hold multiple types of objects. To cast these objects to an assignable type, the instanceof operator is used. Vectors are zero-indexed.

30 © 2002, Cisco Systems, Inc. All rights reserved. Additional List Objects ArrayList implements the List interface. ArrayList objects allow duplicates. LinkedList implements the List interface. Objects can be added and removed from LinkedList objects efficiently. An Iterator object is used to traverse the LinkedList object.

31 © 2002, Cisco Systems, Inc. All rights reserved. Map Objects The Map interface is not technically a collection. It uses keys to manage objects, which may be duplicated.

32 © 2002, Cisco Systems, Inc. All rights reserved. Hash Codes The key is integer generated from the hashCode() method. The hashCode method, inherited from Object, should be overridden to provide a hash code for each object. Objects with similar data should produce unique hash code values.

33 © 2002, Cisco Systems, Inc. All rights reserved. Iterator An Iterator is used to scan a collection object.

34 © 2002, Cisco Systems, Inc. All rights reserved. Iterator Methods Iterator methods can be called to manipulate elements of a collection. Elements can be added and removed by an Iterator.

35 © 2002, Cisco Systems, Inc. All rights reserved. Sorting and Shuffling The bubble sort construct was explored earlier in this curriculum. But, collection classes have built-in sorting functionality with the sort method. The reverse() method reverses the current order of the elements. The shuffle() method is used to place elements in random order.

36 © 2002, Cisco Systems, Inc. All rights reserved. Summary The collections framework in the java.util package contains several classes and interfaces to create collection objects. Collections are flexible and can manage several objects efficiently. There are several storage technologies available to create collections such as array, linked-list, tree, and hash table. The classes of the collections framework implement interfaces that determine the behavior of the collection.

37 © 2002, Cisco Systems, Inc. All rights reserved. Summary (continued) Collection objects impose no rules on order or content duplication. Lists maintain order. Sets reject duplicate elements. Maps use key-value pairs to store and access elements The Iterator and ListIterator interfaces are used to traverse collections and manipulate collection elements.

38 © 2002, Cisco Systems, Inc. All rights reserved.