+ All Categories
Home > Documents > Collections Framework

Collections Framework

Date post: 14-Jan-2016
Category:
Upload: ciel
View: 75 times
Download: 0 times
Share this document with a friend
Description:
Collections Framework. A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain: Interfaces: abstract data types representing collections. - PowerPoint PPT Presentation
22
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain: Interfaces: abstract data types representing collections. allow collections to be manipulated independently of the details of their representation. Implementations: concrete implementations of the collection interfaces. In essence, these are reusable data structures. Algorithms: methods that perform useful computations on objects that implement collection interfaces e.g. searching and sorting,. polymorphic reusable functionality
Transcript
Page 1: Collections Framework

1

Collections FrameworkA collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain: •Interfaces:

– abstract data types representing collections. • allow collections to be manipulated independently of the details of their

representation.

•Implementations: – concrete implementations of the collection interfaces. In essence, these are

reusable data structures.

•Algorithms: – methods that perform useful computations on objects that implement

collection interfaces• e.g. searching and sorting,.

• polymorphic

• reusable functionality

Page 2: Collections Framework

2

Java CollectionsThe Collections framework includes classes that implement Collection, and are thus able to be declared using a specific type

Concrete implementations:

Set List Map

SortedSet SortedMap HashSet

TreeSet ArrayList LinkedList

Vector Collections ArraysThese allow programmers to focus on the abstract, logical properties,

ignoring implementation details.

Page 3: Collections Framework

3

Interfaces

Collection Set extends Collection implemented by HashSet, TreeSet SortedSet extends Set implemented by TreeSet List extends Collection implemented by ArrayList, LinkedListMap implemented by HashMap, TreeMap SortedMap extends Map implemented by TreeMap

Note Class HashSet corresponds to Hashtable in JDK 1.1 and class ArrayList corresponds to Vector in JDK 1.1. But these older classes are no longer needed.

Collections Interfaces

Page 4: Collections Framework

4

.

Collections represent possibly duplicated, unordered collections of objects:

public interface Collection { int size(); boolean isEmpty(); boolean contains(Object obj); boolean add(Object obj); boolean remove(Object obj); Iterator iterator();}

Iterators (cf. string tokenizers) are used to inspect each element of a collection in turn.

public interface Iterator { boolean hasNext(); Object next();}

Basic iterator usage:

Collection c; ...; Iterator it = c.iterator();while (it.hasNext()) { /* use it.next() */ }

Collections Interfaces

Page 5: Collections Framework

5

.

Sets represent duplicate-free, unordered collections of objects.

