Historical Collection Classes (before JDK1.1)
The containers provided are arrays,vector,stack,hashtable,properties,bitset. It defines a standard way of visiting elements within a cluster, called the enumeration interface, as follows:
Vector v=new vector ();
For (enumeration enum =v.elements (); enum.hasmoreelements ();) {
Object o = enum.nextelement ();
Processobject (o);
}
In the JDK1.2 version, the iterator interface was introduced, and the new version of the collection object (Hashset,hashmap,weakheahmap,arraylist,treeset,treemap, LinkedList) is a collection element accessed through the iterator interface.
For example: List list=new ArrayList ();
For (iterator It=list.iterator (); It.hasnext ();)
{
System.out.println (It.next ());
}
This will make an error if you run the old version of the program on the new Java compiler. Because there is no elements () in the list interface, there is only iterator (). So how can the old version of the program run on the new Java compiler? If not modified, it is certainly not, but the change to follow the "open-closed" principle.
This is when I think of the adapter pattern in Java design mode.
/*
* @author I set the date for J Mad 2007-4-18
*
*/
Package net.blogjava.lzqdiy;
Import java.util.ArrayList;
Import java.util.Enumeration;
Import Java.util.Iterator;
Import java.util.List;
Public class Newenumeration implements enumeration
{
Iterator it;
Public Newenumeration (iterator it)
{
This.it=it;
TODO auto-generated Constructor stub
}
public boolean hasmoreelements ()
{
TODO auto-generated Method Stub
return It.hasnext ();
}
Public Object nextelement ()
{
TODO auto-generated Method Stub
return It.next ();
}
public static void Main (string[] args)
{
List list=new ArrayList ();
List.add ("a");
List.add ("B");
List.add ("C");
For (enumeration E=new newenumeration (List.iterator ()); E.hasmoreelements ();)
{
System.out.println (E.nextelement ());
}
}
}
Newenumeration is an adapter class that enables adaptation from the iterator interface to the enumeration interface, so that we can use the old version of the code to use the new collection object.