Skip to content

Instantly share code, notes, and snippets.

@kevinpet
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinpet/64fdecea4ac7aec4338c to your computer and use it in GitHub Desktop.
Save kevinpet/64fdecea4ac7aec4338c to your computer and use it in GitHub Desktop.
Java void visitor
class PartitionFoos {
void doStuff(Collection<Foo> foos) {
final List<Bar> bars = new ArrayList<>();
final List<Baz> bazes = new ArrayList<>();
for (Foo f : foos) {
f.match(new Visitor<Void>() {
@Override
public Void caseBar(Bar b) {
bars.add(b);
return null;
}
@Override
public Void caseBaz(Baz b) {
bazes.add(b);
return null;
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment