Tải bản đầy đủ (.pptx) (22 trang)

Bài giảng cấu trúc dữ liệu chương 14 nguyễn xuân vinh

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 (449.86 KB, 22 trang )

GV: NGUYỄN XUÂN VINH
MÔN: CẤU TRÚC DỮ LIỆU

COLLECTIONs FRAMEWORK

1

/XX

12/3/15

CẤU TRÚC DỮ LIỆU
DATA STRUCTURES
[214441]

Nguyễn Xuân Vinh



GV: NGUYỄN XUÂN VINH
MÔN: CẤU TRÚC DỮ LIỆU
12/3/15
/XX
2

Introduction to Collections
 A collection — sometimes called a container — is simply an 
object that groups multiple elements into a single unit.
 Collections are used to store, retrieve, manipulate, and 
communicate aggregate data.
 Represent data items that form a natural group, such as:


 A poker hand (a collection of cards)
 A mail folder (a collection of letters)
 A telephone directory (a mapping of names to phone numbers)


GV: NGUYỄN XUÂN VINH
MÔN: CẤU TRÚC DỮ LIỆU
12/3/15
/XX
3

What is a Collections Framework
 A collections framework is a unified architecture for representing 
and manipulating collections.
 All collections frameworks contain the following:
 Interfaces: These are abstract data types that represent 
collections
 Implementations: These are the concrete implementations of the 
collection interfaces.
 Algorithms: These are the methods that perform useful 
computations, such as searching and sorting


4

/XX

12/3/15
MÔN: CẤU TRÚC DỮ LIỆU


GV: NGUYỄN XUÂN VINH

Collections Interface


5

/XX

12/3/15
MÔN: CẤU TRÚC DỮ LIỆU

GV: NGUYỄN XUÂN VINH

Collections Implementations


6

/XX

12/3/15
MÔN: CẤU TRÚC DỮ LIỆU

GV: NGUYỄN XUÂN VINH

Collections Implementations


7


/XX

12/3/15
MÔN: CẤU TRÚC DỮ LIỆU

GV: NGUYỄN XUÂN VINH

Collection Implementations


8

/XX

12/3/15
MÔN: CẤU TRÚC DỮ LIỆU

GV: NGUYỄN XUÂN VINH

Concrete Classes: Collection


9

/XX

12/3/15
MÔN: CẤU TRÚC DỮ LIỆU


GV: NGUYỄN XUÂN VINH

Concrete Class: MAP


10

/XX

12/3/15
MÔN: CẤU TRÚC DỮ LIỆU

GV: NGUYỄN XUÂN VINH

Collections Comparision


GV: NGUYỄN XUÂN VINH
MÔN: CẤU TRÚC DỮ LIỆU
12/3/15
/XX
11

1) Collection Interface:Traversing Collections
 There are two ways to traverse collections: 
 (1) with the for-each construct
for (Object o : collection)
System.out.println(o);
 (2) by using Iterators.
Iterator<T> iterator = collection.iterator();

while(iterator.hasNext()) {
System.out.println(iterator.next());
}


GV: NGUYỄN XUÂN VINH
MÔN: CẤU TRÚC DỮ LIỆU
12/3/15
/XX
12

2) Collection Interface: Bulk Operations
 containsAll — returns true if the target Collection contains all of 
the elements in the specified Collection.
 addAll — adds all of the elements in the specified Collection to 
the target Collection.
 removeAll — removes from the target Collection all of its 
elements that are also contained in the specified Collection.
 retainAll — removes from the target Collection all its elements 
that are not also contained in the specified Collection. That is, it 
retains only those elements in the target Collection that are also 
contained in the specified Collection.
 clear — removes all elements from the Collection.


GV: NGUYỄN XUÂN VINH

3) Collection Interface: Array Operations
 The toArray methods are provided as a bridge between collections 
and older APIs that expect arrays on input.

 For example, suppose that c is a Collection:

13

/XX

12/3/15

MÔN: CẤU TRÚC DỮ LIỆU

Object[] a = c.toArray();
String[] a = c.toArray(new String[0]);


GV: NGUYỄN XUÂN VINH

public interface Set<E> extends Collection<E> {
// Basic operations
int size();
boolean isEmpty();
boolean contains(Object element);
// optional
boolean add(E element);
// optional
boolean remove(Object element);
Iterator<E> iterator();
// Bulk operations
boolean containsAll(Collection<?> c);
// optional
boolean addAll(Collection<? extends E> c);

// optional
boolean removeAll(Collection<?> c);
// optional
boolean retainAll(Collection<?> c);
// optional
void clear();

12/3/15

MÔN: CẤU TRÚC DỮ LIỆU

Set Interface

14

/XX

// Array Operations
Object[] toArray();
<T> T[] toArray(T[] a);
}


