Following more closely object-oriented approach, you can create decorators for classes TreeSet and HashMap. Then, instead of calling show(object), you would be calling object.show(), which is more clean IMHO and follows the OOP principles, such as Encapsulation and Abstraction:
public interface Showable {
void show();
}
public class ShowableTreeSet<E> extends TreeSet<E> implements Showable
{
private TreeSet<E> delegate;
public ShowableTreeSet(TreeSet<E> delegate) {
this.delegate = delegate;
}
public void show() {
for (E e : delegate) {
System.out.println(e);
}
}
public boolean add(E e) {
return delegate.add(e);
}
//all other delegate methods
}
public class ShowableHashMap<K, V> extends HashMap<K, V> implements Showable
{
private HashMap<K, V> delegate;
public ShowableHashMap(HashMap<K, V> delegate) {
this.delegate = delegate;
}
public void show(){
Iterator<K> iterator = delegate.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next().toString();
String value = delegate.get(key).toString();
System.out.println(key + " " + value);
}
}
public int size() {
return delegate.size();
}
//all other delegate methods
}
So then you would just have to wrap you showables around any actual object you would use:
ShowableTreeSet<String> sts = new ShowableTreeSet<String>(new TreeSet<String>());
sts.show();
or if you wanted just any Showable for printout, you could go:
Showable showable = new ShowableTreeSet<String>(new TreeSet<String>());
showable.show();
You could create these decorators to just about anything - Iterable, List, Collection, etc... and then use dependency injection to use whatever implementation of Collection (Iterable, List...) you happen to use.