Fundamentals of Java Lesson 13: Linear Collections: Lists.

Post on 17-Jan-2016

245 views 0 download

Tags:

Transcript of Fundamentals of Java Lesson 13: Linear Collections: Lists.

Fundamentals of JavaLesson 13:

Linear Collections: Lists

Lesson 13: Linear Collections: Lists

Objectives:

– Distinguish fundamental categories of collections, such as linear, hierarchical, graph, and unordered.

– Understand the basic features of lists and their applications.

– Use the list interface and the major list implementation classes.

Lesson 13: Linear Collections: Lists

Objectives:

– Recognize the difference between index-based operations and content-based operations on lists.

– Use an iterator to perform position-based operations on a list.

– Understand the difference between an iterator and a list iterator.

– Describe the restrictions on the use of list operations and iterator operations.

Lesson 13: Linear Collections: Lists

Vocabulary:– abstract data type

(ADT)– abstraction– backing store– children– collection– content-based

operation– free list– graph collection– head

– hierarchical collection– index-based operation– iterator– linear collection– list iterator– object heap– parent– position-based operation– tail– unordered collection

13.1 Overview of Collections– A collection is a group of items that we want to treat as a

conceptual unit.– Arrays are the most common and fundamental type of

collection.– Other types of collections include:

• Strings• Stacks• Lists• Queues• Binary search trees• Heaps• Graphs• Maps• Sets• And bags

13.1 Overview of Collections– Collections can be:

• Homogeneous– All items in the collection must be of the same type

• Heterogeneous– The items can be of different types

– An important distinguishing characteristic of collections is the manner in which they are organized.

13.1 Overview of Collections

– There are four main categories of collections:

• Linear collections

• Hierarchical collections

• Graph collections

• And unordered collections

13.1 Overview of CollectionsLinear Collections

– The items in a linear collection are, like people in a line, ordered by position.

– Everyday examples of linear collections are grocery lists, stacks of dinner plates, and a line of customers waiting at a bank.

13.1 Overview of CollectionsHierarchical Collections

– Data items in hierarchical collections are ordered in a structure reminiscent of an upside down tree.

– Each data item except the one at the top has just one predecessor, its parent, but potentially many successors, called its children.

13.1 Overview of Collections– As shown in Figure 13-2, D3’s predecessor

(parent) is D1, and its successors (children) are D4, D5, and D6.

– A company’s organization tree and a book’s table of contents are examples of hierarchical collections.

13.1 Overview of CollectionsGraph Collections

– A graph collection, also called a graph, is a collection in which each data item can have many predecessors and many successors.

– As shown in Figure 13-3, all elements connected to D3 are considered to be both its predecessors and successors.

– Examples of graphs are maps

of airline routes between

cities and electrical wiring

diagrams for buildings.

13.1 Overview of CollectionsUnordered Collections

– Items in an unordered collection are not in any particular order, and one cannot meaningfully speak of an item’s predecessor or successor.

– Figure 13-4 shows such

a structure.

– A bag of marbles is an

example of an

unordered collection.

13.1 Overview of CollectionsOperations on Collections

– Collections are typically dynamic rather than static, meaning they can grow or shrink with the needs of a problem.

– Their contents change throughout the course of a program.

– Manipulations that can be performed on a collection vary with the type of collection being used.

– Table 13-1 shows the categories of operations on collections.

13.1 Overview of Collections

13.1 Overview of CollectionsAbstract Data Types

– Users of collections need to know how to declare and use each type of collection.

– From their perspective, a collection is seen as a means for storing and accessing data in some predetermined manner, without concern for the details of the collection’s implementation.

– From the user’s perspective a collection is an abstraction, and for this reason, in computer science collections are called abstract data types (ADTs).

13.1 Overview of Collections– Abstraction is used as a technique for ignoring or

hiding details that are, for the moment, inessential.– We often build up a software system layer by layer,

each layer being treated as an abstraction by the layers above that utilize it.

– Without abstraction, we would need to consider all aspects of a software system simultaneously, an impossible task.

– The details must be considered eventually, but in a small and manageable context.

– In Java, methods are the smallest unit of abstraction, classes and their interfaces are the next in size, and packages, are the largest.

13.1 Overview of Collections

Multiple Implementations

– The rationale for implementing different types of collections tend to be based on four underlying approaches to organizing and accessing memory:

1. Arrays

2. Linear linked structures

3. Other linked structures

4. Hashing into an array

13.1 Overview of Collections– When there is more than one way to implement

a collection, programmers define a variable of an interface type and assign to it an object that uses the desired implementation.

– For example, java.util includes three classes that implement the List interface.

– Theses classes are LinkedList, ArrayList, and Vector.

– In the following code snippet, the programmer has chosen the LinkedList implementation:

List lst = new LinkedList():

13.1 Overview of CollectionsCollections and Casting

– With the exception of arrays, strings, string buffers, and bit sets, Java collections can contain any kind of object and nothing but objects.

– These constraints have several consequences:• Primitive types, such as int must be placed in a wrapper

before being added to a collection.

• It is impossible to declare a collection that is restricted to just one type of object.

• Items coming out of a collection are, from the compiler’s perspective, instances of Object, and they cannot be sent type-specific messages until they have been cast.

13.2 Lists

Overview

– A list supports manipulation of items at any point within a linear collection. Some common examples of lists include:

• A recipe, which is a list of instruction.

• A String, which is a list of characters.

• A document, which is a list of words.

• A file, which is a list of data blocks on a disk.

13.2 Lists– Array implementations of lists use physical position to

represent logical order, but linked implementations do not.