GV: NGUYỄN XUÂN VINH

public class FindDups {
public static void main(String[] args) {
Set<String> s = new HashSet<String>();
for (String a : args)
if (!s.add(a))

System.out.println("Duplicate detected: " + a);
System.out.println(s.size() + " distinct words: " + s);
}
}

Duplicate detected: i
4 distinct words: [i, left, saw, came]

15

12/3/15

Run:
java FindDups i came i saw i left
Output produced:
Duplicate detected: i

/XX

MÔN: CẤU TRÚC DỮ LIỆU

1) Set Interface: Basic Operations


GV: NGUYỄN XUÂN VINH
MÔN: CẤU TRÚC DỮ LIỆU
12/3/15
/XX
16


2) Set Interface: Bulk Operations
 s1.containsAll(s2) — returns true if s2 is a subset of s1. (s2 is a 
subset of s1 if set s1 contains all of the elements in s2.)
 s1.addAll(s2) — transforms s1 into the union of s1 and s2. (The 
union of two sets is the set containing all of the elements 
contained in either set.)
 s1.retainAll(s2) — transforms s1 into the intersection of s1 and 
s2. (The intersection of two sets is the set containing only the 
elements common to both sets.)
 s1.removeAll(s2) — transforms s1 into the (asymmetric) set 
difference of s1 and s2. (For example, the set difference of s1 
minus s2 is the set containing all of the elements found in s1 but 
not in s2.)


17

/XX

12/3/15
MÔN: CẤU TRÚC DỮ LIỆU

GV: NGUYỄN XUÂN VINH

HỎI ĐÁP


GV: NGUYỄN XUÂN VINH
MÔN: CẤU TRÚC DỮ LIỆU
12/3/15

/XX
18

List Interface
 A List is an ordered Collection (sometimes called a sequence). 
Lists may contain duplicate elements. In addition to the operations 
inherited from Collection, the List interface includes operations 
for the following:
 Positional access — manipulates elements based on their 
numerical position in the list
 Search — searches for a specified object in the list and returns its 
numerical position
 Iteration — extends Iterator semantics to take advantage of the 
list's sequential nature
 Range-view — performs arbitrary range operations on the list.


GV: NGUYỄN XUÂN VINH

public interface List<E> extends Collection<E> {
// Positional access
E get(int index);
// optional
E set(int index, E element);
// optional
boolean add(E element);
// optional
void add(int index, E element);
// optional
E remove(int index);

// optional
boolean addAll(int index, Collection<? extends E> c);
// Search
int indexOf(Object o);
int lastIndexOf(Object o);
// Iteration
ListIterator<E> listIterator();
ListIterator<E> listIterator(int index);

12/3/15

MÔN: CẤU TRÚC DỮ LIỆU

List Interface

19

/XX

// Range-view
List<E> subList(int from, int to);
}


GV: NGUYỄN XUÂN VINH
MÔN: CẤU TRÚC DỮ LIỆU
12/3/15
/XX
20


List Interface: Interators
public interface ListIterator<E> extends Iterator<E> {
boolean hasNext();
E next();
boolean hasPrevious();
E previous();
int nextIndex();
int previousIndex();
void remove(); //optional
void set(E e); //optional
void add(E e); //optional
}


GV: NGUYỄN XUÂN VINH
MÔN: CẤU TRÚC DỮ LIỆU
12/3/15
/XX
21

Queue Interface
public interface Queue<E> extends Collection<E> {
E element();
boolean offer(E e);
E peek();
E poll();
E remove();
}



public interface Map<K,V> {
// Basic operations
V put(K key, V value);
V get(Object key);
V remove(Object key);
boolean containsKey(Object key);
boolean containsValue(Object value);
int size();
boolean isEmpty();

MÔN: CẤU TRÚC DỮ LIỆU

GV: NGUYỄN XUÂN VINH

Map Interface

// Bulk operations
void putAll(Map<? extends K, ? extends V> m);
void clear();
// Collection Views
public Set<K> keySet();
public Collection<V> values();
public Set entrySet();

22

/XX

12/3/15


// Interface for entrySet elements
public interface Entry {
K getKey();
V getValue();
V setValue(V value);
}
}



×