Java Collections Framework - Object-Oriented Programming and Data Structures - Lecture Sl, Lecture notes of Object Oriented Programming

Major points from this lecture are: Java Collections Framework, Generic Types, Type Casting, Autoboxing, Generic Types, Generics, Programming With Generic Types, Wildcards, Bounded, Generic Methods . Object-Oriented Programming and Data Structures course includes program structure and organization, object oriented programming, graphical user interfaces, algorithm analysis, recursion, data structures (lists, trees, stacks, queues, heaps, search trees, hash tables, graphs), simple graph algorithms

Typology: Lecture notes

2012/2013

Uploaded on 08/20/2013

yumni
yumni 🇮🇳

5

(2)

25 documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
GenericTypesandthe
JavaCollectionsFramework
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download Java Collections Framework - Object-Oriented Programming and Data Structures - Lecture Sl and more Lecture notes Object Oriented Programming in PDF only on Docsity!

Generic

Types

and

the

Java

Collections

Framework

Generic

Types

in

Java

-^

When

using

a

collection

(e.g.,

LinkedList,

HashSet,

HashMap

we

generally

have

a

single

type

T

of

elements

that

we

store

in

it

(e.g.,

Integer,

String

-^

Before

Java

when

extracting

an

element,

had

to

cast

it

to

T

before

we

could

invoke

T's

methods

-^

Compiler

could

not

check

that

the

cast

was

correct

at

compile

‐time,

since

it

didn't

know

what

T

was

-^

Inconvenient

and

unsafe,

could

fail

at

runtime

^

Generics in Java provide away to communicate T, thetype of elements in acollection, to the compiler

^

Compiler can check that you haveused the collection consistently

^

Result: safer and more-efficientcode

Another

Example

Map

grades

new

HashMap();

grades.put("John",

new

Integer(67));

grades.put("Jane",

new

Integer(88));

grades.put("Fred",

new

Integer(72));

Integer

x

(Integer)grades.get("John");

sum

=^

sum

x.intValue();

Map<String,

Integer> grades

new

HashMap<String,

Integer>();

grades.put("John",

new

Integer(67));

grades.put("Jane",

new

Integer(88));

grades.put("Fred",

new

Integer(72));

Integer

x^

grades.get("John");

sum

sum

x.intValue();

old new

Type

Casting

In

effect,

Java

inserts

the

correct

cast

automatically,

based

on

the

declared

type

In

this

example,

grades.get("John")

is

automatically

cast

to

Integer

Map<String,

Integer> grades

=^

new

HashMap<String,

Integer>();

grades.put("John",

new

Integer(67));

grades.put("Jane",

new

Integer(88));

grades.put("Fred",

new

Integer(72));

Integer

x

grades.get("John");

sum

=^

sum

x.intValue();

Using

Generic

Types

is

read,

“of

T”

For

example:

Stack

is

read,

“Stack

of

Integer”

The

type

annotation

informs

the

compiler

that

all

extractions

from

this

collection

should

be

automatically

cast

to

T

Specify

type

in

declaration,

can

be

checked

at

compile

time

Can

eliminate

explicit

casts

Advantage

of

Generics

Declaring

Collection c

tells

us

something

about

the

variable

c

(i.e.,

c

holds

only

Strings)

This

is

true

wherever

c

is

used

The

compiler

checks

this

and

won’t

compile

code

that

violates

this

Without

use

of

generic

types,

explicit

casting

must

be

used

A

cast

tells

us

something

the

programmer

thinks

is

true

at

a

single

point

in

the

code

The

Java

virtual

machine

checks

whether

the

programmer

is

right

only

at

runtime

Programming

with

Generic

Types

-^

To

use

the

interface

List

,^ supply

an

actual

type

argument,

e.g.,

List

-^

All

occurrences

of

the

formal

type

parameter

(

E

in

this

case)

are

replaced

by

the

actual

type

argument

(

Integer

in

this

case)

public

interface

List

{^
E

is

a

type

variable

void

add(E

x);

Iterator

iterator();

} public

interface

Iterator

E

next(); boolean

hasNext();

void

remove();

Wildcards

void

printCollection(Collection

c)

Iterator

i

c.iterator();

while

(i.hasNext())

System.out.println(i.next()); } } void

printCollection(Collection

c)

for

(Object

e^

:^

c)

System.out.println(e); } }

old Wildcard

void

printCollection(Collection<?>

c)

for

(Object

e

c)

System.out.println(e); } }

bad

Generic

Methods

Adding

all

elements

of

an

array

to

a

Collection

See

the

online

Java

Tutorial

for

more

information

on

generic

types

and

generic

methods

static

void

a2c(Object[] a,

Collection<?>

c)

for

(Object

o

:^

a)

c.add(o);

compile

time

error

} public

class

myClass

{^

static

void

a2c(T[]

a,

Collection

c)

for

(T

o

a)

c.add(o);

ok

bad good

Generic

Classes

public class Queue

extends

AbstractBag {

private java.util.LinkedList queue

= new

java.util.LinkedList();

public void insert(T item) {

queue.add(item); } public T extract()

throws java.util.NoSuchElementException {

return queue.remove(); } public void clear() {

queue.clear(); } public int size() {

return queue.size(); } }

Java

Collections

Framework

Collections:

holders

that

let

you

store

and

organize

objects

in

useful

ways

for

efficient

access

The

package

java.util

includes

interfaces

and

classes

for

a

general

collection

framework

Goal: conciseness

A few concepts that arebroadly useful

Not an exhaustive set ofuseful concepts

The collectionsframework provides

Interfaces (i.e., ADTs)

Implementations

JCF

Interfaces

and

Classes

Interfaces

Collection

Set (no duplicates)

SortedSet

List (duplicates OK)

Map (i.e., Dictionary)

SortedMap

Iterator

Iterable

ListIterator

Classes^ 

HashSet

TreeSet

ArrayList

LinkedList

HashMap

TreeMap

java.util.Iterator

(an

interface)

public boolean hasNext();^ 

Returns

true

if

the

iteration

has

more

elements

public E next();^ 

Returns

the

next

element

in

the

iteration

Throws

NoSuchElementException

if

no

next

element

public void remove();^ 

The

element

most

recently

returned

by

next()

is

removed

from

the

underlying

collection

Throws

IllegalStateException

if

next()

not

yet

called

or

if

remove()

already

called

since

last

next()

Throws

UnsupportedOperationException

if

remove()

not

supported

Additional

Methods

of

Collection

public Object[] toArray()^ 

Returns

a

new

array

containing

all

the

elements

of

this

collection

public T[] toArray(T[] dest)^ 

Returns

an

array

containing

all

the

elements

of

this

collection;

uses

dest

as

that

array

if

it

can

Bulk

Operations:

public

boolean

containsAll(Collection<?>

c);

public

boolean

addAll(Collection<?

extends

E>

c);

public

boolean

removeAll(Collection<?>

c);

public

boolean

retainAll(Collection<?>

c);

public

void

clear();