Skip to content

Instantly share code, notes, and snippets.

@yurique
Created Aug 22, 2012
Embed
What would you like to do?
java version
class Person {
private String firstName;
private String lastName;
private Address address;
public Person() {
}
public Person(String firstName, String lastName, Address address) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getAddress() {
return address;
}
public String setFirstName(String firstName) {
this.firstName = firstName;
}
public String setLastName(String lastName) {
this.lastName = lastName;
}
public String setAddress(Address address) {
this.address = address;
}
}
class Address {
private String city;
private String street;
...
public Address() {
}
public Address(String city, String street) {
this.city = city;
this.street = street;
}
public String getCity() {
return city;
}
public String setCity(String city) {
this.city = city;
}
public String getStreet() {
return ...;
}
public String setStreet(String street) {
this.... = ...
}
...
}
...
List<Persons> persons = getAListSomewhere();
List<Persons> filtered = doFilter(persons, new Filter<Person>() {
public boolean filter(Person p) {
return p.getAddress().getCity().equals("Kyiv");
}
});
List<T> doFilter(List<T> list, Filter<T> filter) {
List<T> result = new ArrayList<T>(list.size());
for (T t : list) {
if (filter(t)) {
result.add(t);
}
}
return result;
}
Set<Address> extractedAddresses = doExtract(list, new Extractor<Person,Address>() {
public Address extract(Person p) {
return p.address;
}
})
List<Out> doExtract<In,Out>(List<In> list, Extractor<In,Out> extractor) {
...
for (In x : list) {
...
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment