70
Collections Using Generics in Collections Examining LISTS

Collections

  • Upload
    laken

  • View
    128

  • Download
    2

Embed Size (px)

DESCRIPTION

Collections. Using Generics in Collections Examining LISTS. Chapter Objectives. Define the concept and terminology related to collections Explore the basic structure of the Java Collections API Discuss the abstract design of collections Define a set collection - PowerPoint PPT Presentation

Citation preview

Page 1: Collections

Collections

Using Generics in Collections

Examining LISTS

Page 2: Collections

2

Chapter Objectives

Define the concept and terminology related to collections

Explore the basic structure of the Java Collections API

Discuss the abstract design of collections

Define a set collection

Use a set collection to solve a problem

Examine an array implementation of a set

Define and examine concepts of a LIST

Page 3: Collections

3

Collections A collection is an object that gathers and organizes other

objects (elements)

Many types of fundamental collections have been defined: stack, queue, list, tree, graph, etc.

They can be broadly categorized as linear (organizes the elements in a straight line) or nonlinear

Page 4: Collections

4

FIGURE 3.1 A linear and a nonlinear collection

Page 5: Collections

5

Collections The elements within a collection are usually organized

based on:

the order in which they were added to a collection, or some inherent relationship among the elements themselves

For example, a list of people may be kept in alphabetical order by name or in the order in which they were added to the list

Which type of collection you use depends on what you are trying to accomplish

Page 6: Collections

6

Abstraction

An abstraction hides certain details at certain times

It provides a way to deal with the complexity of a large system

A collection, like any well-defined object, is an abstraction

We want to separate the interface of the collection (how we interact with it) from the underlying details of how we choose to implement it

Page 7: Collections

7

FIGURE 3.2 A well-defined interface masks the implementation of the collection.

Page 8: Collections

8

Issues with Collections For each collection we examine, we will consider:

How does the collection operate conceptually?

How do we formally define its interface?

What kinds of problems does it help us solve?

What ways might we implement it?

What are the benefits and costs of each implementation?

Page 9: Collections

9

Terms Various terms are used in the study of collections – defined in various

ways Recall our definitions:

data type – a group of values and the operations defined on those values

abstract data type – a data type whose values and operations are not inherently defined in a programming language -

data structure – the programming constructs used to implement a collection

Page 10: Collections

10

The Java Collections API The Java Collections API is a set of classes that represent some specific

collection types, implemented in various ways

It is part of the large class library that can be used by any Java program

API stands for Application Programming Interface

As we explore various collections, we will examine the appropriate classes in the Java Collections API

Page 11: Collections

11

A Set Collection Let's look at an example of a collection

A set collection groups elements without regard to their relationship to each other

It's as if you threw them all in a box

You can reach into a box and pull out an element, and are equally likely to get any one

It is a nonlinear collection, but could be implemented with a linear data structure

Page 12: Collections

12

The conceptual view of a set collection

Page 13: Collections

13

Collection Operations Every collection has a set of operations that define how we

interact with it They usually include ways for the user to:

add and remove elements determine if the collection is empty determine the collection's size

They also may include:

iterators, to process each element in the collection operations that interact with other collections

Page 14: Collections

14

FIGURE 3.4 The operations on a set collection

Page 15: Collections

15

Java Interfaces The programming construct in Java called an interface is a convenient

way to define the operations on a collection

A Java interface lists the set of abstract methods (no bodies) that a class implements

It provides a way to establish a formal declaration that a class will respond to a particular set of messages (method calls)

Page 16: Collections

GENERIC TYPES

Generic Data Type A type for which the operations are defined but the types of the items being manipulated are not.

16

Page 17: Collections

17

Listing 3.1

Page 18: Collections

18

Listing 3.1 (cont.)

Page 19: Collections

19

FIGURE 3.5 UML description of the SetADT<T> interface

Page 20: Collections

20

Iterators An iterator is an object that allows the user to acquire and

use each element in a collection in turn

The program design determines:

the order in which the elements are delivered the way the iterator is implemented

In the case of a set, there is no particular order to the elements, so the iterator order will be arbitrary (random)

Page 21: Collections

21

Iterators Collections that support iterators often have a method

called iterator that returns an Iterator object

Iterator is actually an interface defined in the Java standard class library

Iterator methods:

hasNext – returns true if there are more elements in the iteration

next – returns the next element in the iteration

Page 22: Collections

22

Set Exceptions Collections must always manage problem situations

carefully

For example: attempting to remove an element from an empty set

The designer of the collection determines how it might be handled

Page 23: Collections

23

Implementing a Set We use a set collection without any regard to how that

collection was implemented

We use the set collection for its functionality – how it is implemented is fundamentally irrelevant

It could be implemented in various ways

Let's examine how we can use an array to implement it

Page 24: Collections

24

An array of object references

Page 25: Collections

25

Managing Capacity An array has a particular number of cells when it is created

– its capacity So the array's capacity is also the set’s capacity

