FileDocCategorySizeDatePackage
GenericsTester.javaAPI DocExample3249Wed May 05 15:02:30 BST 2004com.oreilly.tiger.ch02

GenericsTester

public class GenericsTester extends Object

Fields Summary
Constructors Summary
public GenericsTester()

  
Methods Summary
private java.util.ListgetList()

    List list = new LinkedList();
    list.add(3);
    list.add("Blind");
    list.add("Mice");

    return list;
  
private java.util.ListgetListOfStrings()

    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 voidmain(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 voidprintList(java.util.List list, java.io.PrintStream out)

    for (Iterator<?> i = list.iterator(); i.hasNext(); ) {
      out.println(i.next().toString());
    }
  
private voidprintListOfStrings(java.util.List list, java.io.PrintStream out)


    for (Iterator<String> i = list.iterator(); i.hasNext(); ) {
      out.println(i.next());
    }
  
public voidtestTypeSafeIterators(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 voidtestTypeSafeLists(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 voidtestTypeSafeMaps(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 voidtestTypeSafeReturnValues(java.io.PrintStream out)

    List<String> strings = getListOfStrings();
 
    for (String s : strings) {
      out.println(s);
    }