Methods Summary |
---|
private java.util.List | getList()
List list = new LinkedList();
list.add(3);
list.add("Blind");
list.add("Mice");
return list;
|
private java.util.List | getListOfStrings()
List<String> list = new LinkedList<String>();
list.add("Hello");
list.add("World");
list.add("How");
list.add("Are");
list.add("You?");
return list;
|
public static void | main(java.lang.String[] args)
GenericsTester tester = new GenericsTester();
try {
tester.testTypeSafeLists(System.out);
tester.testTypeSafeMaps(System.out);
tester.testTypeSafeIterators(System.out);
tester.testTypeSafeReturnValues(System.out);
List<Integer> ints = new LinkedList<Integer>();
ints.add(1);
ints.add(2);
ints.add(3);
tester.printList(ints, System.out);
/** Uncomment for an error
NumberBox<String> illegal = new NumberBox<String>();
*/
} catch (Exception e) {
e.printStackTrace();
}
|
public void | printList(java.util.List list, java.io.PrintStream out)
for (Iterator<?> i = list.iterator(); i.hasNext(); ) {
out.println(i.next().toString());
}
|
private void | printListOfStrings(java.util.List list, java.io.PrintStream out)
for (Iterator<String> i = list.iterator(); i.hasNext(); ) {
out.println(i.next());
}
|
public void | testTypeSafeIterators(java.io.PrintStream out)
List<String> listOfStrings = new LinkedList<String>();
listOfStrings.add("Happy");
listOfStrings.add("Birthday");
listOfStrings.add("To");
listOfStrings.add("You");
for (Iterator<String> i = listOfStrings.iterator(); i.hasNext(); ) {
String s = i.next();
out.println(s);
}
printListOfStrings(getListOfStrings(), out);
|
public void | testTypeSafeLists(java.io.PrintStream out)
List listOfStrings = getListOfStrings();
for (Iterator i = listOfStrings.iterator(); i.hasNext(); ) {
String item = (String)i.next();
// Work with that string
}
List<String> onlyStrings = new LinkedList<String>();
onlyStrings.add("Legal addition");
/**
* Uncomment these two lines for an error
onlyStrings.add(new StringBuilder("Illegal Addition"));
onlyStrings.add(25);
*/
|
public void | testTypeSafeMaps(java.io.PrintStream out)
Map<Integer, Integer> squares = new HashMap<Integer, Integer>();
for (int i=0; i<100; i++) {
squares.put(i, i*i);
}
for (int i=0; i<10; i++) {
int n = i*3;
out.println("The square of " + n + " is " + squares.get(n));
}
|
public void | testTypeSafeReturnValues(java.io.PrintStream out)
List<String> strings = getListOfStrings();
for (String s : strings) {
out.println(s);
}
|