SE450: Comment on implementing iterators [3/5] Previous pageContentsNext page

For InventorySet.iterator(), it's easy just return
Collections.unmodifiableCollection(_data.values()).iterator();

For InventorySet.iterator(Comparator c), it is a bit more delicate.
Here's one way:

 + Create a separate copy in a list, for example
   List l = new ArrayList(_data.values())
 + Sort the list using Collections.sort(List,Comparator)
 + Return an iterator over the sorted list

Previous pageContentsNext page