Tải bản đầy đủ (.pdf) (60 trang)

Chapter 2 Collections

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (358.24 KB, 60 trang )

CHAPTER 2
COLLECTIONS
Lecturer: Hồ Tiến Lâm
Contents
 Collection Interfaces
 Concrete Collections
 Legacy Collections
2
Created by Ho Tien Lam
Contents
 Collection Interfaces
 Concrete Collections
 Legacy Collections
3
Created by Ho Tien Lam
Interfaces and Implementation
 The Java collection library separates
interfaces and implementations.
 Let’s look at that separation with a familiar
data structure, the
queue
.
4
data structure, the
queue
.
 You use a queue when you need to collect
objects and retrieve them in a FIFO order.
Created by Ho Tien Lam
Interfaces and Implementation
(cont.)


 A minimal form of a queue interface:
interface Queue<E> {
void add(E element);
E remove();
5
 This interface tells you nothing about how the
queue is implemented. There are two
implementations of a queue.
int size();
}
Created by Ho Tien Lam
Interfaces and Implementation
(cont.)
 One implementation uses a circular array and
one uses a linked list.
6
Created by Ho Tien Lam
Interfaces and Implementation
(cont.)
 When you use a queue in your program, you
don’t need to know which implementation is
actually used.

Use the interface type to hold the collection
7

Use the interface type to hold the collection
reference.
Created by Ho Tien Lam
Queue<Customer> expressLane = new

CircularArrayQueue<Customer>(100);
expressLane.add(new Customer("Harry"));
Interfaces and Implementation
(cont.)
 With this approach if you change your mind,
you can easily use a different implementation.
 You only need to change in one place, the
constructor → save time of modification
8
constructor → save time of modification
Created by Ho Tien Lam
Queue<Customer> expressLane = new
LinkedListQueue<Customer>();
expressLane.add(new Customer("Harry"));
Interfaces and Implementation
(cont.)
 Among Java collection classes, there are
some classes whose name begins with
Abstract (AbstractQueue).

These classes are intended for library
9

These classes are intended for library
implementors.
 It is more convenient for developer to extend
AbstractQueue than to implement Queue
interface.
Created by Ho Tien Lam
Collection and Iterator Interfaces

10
public interface Collection<E> {
boolean add(E element);
Iterator<E> iterator();

}
Created by Ho Tien Lam
 add: adds an element to the collection.
Returns true if adding the element actually
changes the collection. Otherwise, returns
false.
}
Collection and Iterator Interfaces
(cont.)
11
public interface Collection<E> {
boolean add(E element);
Iterator<E> iterator();

}
Created by Ho Tien Lam
 iterator: returns an object that implements
the Iterator interface. You can use the iterator
object to visit the elements in the collection
one by one.
}
Collection and Iterator Interfaces
(cont.)
12
public interface Iterator<E> {

E next();
boolean hasNext();
void remove();
}
Created by Ho Tien Lam
 next: visits the elements one by one. If you
reach the end of collection, this method throws
a NoSuchElementException.
 hasNext: returns true if the iterator still has
more elements to visit.
}
Collection and Iterator Interfaces
(cont.)
13
Collection<String> c = ;
Iterator<String> iter = c.iterator();
while (iter.hasNext()) {
String element = iter.next();
do something with element
Created by Ho Tien Lam
do something with element
}
Collection and Iterator Interfaces
(cont.)
14
 “for each” loop: simple loop
Collection<String> c = ;
for (String element : c) {
do something with element
}

Created by Ho Tien Lam
 This kind of loop works with any object that
implements the Iterable interface.
}
public interface Iterable<E> {
Iterator<E> iterator();
}
Collection and Iterator Interfaces
(cont.)
15
 The Collection interface extends the
Iterable interface. So you can use “for
each” loop with any collection in Java library.

The order in which the elements are visited
Created by Ho Tien Lam

The order in which the elements are visited
depends on the collection type.
 But you can be assured that you will encounter
all elements of the collection.
Collection and Iterator Interfaces
(cont.)
16
 Example: How do next and remove method of
Iterator works?
A
B
C
D

Created by Ho Tien Lam
 it.next(); → returns A
 it.next(); → returns B
 it.remove(); → removes B
 it.remove(); → IllegalStateException
A
B
C
D
Collection and Iterator Interfaces
(cont.)
17
 Generic Utility Methods:
int size()
boolean isEmpty()
boolean contains(Object obj)
boolean
containsAll
(Collection<?> c)
Created by Ho Tien Lam
boolean
containsAll
(Collection<?> c)
boolean equals(Object other)
boolean addAll(Collection<? extends E> from)
boolean remove(Object obj)
boolean removeAll(Collection<?> c)
void clear()
boolean retainAll(Collection<?> c)
Object[] toArray()

<T> T[] toArray(T[] arrayToFill)
Collection and Iterator Interfaces
(cont.)
18
 The library supplies an abstract class that
leaves the fundamental methods size and
iterator abstract.
public abstract class AbstractCollection<E>
Created by Ho Tien Lam
implements Collection<E> {
public abstract Iterator<E> iterator();
public boolean contains(Object obj) {
for (E element : c) // calls iterator()
if (element.equals(obj)) return = true;
return false;
}
. . .
}
Collection and Iterator Interfaces
(cont.)
19
 A concrete collection class can now extend the
AbstractCollection class.
 The implementor just needs to supply an
iterator
method, but the
contains
method
Created by Ho Tien Lam
iterator

method, but the
contains
method
has been taken care of by the superclass.
 This is an excellent design for a class
framework.
Contents
 Collection Interfaces
 Concrete Collections
 Legacy Collections
20
Created by Ho Tien Lam
Concrete Collections
21
 Linked Lists
 Array Lists
 Hash Sets

Tree Sets
Created by Ho Tien Lam

Tree Sets
 Priority Queues
 Maps
 Specialized Set and Map Classes
Linked Lists
22
 ArrayList class has a major drawback:
Removing an element from the middle of an
array is expensive because of the cost of

moving all elements beyond the removed one.
Created by Ho Tien Lam
Linked Lists (cont.)
23
 The linked list will solve this problem because
a linked list stores each object in a separate
link. Each link also stores a reference to the
next link in the sequence.
Created by Ho Tien Lam
Linked Lists (cont.)
24
 Example 1:
List<String> staff = new LinkedList<String>();
// LinkedList implements List
staff.add("Amy");
staff.add
("Bob");
Created by Ho Tien Lam
staff.add
("Bob");
staff.add("Carl");
Iterator iter = staff.iterator();
String first = iter.next(); // visit first element
String second = iter.next(); // visit second element
iter.remove(); // remove last visited element
Linked Lists (cont.)
25
 Example 1:
Created by Ho Tien Lam

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×