L14cs2110fa08 - Generic Types and the Java Collections...

Info iconThis preview shows pages 1–8. 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 DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: Generic Types and the Java Collections Framework Lecture 14 CS2110 Fall 2008 2 Generic Types in Java 5 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 ) 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 Generics in Java 5 provide a way to communicate T, the type of elements in a collection, to the compiler Compiler can check that you have used the collection consistently Result: safer and more-efficient code 3 Example //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(); }} //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(); }} old new 3 Example //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(); }} //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(); }} old new 4 Another Example 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(); 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(); old new 5 Type Casting In effect, Java inserts the correct cast automatically, based on the declared type 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(); 6 An Aside: Autoboxing Java 5 also has autoboxing and auto-unboxing of primitive types, so the example can be further simplified 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()); 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"); 7 Using Generic Types <T> is read, of T For example:...
View Full Document

This note was uploaded on 02/01/2010 for the course CS 2110 at Cornell University (Engineering School).

Page1 / 29

L14cs2110fa08 - Generic Types and the Java Collections...

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

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