The Java Collections Framework (JCF) & Iterators Lecture 11 - - PowerPoint PPT Presentation

the java collections framework jcf iterators
SMART_READER_LITE
LIVE PREVIEW

The Java Collections Framework (JCF) & Iterators Lecture 11 - - PowerPoint PPT Presentation

Wentworth Institute of Technology COMP1050 Computer Science II | Spring 2017 | Derbinsky The Java Collections Framework (JCF) & Iterators Lecture 11 JCF & Iterators March 24, 2017 1 Wentworth Institute of Technology COMP1050


slide-1
SLIDE 1

Wentworth Institute of Technology COMP1050 – Computer Science II | Spring 2017 | Derbinsky

The Java Collections Framework (JCF) & Iterators

Lecture 11

March 24, 2017 JCF & Iterators 1

slide-2
SLIDE 2

Wentworth Institute of Technology COMP1050 – Computer Science II | Spring 2017 | Derbinsky

What is a Data Structure?

  • A data structure is a collection of data
  • rganized in some fashion
  • The structure not only stores data but also

supports operations for accessing/manipulating the data

  • Java provides several data structures that

can be used to organize/manipulate data efficiently in the Java Collections Framework

March 24, 2017 JCF & Iterators 2

slide-3
SLIDE 3

Wentworth Institute of Technology COMP1050 – Computer Science II | Spring 2017 | Derbinsky

Java Collections Framework (JCF)

  • The Java Collections Framework supports

two types of containers:

– Storing a collection of elements (collection) – Storing key/value pairs (map)

March 24, 2017 JCF & Iterators 3

slide-4
SLIDE 4

Wentworth Institute of Technology COMP1050 – Computer Science II | Spring 2017 | Derbinsky

JCF Hierarchy (1)

March 24, 2017 JCF & Iterators 4

Why this distinction?

slide-5
SLIDE 5

Wentworth Institute of Technology COMP1050 – Computer Science II | Spring 2017 | Derbinsky

JCF Hierarchy (2)

March 24, 2017 JCF & Iterators 5

slide-6
SLIDE 6

Wentworth Institute of Technology COMP1050 – Computer Science II | Spring 2017 | Derbinsky

Collections

  • The Collection interface is the root

interface for manipulating a collection of

  • bjects

– Defines the common operations for lists, vectors, stacks, queues, priority queues, and sets

  • The AbstractCollection class implements

all the methods in Collection interface, except add, size, and iterator

– Implemented in appropriate concrete subclasses

March 24, 2017 JCF & Iterators 6

slide-7
SLIDE 7

Wentworth Institute of Technology COMP1050 – Computer Science II | Spring 2017 | Derbinsky

The Collection Interface

March 24, 2017 JCF & Iterators 7

slide-8
SLIDE 8

Wentworth Institute of Technology COMP1050 – Computer Science II | Spring 2017 | Derbinsky

Iterators

  • As seen in the previous diagram, a common concept

in collections is to iterate (or inspect one-by-one) over all the elements in the collection

  • Each collection (Set, List, Stack, Queue, etc.)

implements the Iterable interface, thereby allowing a way for users to get an Iterator (an object used to iterate)

– The next() method of an iterator gets the next element

  • Iterator is a classic design pattern for walking through

a data structure without having to expose the details of how data is stored in the data structure

March 24, 2017 JCF & Iterators 8

slide-9
SLIDE 9

Wentworth Institute of Technology COMP1050 – Computer Science II | Spring 2017 | Derbinsky

Example

final Collection<String> c = new ArrayList<>(); c.add("New York"); c.add("Atlanta"); c.add("Dallas"); c.add("Madison"); final Iterator<String> i = c.iterator(); while(i.hasNext()) { System.out.printf("%s ", i.next().toUpperCase()); } System.out.printf("- done!%n");

March 24, 2017 JCF & Iterators 9

slide-10
SLIDE 10

Wentworth Institute of Technology COMP1050 – Computer Science II | Spring 2017 | Derbinsky

The foreach Loop

  • Any type that implements Iterable can be

iterated over via the foreach loop:

for (String s : c) { System.out.printf("%s ", s.toUpperCase()); }

March 24, 2017 JCF & Iterators 10

slide-11
SLIDE 11

Wentworth Institute of Technology COMP1050 – Computer Science II | Spring 2017 | Derbinsky

Exercise

  • Use ArrayList to store following items

and assign it to the variable myCollection of type Collection

– Pineapple, Banana, Orange, Apple, Watermelon

  • Use an Iterator to traverse all items in

myCollection and print out items that contain “an”

March 24, 2017 JCF & Iterators 11

slide-12
SLIDE 12

Wentworth Institute of Technology COMP1050 – Computer Science II | Spring 2017 | Derbinsky

Solution

final Collection<String> myCollection = new ArrayList<>(); myCollection.add("Pineapple"); myCollection.add("Banana"); myCollection.add("Orange"); myCollection.add("Apple"); myCollection.add("Watermelon"); final Iterator<String> myIterator = myCollection.iterator(); while(myIterator.hasNext()) { final String str = myIterator.next(); if (str.toLowerCase().contains("an")) { System.out.printf("%s%n", str); } }

March 24, 2017 JCF & Iterators 12

slide-13
SLIDE 13

Wentworth Institute of Technology COMP1050 – Computer Science II | Spring 2017 | Derbinsky

Take Home Points

  • A data structure is a collection of data organized to

support efficient operations that access/modify the data

  • The Java Collections Framework provides a set of

data structures for you to use in your programs

– Collections contain elements – Maps contain key-value pairs

  • Iterators are a common pattern by which to access

elements of data structures, independent of how they are implemented

March 24, 2017 JCF & Iterators 13