Created
January 17, 2012 14:20
-
-
Save wgroeneveld/1626779 to your computer and use it in GitHub Desktop.
Java: counting collections by folding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CollectionUtil { | |
public static interface EnkelvoudigeTeller<TellerType> { | |
boolean magMeegeteldWorden(TellerType object); | |
} | |
public static interface MeervoudigeTeller<TellerType> { | |
int geefAantalMeegeteld(TellerType object); | |
} | |
public static <TellerType> int tel(Collection<TellerType> objecten, MeervoudigeTeller<TellerType> teller) { | |
int geteld = 0; | |
for (TellerType object : objecten) { | |
geteld += teller.geefAantalMeegeteld(object); | |
} | |
return geteld; | |
} | |
public static <TellerType> int tel(Collection<TellerType> objecten, final EnkelvoudigeTeller<TellerType> teller) { | |
return tel(objecten, new MeervoudigeTeller<TellerType>() { | |
@Override | |
public int geefAantalMeegeteld(TellerType object) { | |
return teller.magMeegeteldWorden(object) ? 1 : 0; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment