L14cs2110fa09-6up

L14cs2110fa09-6up - 1 GENERIC TYPES AND THE JAVA...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 10/5/2009 1 GENERIC TYPES AND THE JAVA COLLECTIONS FRAMEWORK Lecture 14 CS2110 – Fall 2009 Generic Types in Java 5 2 ¡ When using a collection (e.g., LinkedList , HashSet , HashMap ), we generally have a single type T of elements that we store in it (e.g., Integer, String ) Generics in Java 5 provide a way to communicate T, the type of elements in a collection, to the compiler ¡ Before Java 5, when extracting an element, had to cast it to T before we could invoke T's methods ¡ Compiler could not check that the cast was correct at compile-time , since it didn't know what T was ¡ Inconvenient and unsafe, could fail at runtime ¢ Compiler can check that you have used the collection consistently ¢ Result: safer and more-efficient code Example 3 //removes 4-letter words from c //elements must be Strings static void purge(Collection c) { Iterator i = c.iterator(); while (i.hasNext()) { if (( (String) i.next()).length() == 4) i.remove(); old } } //removes 4-letter words from c static void purge(Collection <String> c) { Iterator <String> i = c.iterator(); while (i.hasNext()) { if (i.next().length() == 4) i.remove(); } } new Another Example 4 Map grades = new HashMap(); grades.put("John", new Integer(67)); grades.put("Jane", new Integer(88)); grades.put("Fred", new Integer(72)); Integer x = (Integer) grades.get("John"); sum = sum + x.intValue(); old Map <String, Integer> grades = new HashMap <String, Integer> (); grades.put("John", new Integer(67)); grades.put("Jane", new Integer(88)); grades.put("Fred", new Integer(72)); Integer x = grades.get("John"); sum = sum + x.intValue(); new Type Casting 5 ¡ In effect, Java inserts the correct cast automatically, based on the declared type ¡ In this example grades get("John") is automatically ¡ In this example, grades.get( John ) is automatically cast to Integer Map <String, Integer> grades = new HashMap <String, Integer> (); grades.put("John", new Integer(67)); grades.put("Jane", new Integer(88)); grades.put("Fred", new Integer(72)); Integer x = grades.get("John"); sum = sum + x.intValue(); An Aside: Autoboxing 6 ¡ Java 5 also has autoboxing and auto-unboxing of primitive types, so the example can be simplified Map <String,Integer> grades = new HashMap <String,Integer> (); grades.put("John",new Integer(67)); grades.put("Jane",new Integer(88)); ¡ AutoBoxing/Unboxing: converts from “int” to “Integer”, “byte” to “Byte”, etc grades.put("Fred",new Integer(72)); Integer x = grades.get("John"); sum = sum + x.intValue()); Map <String,Integer> grades = new HashMap <String,Integer> (); grades.put("John", 67); grades.put("Jane", 88); grades.put("Fred", 72); sum = sum + grades.get("John"); 10/5/2009 2 Using Generic Types 7 ¡ <T> is read, “of T” ¡ For example: Stack<Integer> is read, “ Stack of Integer ” ¡ The type annotation <T> informs the compiler that all extractions from this collection should be automatically cast to T ¡ Specify type in declaration, can be checked at compile time ¡...
View Full Document

{[ snackBarMessage ]}

Page1 / 5

L14cs2110fa09-6up - 1 GENERIC TYPES AND THE JAVA...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online