What do we do when the set is full and a new element is added?

We could throw an exception

We could return some kind of status indicator

We could automatically expand the capacity- This is the best solution

Page 26: Collections

26

The ArraySet Class

In the Java Collections API, class names indicate both the underlying data structure and the collection

Thus, the ArraySet class represents an array implementation of a set collection

Set elements are kept contiguously at one end of the array

An integer (count) represents: the number of elements in the set the next empty index in the array

Page 27: Collections

27

FIGURE 3.9 An array implementation of a set

Page 28: Collections

28

ArraySet - Constructors

Page 29: Collections

29

ArraySet - size and isEmpty

Page 30: Collections

30

ArraySet - add

Page 31: Collections

31

ArraySet - expandCapacity

Page 32: Collections

32

ArraySet - addAll

Page 33: Collections

33

ArraySet - removeRandom

Page 34: Collections

34

ArraySet - remove

Page 35: Collections

35

ArraySet - union

Page 36: Collections

36

ArraySet - contains

Page 37: Collections

37

ArraySet - equals

Page 38: Collections

38

ArraySet - equals (continued)

Page 39: Collections

39

ArraySet - iterator

Page 40: Collections

40

Listing 3.4

Page 41: Collections

41

Listing 3.4 (cont.)

Page 42: Collections

42

Listing 3.4 (cont.)

Page 43: Collections

43

FIGURE 3.10 UML description of the bingo system

Page 44: Collections

44

Analysis of ArraySet If the array is not full, adding an element to the set is O(1)

Expanding the capacity is O(n)

Removing a particular element, because it must be found, is O(n)

Removing a random element is O(1)

Adding all elements of another set is O(n)

The union of two sets is O(n+m), where m is the size of the second set

Page 45: Collections

Chapter 8 LISTS

Using Arrays with LISTS -

Page 46: Collections

46

Chapter 8 Objectives Examine list processing and various ordering techniques

Define a list abstract data type

Demonstrate how a list can be used to solve problems

Examine various list implementations

Compare list implementations

Page 47: Collections

47

Lists Lists are linear collections that are noted for their

flexibility

Adding and removing elements in lists are not restricted by the collection structure

That is, they don't have to operate on one end or the other

We will examine three types of list collections: ordered lists unordered lists indexed lists

Page 48: Collections

48

Ordered Lists The elements in an ordered list are ordered by some

inherent characteristic of the elements

names in alphabetical order

scores in ascending order

Therefore, the elements themselves determine where they are stored in the list

Page 49: Collections

49

A conceptual view of an ordered list

Page 50: Collections

50

Unordered Lists

There is an order to the elements in an unordered list, but that order is not based on element characteristics

The user of the list determines the order of the elements

A new element can be put on the front or the rear of the list, or it can be inserted after a particular element already in the list

Page 51: Collections

51

A conceptual view of an unordered list

Page 52: Collections

52

Indexed Lists In an indexed list, elements are referenced by their numeric

position in the list

Like an unordered list, there is no inherent relationship among the elements

The user can determine the order

Every time the list changes, the indexes are updated

Page 53: Collections

53

A conceptual view of an indexed list

Page 54: Collections

54

List Operations There are several operations common to all three list types

These include removing elements in various ways and checking the status of the list

The key differences between the list types involve the way elements are added

Page 55: Collections

55

The common operations on a list

Page 56: Collections

56

The operation particular to an ordered list

Page 57: Collections

57

FIGURE 8.6 The operations particular to an unordered list

Page 58: Collections

58

List Operations As with other collections, we use Java interfaces to define

the collection operations

Recall that interfaces can be defined via inheritance (derived from other interfaces)

We define the common list operations in one interface, then derive three others from it that define the interfaces of the three list types

Page 59: Collections

59

Listing 8.1

Page 60: Collections

60

Listing 8.1 (cont.)

Page 61: Collections

61

Listing 8.2

Page 62: Collections

62

Listing 8.3

Page 63: Collections

63

Listing 8.4 (cont.)

Page 64: Collections

64

Implementing Lists with Arrays

An array implementation of a list could follow strategies similar to those used for a queue – which we will look at later

We could fix one end of the list at index 0 and shift as needed when an element is added or removed

Or we could use a circular array to avoid the shift at one end

However, there is no avoiding a shift when an element in the middle is added or removed

Let's examine the fixed version

Page 65: Collections

65

FIGURE 8.13 An array implementation of a list

Page 66: Collections

66

ArrayList - the remove Operation

Page 67: Collections

67

ArrayList - the find Operation

Page 68: Collections

68

ArrayList - the contains Operation

Page 69: Collections

69

ArrayOrderedList - the add Operation

Page 70: Collections

70

Analysis of List Implementations

In both array and linked implementations, many operations are similar in efficiency

Most are O(1), except when shifting or searching need to occur, in which case they are order O(n)

In particular situations, the frequency of the need for particular operations may guide the use of one approach over another