JCF Collection classes and interfaces

Slides:



Advertisements
Similar presentations
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Advertisements

CSC 205 – Java Programming II Lecture 25 March 8, 2002.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
15-Jun-15 Lists in Java Part of the Collections Framework.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
COMP 103 Iterators and Iterable. RECAP  Maps and Queues TODAY  Queue Methods  Iterator and Iterable 2 RECAP-TODAY.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
1 ArrayList  Array’s are limited because we need to know the size before we use them.  An ArrayList is an extension of an array that grows and shrinks.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
The Java Collections Framework (JCF) Introduction and review 1.
Information and Computer Sciences University of Hawaii, Manoa
Computer Science 209 Software Development Java Collections.
LinkedList Many slides from Horstmann modified by Dr V.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Information and Computer Sciences University of Hawaii, Manoa
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
List Interface and Linked List Mrs. Furman March 25, 2010.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
1 Copyright © 2011 Tata Consultancy Services Limited COLLECTIONS By TEAM 5 Rajendhiran Sivan Christi Yashwanth Bijay Smruthi Satyajit.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
Object Oriented Programming in Java Habib Rostami Lecture 7.
CS-2851 Dr. Mark L. Hornick 1 Stacks and Queues Behavior vs. Structure.
CS-2851 Dr. Mark L. Hornick 1 Linked-List collections Structure and Implementation.
CS-2852 Data Structures Week 4, Class 1 - Review Review! Thursday Exam I - Review Implementing ArrayList Big-O Iterators – High-level description Linked.
1 CS162: Introduction to Computer Science II Abstract Data Types.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs)
CSE 373 Implementing a Stack/Queue as a Linked List
The List ADT Reading: Textbook Sections 3.1 – 3.5
Announcements & Review
Software Development Inheritance and Composition
COMP 121 Week 9: ArrayList.
COP 3503 FALL 2012 Shayan Javed Lecture 8
Programming in Java Lecture 11: ArrayList
The List ADT Reading: Textbook Sections 3.1 – 3.5
(Java Collections Framework) AbstractSequentialList
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
EE 422C Sets.
Arrays and Collections
Building Java Programs
Collections Framework
Interator and Iterable
slides created by Marty Stepp
CSE 143 Lecture 25 Advanced collection classes
slides created by Alyssa Harding
Iterators Dan Fleck.
TCSS 143, Autumn 2004 Lecture Notes
Part of the Collections Framework
CSE 143 Lecture 21 Advanced List Implementation
Java Generics & Iterators
Presentation transcript:

JCF Collection classes and interfaces Iterators CS-2851 Dr. Mark L. Hornick

The Java Collections Framework… Is implemented as a series of hierarchies with: interfaces at the top abstract classes in the middle and concrete (instantiable) classes at the bottom CS-2851 Dr. Mark L. Hornick

A small part of the hierarchy: CS-2851 Dr. Mark L. Hornick

The Collection interface declares methods for inserting, removing and searching for an element in a collection All Collections support the following: add(E element) remove(int index) contains(E element) ... CS-2851 Dr. Mark L. Hornick

The List interface extends the Collection interface by supplying some index-oriented methods List-specific behavior that, for example, doesn’t make sense for all Collections add(int index, E element) get(int index) set(int index) ... Some data structures, like queues, are Collections but not Lists – meaning their elements cannot be accessed by index. CS-2851 Dr. Mark L. Hornick

The Collection interface extends the Iterable interface The Iterable interface declares a single method: iterator(), which returns an Iterator object An Iterator is an object that allows a user to loop through a Collection without accessing the elements directly CS-2851 Dr. Mark L. Hornick

Every JCF class that implements Collection can be iterated Associated with each class that implements the Collection interface, there is a private inner class that implements the Iterator interface CS-2851 Dr. Mark L. Hornick

Accessing and using a collection’s Iterator To get an Iterator from a Collection, use the iterator() method ArrayList<Double> al= new ArrayList<Double)(); // Get an Iterator object to iterate over // this collection. Iterator<Double> itr = al.iterator(); while( itr.hasNext() ) { // while elements exist… double x = itr.next(); // …get the next element System.out.println( x ); } CS-2851 Dr. Mark L. Hornick

The Iterator<E> methods public interface Iterator<E> { // Returns true if this Iterator object is positioned // at an element in the collection. public boolean hasNext(); // Returns the element this Iterator object is // positioned at, and advances this Iterator object. public E next(); // Removes the element returned by the most // recent call to next(). public void remove(); } // interface Iterator CS-2851 Dr. Mark L. Hornick

The for-each loop uses a collection’s Iterator automatically // create a collection List<Double> al = new ArrayList<Double>(); // use the built-in Iterator… for( Double x : al ) // “for each x in al” System.out.println(x); CS-2851 Dr. Mark L. Hornick