public interface Set extends Collection { boolean containsAll(Collection c); // subset void addAll(Collection c); // union void retainAll(Collection c); // intersection void removeAll(Collection c); // difference

// Plus methods inherited from class Collection}

These additional methods correspond to set-theoretic operations as follows. Suppose S and T are variables of type Set. Then:

S.contains(x) is equivalent to x S. S.containsAll(T) is equivalent to S T. S.addAll(T) is equivalent to S = S T. S.retainAll(T) is equivalent to S = S T. S.removeAll(T) is equivalent to S = S T.

Collections Class: Set

Page 6: Collections Framework

6

Print all distinct command line arguments to standard output.

class Words {

public static void main(String[] args) { Set words = new HashSet(); // Set of words seen // Add new command line arguments to words for (int i = 0; i < args.length; i++) { words.add(args[i]); } // Print elements of words one per line // Cf. System.out.println(words); Iterator it = words.iterator(); while (it.hasNext() ) { System.out.println(it.next()); } }}

Example - Find distinct words

Page 7: Collections Framework

7

Usage:

$ java Words I am who I am

am

I

who

$

Note If we had used TreeSet (which implements SortedSet) instead of HashSet , the set words would have been maintained in its natural (alphabetical) order, and the output would have been ordered:

I

am

who

Example - Find distinct words

Page 8: Collections Framework

8

Print all command line arguments that occur more than once to standard output.

class Words {

public static void main(String[] args) { Set words = new HashSet(); // Set of all words Set repWords = new HashSet(); // Set of repeated words // Add new command line arguments to words for (int i = 0; i < args.length; i++) { String word = args[i]; if (repWords.contains(word)) { /* skip */ } else if (words.contains(word)) { repWords.add(word) } else { words.add(word); } } // Print elements of repWords one per line Iterator it = repWords.iterator(); while (it.hasNext() ) { System.out.println(it.next()); } }}

Example - Find repeated words

Page 9: Collections Framework

9

Lists represent possibly duplicated, ordered collections of objects.

public interface List extends Collection { Object get(int index); Object set(int index, Object obj); void add(int index, Object obj); Object remove(int index); List subList(int from, int to); ListIterator listIterator();

// Plus methods inherited from class Collection}

A list (or Vector in JDK 1.1) is like an array except that:

• Its elements must be objects (not primitive types or arrays).• Its elements may be values of different types.• Its size can grow and shrink dynamically.• It can be efficient to add and remove elements in the middle of the list.

Collections Class: List

Page 10: Collections Framework

10

public void reverse (List objs) {

int i = 0, j = objs.size()-1;

while (i < j) {

// Exchange elements at positions i and j

Object tmp = objs.get(i);

objs.set(i, objs.get(j));

objs.set(j, tmp);

i++; j--;

}

}

Example – Reverse a List in place

Page 11: Collections Framework

11

Return a new list containing the elements of the given list in reverse order.

public List reverse (List objs) { List revObjs = new ArrayList(); // the new list // Add elements to revObjs from the end of objs for (int i = objs.size()-1; i >= 0; i--) { revObjs.add(objs.get(i));//append to the end of revObjs } return revObjs;}

Note Collection parameters, local variables and results all have interface types (e.g., List), not implementation types (e.g. ArrayList).

Example – Return a List that is the reverse of another

Page 12: Collections Framework

12

Maps represent finite mappings or functions from keys to values:

public interface Map { int size(); boolean isEmpty(); Object put(Object key, Object value); Object get(Object key); Object remove(Object key); boolean containsKey(Object key); boolean containsValue(Object value);

public Set keySet(); public Collection values(); public Set entrySet();}

A map is equivalent to a set of (key, value) pairs. (Cf. a pair of parallel arrays, or Dictionary in JDK 1.1.) The keys and values may be objects of any type. There may be any (finite) number of (key, value) pairs. Each key must be distinct, but different keys may map to the same value.

Collections Class: Map

Page 13: Collections Framework

13

class CountWords {

public static final Integer ONE = new Integer(1);

public static void main(String[] args) { Map map = new HashMap(); // Maps words to no. of occs. for (int i = 0; i < args.length; i++) { // Get number of occurrences of next word Integer value = (Integer) map.get(args[i]); // Increment number of occurrences of word if (value == null) { map.put(args[i], ONE); } else { int val = value.intValue(); map.put(args[i], new Integer(val+1)); } } // Print number of occurrences of each word System.out.println(map); }}

Example - Count distinct words

Works

Page 14: Collections Framework

14

Another way to print the final value of map (version 2):

// Print values of map, one per line Iterator it = map.keySet().iterator(); while (it.hasNext() ) { Object key = it.next(); System.out.println(key + " = " + map.get(key)); }

And yet another way (version 3):

// Print values of map, one per line Iterator it = map.entrySet().iterator(); while (it.hasNext() ) { Map.Entry e = (Map.Entry) it.next(); System.out.println(e.getKey() + " = " + e.getValue()); }

Example - Count distinct words (con’t.)

Page 15: Collections Framework

15

Usage (version 1):

$ java Words I am who I am

{am=2,who=1,I=2}

$

Usage (versions 2 and 3):

$ java Words I am who I am

am = 2

who = 1

I = 2

$

Note Again, if we had used TreeMap instead of HashMap, the keys would have been processed (and printed) in their natural (alphabetical) order.

Example - Count distinct words (con’t.)

Page 16: Collections Framework

16

A concordance for a given text file is a listing of all words that occur in the file, together with a list of all the pages (or lines) on which each word occurs.

This requires a collection of collections.

•The outer collection has keys the words.– Each (key,value) pair is a word and a List.

• The List holds the page numbers for that word

Simpler version: Concordance.java counts #times each character occurs.• Each (key,value) pair is a character and an integer .

See java/Collections/Concordance

Example - Concordance

Page 17: Collections Framework

17

The collection of collection paradigm can be used to model a (book) library, keeping track of loans of books by borrowers.

– Each person can borrow one or more books.

– Each book can only be borrowed by a single person (at one time).

•Can implement methods for:– borrowing and returning books

• displaying which books have been borrowed by a given person

• determining which person has borrowed a given book.

model information about loans as two related maps, allowing greater clarity and faster searches:

– person: a map from persons to lists (or sets) of books, and– book: an inverse map from books to persons.

Example - Library database

Page 18: Collections Framework

18

Example - Library database (cont.)

Person map

Book map

p1

p5

p3

p1 (person)

b2 b10 b5 b3

b7 b12

b10

(book list)

b1 (new book in list)

Page 19: Collections Framework

19

Probably the most subtle method in the implementation using maps and lists is the following. It requires careful study, using the diagram on the previous slide.

// Pre: book b is not currently borrowed // Post: person p has borrowed b public void borrow (String p, String b) { // Get books borrowed by person p

List books = (List) person.get(p); if (books == null) { // if none => books = new ArrayList(); // make it an empty list person.put(p, books); } books.add(b); // Add b to the books borrowed by p

// (No additional put is needed) book.put(b, p); // Make p the borrower of b

}

p5p5 p5 b3

Example - Library database (cont.)

Page 20: Collections Framework

20

compare objects for equality:– based on one or more of its components

– compute a hash code that depends on one or more components. • the components form a key that uniquely identifies the object.

• must override the definitions of methods equals and hashCode inherited from class Object.

class Book { public String catref; // key for books public String title; public String author; ... public boolean equals (Object o) { if (o == null || ! (o instanceof Book)) return false; else return catref.equals(((Book) o).catref); } public int hashCode () { return catref.hashCode(); }}

The Collections Framework (cont.)

Page 21: Collections Framework

21

1. Use the collections framework to implement sets, lists, dynamic arrays, and maps.

2. Always import java.util.*;

3. Use interface types –  Set, List, Map – for variables and parameters

4. Use implementation types – HashSet, ArrayList – only in constructors.

5. Lists, maps, etc., must contain objects, not primitive types or arrays.

6. Remember to specify values selected from collections and maps to the appropriate type, e.g., if a list ints is known to contain values of type Integer, then you should declare it List<Integer> ints to avoid casting:

Integer i = (Integer) ints.get(0);

to retrieve the first integer. Omitting the cast is a compile-time error only if the collections element type hasn’t been specified.

7. A class List is defined in both packages java.awt and java.util. It is thus necessary to write, e.g., java.util.List in variable declarations and casts when both packages are being used.

Notes on the collections framework

Page 22: Collections Framework

22

8. This method can transform arrays to lists:

Arrays.asList(Object[] a);

Returns a list containing the objects of the array argument.

9. Use the following method to transform collections (e.g., sets and lists) to arrays:

collection.toArray();

Returns an array containing the objects of the given collection.

10. Use the following constructor to create a list from any collection:

List list = new ArrayList(collection);

There is a simple constructor for LinkedList.

Notes on the collections framework (con’t)


Recommended