– The first item in a list is at its head, whereas the last item in a list is at its tail.

– In a list, items retain position relative to each other over time, and additions and deletions affect predecessor/successor relationships only at the point of modification.

– Figure 13-5 shows how a list changes in response to a succession of operations.

– The operations are described in Table 13-2.

13.2 Lists

13.2 Lists

13.2 Lists

Categories of List Operations

– There are several broad categories of operations:

• Index-based operations

• Content-based operations

• And position-based operations.

13.2 Lists– Index-based operations manipulate items at

designated positions within a list.

• The head is at index 0

• The tail is at index n-1

– Table 13-3 shows some fundamental index-based operations.

13.2 Lists– Content-based operations are based not on an index

but on the content of a list.

– Most of these operations search for an object equal to a given object before taking further action.

– Table 13-4 outlines some basic content-based operations.

13.2 Lists

– Position-based operations are performed relative to a currently established position within a list, and in java.util, they are provided via an iterator.

– An iterator is an object that allows a client to navigate through a list and perform various operations at the current position.

13.2 Lists

The List Interface

– Table 13-5 lists the methods in the interface java.util.List.

– The classes ArrayList and LinkedList both implement this interface.

13.2 Lists

13.2 Lists

13.2 Lists

13.2 Lists

13.2 Lists

– To make certain that there are no ambiguities in the meaning of the operations shown in Table 13-5, we now present an example that shows a sequence of these operations in action.

– This sequence is listed in Table 13-6.

13.2 Lists

13.2 Lists

13.2 Lists

Applications of Lists

Heap Storage Management

– The object heap can be managed using a inked list.

– Heap management schemes can have a significant impact on an application’s overall performance, especially if the application creates and abandons many objects during the course of its execution.

13.2 Lists

– In our scheme, contiguous blocks of free space on the heap are linked together in a free list.

– When an application instantiates a new object, the JVM searches the free list for the first block large enough to hold the object and returns any excess space to the free list.

– When the object is no longer needed, the garbage collector returns the object’s space to the free list.

13.2 Lists– To counteract fragmentation, the garbage collector

periodically reorganizes the free list by recognizing physically adjacent blocks.

– To reduce search time, multiple free lists can be used.

– If an object reference requires 4 bytes, then:

• list 1 could consist of blocks of size 4,

• list 2, blocks of size 8,

• list 3, blocks of size 16,

• list 4, blocks of size 32,

• and so on.

13.2 Lists– In this scheme, space is always allocated in

units of 4 bytes, and space for a new object is taken from the head of the first nonempty list containing blocks of sufficient size.

– Allocating space for a new object now takes O(1) time unless the object requires more space than is available in the first block of the last list.

– At that point, the last list must be searched, giving the operation a maximum running time of O(n), where n is the size of the last list.

13.2 Lists

Organization of Files on a Disk

– A computer’s file system has three major components:

• A directory of files,

• The files themselves,

• And free space.

13.2 Lists– Figure 13-6 shows the standard arrangement of

a disk’s physical format.

– The disk’s surface is divided into concentric tracks, and each track is further subdivided into sectors.

– The numbers of these vary depending on the disk’s capacity and physical size; however all tracks contain the same number of sectors and all sectors contain the same number of bytes.

13.2 Lists

13.2 Lists– Sectors that are not in use are linked together in

a free list.

– When new files are created, they are allocated space from this list, and when old files are deleted, their space is returned to the list.

– Because all sectors are the same size and because space is allocated in sectors, a file system does not experience the same fragmentation problem encountered in Java’s object heap.

13.3 Iterators– An iterator supports position-based operations

on a list.

– An iterator uses the underlying list as a backing collection, and the iterator’s current position is always in one of three places:

• Just before the first item

• Between two adjacent items

• Just after the last item

13. Iterators– Initially when an iterator is first instantiated,

the position is immediately before the first item.

– From this position the user can either navigate to another position or modify the list in some way.

– For lists, java.util provides two types of iterators:

• a simple iterator

• and a list iterator.

13.3 IteratorsThe Simple Iterator

– In Java, a simple iterator is an object that responds to messages specified in the interface java.util.Iterator, as summarized in Table 13-9:

13.3 Iterators– You create an iterator object by sending the message

iterator to a list, as shown in the following code segment:

– The list serves as a backing store for the iterator.

– A backing store is a storage area, usually a collection, where data elements are maintained.

– The relationship between these two objects is depicted in Figure 13-8.

– The methods hasNext and next are for navigating, whereas the method remove mutates the underlying list.

Iterator iter = someList.iterator():

13.3 Iterators

13.3 Iterators– The next code segment demonstrates a traversal of a

list to display its contents using an iterator:

– Note that you should avoid doing the following things:

• Sending a mutator message, such as add, to the backing list when an interator is active.

• Sending the message remove to an iterator when another iterator is open on the same backing list.

While (iter.hasNext()) System.out.println(iter.next());

13.3 Iterators– A list iterator responds to messages specified in the

interface java.util.ListIterator.– This interface extends the Iterator interface with several

methods for navigation and modifying the backing list.

– Table 13-10 shows the additional navigational methods:

13.3 Iterators

– The additional mutator operations work at the currently established position in the list (see Table 13-11.)

13.3 IteratorsUsing a List Iterator

– In Table 13-12, we present a sequence of list iterator operations and indicate the state of the underlying list after each operation.

– Remember that a list iterator’s current position is located:

• before the first item,

• after the last item,

• or between two items.

– In the table, the current position is indicated by a comma and by an integer variable called current position.

13.3 Iterators

13.3 Iterators

13.3 Iterators

13.3 Iterators

13.3 Iterators