Module 2
Module 2
Generics add the one feature that collections had been missing: type safety. Prior to
generics, all collections stored Object references, which meant that any collection could
store any type of object. Thus, it was possible to accidentally store incompatible types in
a collection. Doing so could result in run-time type mismatch errors. With generics, it is
possible to explicitly state the type of data being stored, and run-time type mismatch
errors can be avoided.
In the past, if wanted to store a primitive value, such as an int, in a collection, you had to
manually box it into its type wrapper. When the value was retrieved, it needed to be
manually unboxed (by using an explicit cast) into its proper primitive type. Because of
autoboxing/unboxing, Java can automatically perform the proper boxing and unboxing
needed when storing or retrieving primitive types.
The For-Each Style for Loop
All collection classes in the Collections Framework have been retrofitted to implement
the Iterable interface, which means that a collection can be cycled through by use of the
for-each style for loop.
Advanced Java and J2EE – 15CS553 Module 2
1. Collection
The Collection interface is the foundation upon which the Collections Framework is
builtbecause it must be implemented by any class that defines a collection. Collection is
a genericinterface that has this declaration:
interface Collection<E>
interface NavigableSet<E>
Some of the classes provide full implementations that can be used as-is. Others are
abstract, providing skeletal implementations that are used as starting points for creating
concrete collections.
The standard collection classes are summarized in the following table:
class ArrayList<E>
// Demonstrate ArrayList.
import java.util.*;
class ArrayListDemo {
public static void main(String args[]) {
// Create an array list.
class LinkedList<E>
import java.util.*;
class LinkedListDemo {
public static void main(String args[]) {
// Create a linked list.
LinkedList<String> ll = new LinkedList<String>();
// Add elements to the linked list.
ll.add("F");
ll.add("B");
ll.add("D");
ll.add("E");
ll.add("C");
ll.addLast("Z");
ll.addFirst("A");
ll.add(1, "A2");
System.out.println("Original contents of ll: " + ll);
// Remove elements from the linked list.
ll.remove("F");
ll.remove(2);
System.out.println("Contents of ll after deletion: "
+ ll);
// Remove first and last elements.
ll.removeFirst();
ll.removeLast();
System.out.println("ll after deleting first and last: "String val = ll.get(2);
ll.set(2, val + " Changed");
class HashSet<E>
import java.util.*;
class HashSetDemo {
public static void main(String args[]) {
// Create a hash set.
HashSet<String> hs = new HashSet<String>();
// Add elements to the hash set.
hs.add("B");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("F");
System.out.println(hs);
}
}
class LinkedHashSet<E>
class TreeSet<E>
// Demonstrate TreeSet.
import java.util.*;
class TreeSetDemo {
public static void main(String args[]) {
// Create a tree set.
TreeSet<String> ts = new TreeSet<String>();
// Add elements to the tree set.
ts.add("C");
ts.add("A");
ts.add("B");
ts.add("E");
ts.add("F");
ts.add("D");
System.out.println(ts);
}
}
The output from this program is shown here:
[A, B, C, D, E, F]
The PriorityQueue Class
PriorityQueue extends AbstractQueue and implements the Queue interface. It creates a
queue that is prioritized based on the queue’s comparator. PriorityQueue is a generic
class that has this declaration:
class PriorityQueue<E>
class ArrayDeque<E>
// Demonstrate ArrayDeque.
import java.util.*;
class ArrayDequeDemo {
public static void main(String args[]) {
// Create a tree set.
ArrayDeque<String> adq = new ArrayDeque<String>();
interface Iterator<E>
interface ListIterator<E>
al.add("E");
al.add("B");
al.add("D");
al.add("F");
// Use iterator to display contents of al.
System.out.print("Original contents of al: ");
Iterator<String> itr = al.iterator();
while(itr.hasNext()) {
String element = itr.next();
System.out.print(element + " ");
}
System.out.println();
// Modify objects being iterated.
ListIterator<String> litr = al.listIterator();
while(litr.hasNext()) {
String element = litr.next();
litr.set(element + "+");
}
System.out.print("Modified contents of al: ");
itr = al.iterator();
while(itr.hasNext()) {String element = itr.next();
System.out.print(element + " ");
}
System.out.println();
// Now, display the list backwards.
System.out.print("Modified list backwards: ");
while(litr.hasPrevious()) {
String element = litr.previous();
System.out.print(element + " ");
}
System.out.println();
}
}
The output is shown here:
Original contents of al: C A E B D F
Modified contents of al: C+ A+ E+ B+ D+ F+
Modified list backwards: F+ D+ B+ E+ A+ C+
2.6. Storing User Defined Classes in collections
The power of collections is that they can store any type of object, including objects of
classes that you create. For example, consider the following example that uses a
LinkedList to store mailing addresses:
// A simple mailing list example.
import java.util.*;
class Address {
private String name;
private String street;
private String city;
private String state;
private String code;
Address(String n, String s, String c,
String st, String cd) {
name = n;
street = s;
city = c;
state = st;
code = cd;
}
public String toString() {
return name + "\n" + street + "\n" +
city + " " + state + " " + code;
}
}class MailList {
public static void main(String args[]) {
LinkedList<Address> ml = new LinkedList<Address>();
// Add elements to the linked list.
ml.add(new Address("J.W. West", "11 Oak Ave",
"Urbana", "IL", "61801"));
ml.add(new Address("Ralph Baker", "1142 Maple Lane",
"Mahomet", "IL", "61853"));
ml.add(new Address("Tom Carlton", "867 Elm St",
"Champaign", "IL", "61820"));
// Display the mailing list.
for(Address element : ml)
System.out.println(element + "\n");
System.out.println();
}
}
The output from the program is shown here:
J.W. West
11 Oak Ave
Urbana IL 61801
Ralph Baker
1142 Maple Lane
Mahomet IL 61853
Tom Carlton
867 Elm St
Champaign IL 61820
2.7. The Random Access Interface
Amap is an object that stores associations between keys and values, or key/value pairs.
Given a key, you can find its value. Both keys and values are objects. The keys must be
unique, but the values may be duplicated
The Map Interfaces
2.9. Comparators
If you want to order elements a different way, then specify a Comparator when you
construct the set or map. Doing so gives you the ability to govern precisely how elements
are stored within sorted collections and maps.
Comparator is a generic interface that has this declaration:
interface Comparator<T>
The Comparator interface defines two methods: compare( ) and equals( ). The
compare( )
method, shown here, compares two elements for order:
import java.util.*;
// A reverse comparator for strings.
class MyComp implements Comparator<String> {
public int compare(String a, String b) {
String aStr, bStr;
aStr = a;
bStr = b;
// Reverse the comparison.
return bStr.compareTo(aStr);
}
// No need to override equals.
}
class CompDemo {
public static void main(String args[]) {
// Create a tree set.
TreeSet<String> ts = new TreeSet<String>(new MyComp());
// Add elements to the tree set.
ts.add("C");
ts.add("A");
ts.add("B");
ts.add("E");
ts.add("F");
ts.add("D");
// Display the elements.
for(String element : ts)
System.out.print(element + " ");
System.out.println();
}
}
As the following output shows, the tree is now stored in reverse order:
FEDCBA