View RSS Feed

Java Collection

Using a Collection vs. Using an Iterator

Rate this Entry
by , 11-28-2011 at 06:36 PM (1716 Views)
In this tip, we will look at determining the appropriate use of a collection versus using an iterator. One of the main reasons for using an interface is that you can create more generic and flexible code. By writing to an interface rather than an implementation, you can reuse your code for more types of objects.

The root interface for all sequence containers is Collection. The java.utiLAbstractCollection class provides a default implementation for a Collection, so that you can create a new subtype of AbstractCollection without duplicating the code. So if you write a method that takes a Collection that method can be applied to any type that implements Collection. So you can create a new class implementing the Collection interface order to be used with my method. The nice thing about Java is that the it expresses commonality not only via the Collection interface but also by the Collection providing an iterator( ) method:

Java Code:
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class IteratorExample {
	public static void display(Iterator<ComparableFeline> it) {
		while (it.hasNext()) {
			ComparableFeline c = it.next();
			System.out.print(((WildCat)c).id() + ":" + ((WildCat)c) + " ");
		}
		System.out.println();
	}
	
	public static void display(Collection<ComparableFeline> cats) {
		for(ComparableFeline c : cats) 
			System.out.print(((WildCat)c).id() + ":" + ((WildCat)c) + " ");
		System.out.println();
		}
	
	public static void main(String[] args) {
		List<ComparableFeline> wildCatList = ComparableFelines.arrayList(8); 
		Set<ComparableFeline> wildCatSet = new HashSet<ComparableFeline>(wildCatList); 
		Map<String,ComparableFeline> wildCatMap = new LinkedHashMap<String,ComparableFeline>();
		String[] names = ("Wildy, Beasty, Scratchy, Screamy, " +
		"Growly, Scaredy, Sneaky, Savage").split(", "); 
		for(int i = 0; i < names.length; i++)
		wildCatMap.put(names[i], 
		wildCatList.get(i)); 
		display(wildCatList); 
		display(wildCatSet); 
		display(wildCatList.iterator()); 
		display(wildCatSet.iterator()); 
		System.out.println(wildCatMap); 
		System.out.println(wildCatMap.keySet()); 
		display(wildCatMap.values()); 
		display(wildCatMap.values().iterator());
	}
}



You can see that the different versions of display( ) work equally well with Map objects and subtypes of Collection. , Both the Collection interface and the Iterator decouple the display( ) methods from knowing about the particular implementation of the underlying container. The other code tying this together is shown below.


Java Code:
public class WildCat extends ComparableFeline {
	private static int numberOfWildCats = 0;
	private int id;
	private String name;
	
	public WildCat() {
		super();
		id = ++numberOfWildCats;
		name = "wildcat" + id;
	}

	public String getName() {
		return name;
	}

	public int compareTo(ComparableFeline arg0) {
		return super.compareTo(arg0);
	}

	public int id() {
		return id;
	}
}

import java.util.ArrayList;

public class ComparableFelines {
	public static final CatCreator creator = new LiteralCatCreator();

	public static ComparableFeline randomCat() {
		return creator.randomCat();
	}

	public static ComparableFeline[] createArray(int size) {
		return creator.createArray(size);
	}

	public static ArrayList<ComparableFeline> arrayList(int size) {
		return creator.arrayList(size);
	}
}

Submit "Using a Collection vs. Using an Iterator" to Facebook Submit "Using a Collection vs. Using an Iterator" to Digg Submit "Using a Collection vs. Using an Iterator" to del.icio.us Submit "Using a Collection vs. Using an Iterator" to StumbleUpon Submit "Using a Collection vs. Using an Iterator" to Google

Comments