Created
June 21, 2011 13:51
-
-
Save karanmalhi/1037890 to your computer and use it in GitHub Desktop.
Simple example on generics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.lq; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.Date; | |
import java.util.Iterator; | |
public class GenericsExample { | |
public static void main(String[] args) { | |
} | |
private static void simpleUsage() { | |
ArrayList<String> names = new ArrayList<String>(); | |
names.add("Alice"); | |
names.add("Sarah"); | |
names.add("Karl"); | |
// retrieving elements from collection do not require casts now | |
String str = names.get(1); | |
} | |
private static void simpleIteration() { | |
ArrayList<String> names = new ArrayList<String>(); | |
names.add("Alice"); | |
names.add("Sarah"); | |
names.add("Karl"); | |
// Iterator also "knows" about the underlying elements in collection | |
Iterator<String> iterator = names.iterator(); | |
while (iterator.hasNext()) { | |
String string = iterator.next(); | |
System.out.println(string); | |
} | |
} | |
private static void simpleHeirarchy() { | |
// have the ability to store subtypes of a type in a collection | |
ArrayList<Number> numbers = new ArrayList<Number>(); | |
numbers.add(new Integer(10)); | |
numbers.add(new Double(10)); | |
numbers.add(new Float(10)); | |
simpleHeirarchy2(numbers); // WORKS- Collection<Numbers> num = numbers; | |
// see $1 below: | |
ArrayList<Integer> integers = new ArrayList<Integer>(); | |
// simpleHeirarchy2(integers); DOES NOT WORK :( - $2 below | |
// To make it work, I need to use wildcards -- simpleHeirarchy3() | |
simpleHeirarchy3(integers); | |
/* | |
* $1 - a ArrayList of Number is assignable to a Collection of Number $2 | |
* - a ArrayList of Integer is not assignable to an ArrayList of Number | |
* even though an Integer is assignable to a Number | |
*/ | |
} | |
// to add to collection, we need to use super with ? | |
private static void simpleHeirarchy4(Collection<? super Number> numbers) { | |
// populate collection with some numbers | |
numbers.add(new Integer(10)); | |
numbers.add(new Float(20)); | |
numbers.add(new Double(30)); | |
Iterator<? super Number> iterator = numbers.iterator(); | |
for (Object object : numbers) { | |
} | |
} | |
private static void simpleHeirarchy3(Collection<? extends Number> numbers) { | |
// You can now pass in the following:- | |
// 1. Collection<Number> | |
// 1. Collection<Integer> | |
// 1. Collection<Float> etc | |
// you cannot add to the collection because you are not certain | |
// what type of collection was passed in i.e. was it a collection of | |
// Float or Integer | |
// if you try to add a Float to it and a Collection of Integer was | |
// passed in | |
// and since Float is not assignable to Integer, that could cause an | |
// error | |
for (Number number : numbers) { | |
System.out.println(number); | |
} | |
} | |
private static void simpleHeirarchy2(Collection<Number> numbers) { | |
// have the ability to print any collection of Number types | |
for (Number number : numbers) { | |
System.out.println(number); | |
} | |
} | |
private static void testCopy() { | |
Number[] arrayOfNumbers = { new Double(10), new Float(20), | |
new Integer(30) }; | |
ArrayList<Number> numbersCollection = new ArrayList<Number>(); | |
copy(arrayOfNumbers, numbersCollection); | |
} | |
private static <T extends Number> Collection<T> copy(T[] array, | |
Collection<T> objects) { | |
for (T object : array) { | |
objects.add(object); | |
} | |
return objects; | |
} | |
} | |
class Box<E> { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment