Intro To JCF
Intro To JCF
Framework
Chapter 20
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
1
Objectives
▪ To describe the Java Collections Framework
hierarchy
▪ To use the common methods defined in the Collection
interface for operations on sets and lists
▪ To use the Iterator interface to traverse a collection
▪ To use the “for each” loop to simplify traversing a
collection
▪ To compare elements using the Comparator interface.
▪ To learn how good Object Oriented programming
practices can be used in implementing data structures
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
2
Java Collections Framework (JCF)
▪ A collection is a container object that
represents a group of objects, often referred
to as elements.
▪ The Java Collections Framework supports
two types of named containers:
1. Collection: stores a simple collection of elements
2. Map: stores key/value pairs
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
3
JCF: Collection hierarchy
SortedSet TreeSet
Collection AbstractCollection
Vector Stack
List AbstractList
ArrayList
AbstractSequentialList LinkedList
Deque
JCF supports three major collections Set, List, Queue under the Collection
interface
The Abstract Classes are provided for convenience and hence they are referred
to as Convenience Abstract Classes.
The AbstractCollection class implements all methods of Collection, except for
add, size and iterator.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
4
JCF: Map hierarchy
SortedMap TreeMap
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
6
The Collection Interface
The Collection
«interface» interface is the root
java.util.Collection<E> interface for
+add(o: E): boolean Adds a new element o to this collection.
+addAll(c: Collection<? extends E): boolean Adds all the elements in the collection c to this collection. manipulating a
+clear(): void Removes all the elements from this collection. collection of objects.
+contains(o: Object): boolean Returns true if this collection contains the element o.
+containsAll(c: Collection<?>):boolean Returns true if this collection contains all the elements in c.
+equals(o: Object): boolean Returns true if this collection is equal to another collection o.
+hashCode(): int Returns the hash code for this collection.
+isEmpty(): boolean Returns true if this collection contains no elements.
+iterator(): Iterator Returns an iterator for the elements in this collection.
+remove(o: Object): boolean Removes the element o from this collection.
+removeAll(c: Collection<?>): boolean Removes all the elements in c from this collection.
+retainAll(c: Collection<?>): boolean Retains the elements that are both in c and in this collection.
+size(): int Returns the number of elements in this collection.
+toArray(): Object[] Returns an array of Object for the elements in this collection.
«interface»
java.util.Iterator<E>
+hasNext(): boolean Returns true if this iterator has more elements to traverse.
+next(): E Returns the next element from this iterator.
+remove(): void Removes the last element obtained using the next method.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
7
Example: Using Methods defined
in Collection
The program creates a concrete collection object using ArrayList .
invokes the Collection interface’s methods:
– contains() ,
– Remove(),
– Size(),
– addAll() ,
– retainAll (), and
– removeAll()
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
8
1 import java.util.*;
Test Collection
2
3 public class TestCollection { 26
4 public static void main(String[] args) { 27 System.out.println("\nA list of cities in collection2:");
5 ArrayList<String> collection1 = new ArrayList<String>(); 28 System.out.println(collection2);
6 collection1.add("New York");
7 collection1.add("Atlanta");
8 collection1.add("Dallas"); 29
9 collection1.add("Madison"); 30 ArrayList<String> c1 = (ArrayList<String>)(collection1.clone());
10 31 c1.addAll(collection2);
11 System.out.println("A list of cities in collection1:"); 32 System.out.println("\nCities in collection1 or collection2: ");
12 System.out.println(collection1); 33 System.out.println(c1);
13 34
14 System.out.println("\nIs Dallas in collection1? “ 35 c1 = (ArrayList<String>)(collection1.clone());
15 + collection1.contains("Dallas")); 36 c1.retainAll(collection2)
37 System.out.print("\nCities in collection1 and collection2: ");
38 System.out.println(c1);
16
17 collection1.remove("Dallas");
18 System.out.println("\n" + collection1.size() + 39
19 " cities are in collection1 now"); 40 c1 = (ArrayList<String>)(collection1.clone());
41 c1.removeAll(collection2);
42 System.out.print("\nCities in collection1, but not in 2: ");
20 43 System.out.println(c1);
21 Collection<String> collection2 = new ArrayList<String>(); 44 }
22 collection2.add("Seattle"); 45 }
23 collection2.add("Portland");
24 collection2.add("Los Angles");
25 collection2.add("Atlanta");
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
9
Manipulating Collections
Lets defer a little bit looking at the
collections and move on to look and the
apparatus for manipulating collections
provided by the:
– Iterator interface, and
– Iterators enables traversal of the elements in a collection
one after the other
– Comparator Interface
– Enables elements in a collection to be sorted in a particular
order
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All
rights reserved.
10