Comparable Example
[ Home ] [ Up ] [ Vector/Enumeration Example ] [ ArrayList/Iterator Example ] [ LinkedList Example ] [ Stack Example ] [ Comparable Example ] [ Comparator Example ] [ HashSet Example ] [ TreeSet Example ] [ TreeSet with Comparator ] [ HashMap Example ] [ TreeMap Example ] [ TreeMap with Comparator ]

 

 

Here's the ComparableSorter example, translated to using generics:

package examples;

import java.util.Collections;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ComparableSorter
{
  public static void main(String[] args)
  {
    List<String> list = new ArrayList<String>();
    list.add("abc");
    list.add("DEF");
    list.add("ghi");

    // Do the sort
    Collections.sort(list);

    // See results
    Iterator<String> iter = list.iterator();
    while (iter.hasNext())
    {
      String item = iter.next(); // NO cast required
      System.out.println(item);
    }
  }
}

which outputs (same as original):

DEF
abc
ghi

 

This page was last modified on 27 November, 2007