Java Collections and Generics: - Prashant Kumar
Java Collections and Generics: - Prashant Kumar
-Prashant kumar
Objective
– Hash Table
– Vector
– Stack
• Lacked interoperability.
Features of Collection framework
Rule of thumb: Use a Tree only if you need them sorted, otherwise
use a Hash
Set Usage Example
import java.util.*;
public class SetExample {
public static void main(String args[]) {
Set set = new HashSet();
set.add("Bernadine");
set.add("Elizabeth");
set.add("Gene");
set.add("Elizabeth");
set.add("Clara");
System.out.println(set);
Set sortedSet = new TreeSet(set);
System.out.println(sortedSet);
}
}
AbstractSet class
map.put("1", “Kunal");
map.put("3", “Varun");
map.put(“2", “Suraj");
map2.put(“4", “Vikram");
while(iterator.hasNext()){
System.out.println("Map Entry"+entry.getKey());
}
}
HashMap and TreeMap classes
• Depending upon the size of your collection, it may be faster to add elements to a
HashMap, then convert the map to a TreeMap for sorted key traversal.
•Using a HashMap requires that the class of key added have a well-defined
hashCode() implementation.
•With the TreeMap implementation, elements added to the map must be sortable.
To optimize HashMap space usage, you can tune the initial capacity and load
factor.
•The TreeMap has no tuning options, as the tree is always balanced. Both
HashMap and TreeMap implement the Cloneable interface.
Performance Difference between
ConcurrentHashMap and synchronised HashMap
• Only one sort sequence can be • Many sort sequences can be created.
created.
Read-Only Collections
After you've added all the necessary elements to a collection, it may be convenient
to treat that collection as read-only, to prevent the accidental modification of the
collection.
To provide this capability, the Collections class provides six factory methods, one
for each of Collection, List, Map, Set, SortedMap, and SortedSet.
Once you've filled the collection, the best way to make the collection read-only is to
replace the original reference with the read-only reference. If you don't replace the
original reference, then the collection is not read-only, as you can still use the
original reference to modify the collection. The following program demonstrates the
proper way to make a collection read-only. In addition, it shows what happens
when you try to modify a read-only collection.
When run and the last add() operation is attempted on the read-only set, an
UnsupportedOperationException is thrown.
Read-Only Collections (Contd.)
public static void main(String[] args) {
set2 = Collections.unmodifiableSet(set2);
set2.add("Sushant");
}
Exception Thrown
Example:
If you compile and run the Old class and then run it with a string, like this:
java Old Hello
you get: Hello / 5
What Generics are not ?
The type
The type checking
checking with
with Java
Java 1.5
1.5 Generics
Generics
changes the
changes the way
way one
one programs
programs
Java 1.0 vs Generics
Compare with
• Type erasure
– Compile-time type checking uses generics
– Compiler eliminates generics by erasing them
• Compile List<T> to List, T to Object, insert casts