PDF Generic Types and the Java Collections Framework
Generic Types and the
Java Collections Framework
Lecture 15 CS2110 ? Fall 2011
Generic Types
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 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
2
old
Example
//removes all 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 all 4-letter words from c static void purge(Collection c) {
Iterator i = c.iterator(); while (i.hasNext()) {
if (i.next().length() == 4) i.remove();
}}
3
new
old
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 grades = new HashMap(); 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();
4
new
Type Casting
The Java compiler determines that the cast is not necessary, based on the declared type
In this example, grades.get("John") is known at compile time always to be an Integer!
Map grades = new HashMap(); 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();
5
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- pdf standard template library and the java collections classes
- pdf learning computer programming using java with 101 examples
- pdf lambda expressions in java 8 part 1 basics
- pdf java current affairs 2018 apache commons collections
- pdf 15 department of computer science
- pdf java collections framework
- pdf java basics usf computer science
- pdf introduction to programming using java iit kanpur
- pdf java in a nutshell 6th edition covers java 8 r 5
- pdf photo credit andrew kennedy generics and the java
Related searches
- java collections tutorial with examples
- generic terms and conditions website
- generic terms and conditions template
- generic viagra without the prescription
- the java programming language pdf
- java collections sort example
- java collections sort
- collections framework in java
- happiness is the meaning and the purpose of life the whole aim and end of human
- the nine personality types of the enneagram
- java collections framework pdf
- generic terms and conditions agreement