12-GenericTypes - 3/8/2011 Generic Types in Java 5 CS/ENGRD...

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

View Full Document Right Arrow Icon
3/8/2011 1 CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2010 Thorsten Joachims Lecture 12: Generic Types and the Collection Framework 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 Problem? Need explicit cast inconvenient Compiler could not check that the cast was correct at compile-time, since it didn't know what T was 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 safer and more-efficient code 2 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(); Example 3 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 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 4 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 Java 5 also has autoboxing and auto- unboxing of primitive types, so the example can be further simplified 5 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"); Another 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(); }} static void purge(Collection <String> c) { Iterator <String> i = c.iterator(); while (i.hasNext()) { if (i.next().length() == 4) i.remove(); }}
Background image of page 1

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

View Full DocumentRight Arrow Icon
3/8/2011 2 Using Generic Types <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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

12-GenericTypes - 3/8/2011 Generic Types in Java 5 CS/ENGRD...

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