Skip to content

Instantly share code, notes, and snippets.

@1moita
Last active March 5, 2022 23:36
Show Gist options
  • Save 1moita/d42caed55b4c9f691055aeb84daee477 to your computer and use it in GitHub Desktop.
Save 1moita/d42caed55b4c9f691055aeb84daee477 to your computer and use it in GitHub Desktop.
Easy data caching in Java using ArrayList and generic type.
public final class Cache<T> extends ArrayList<T> {
protected T find(Predicate<T> predicate) {
for(T value : this)
if(predicate.test(value))
return value;
return null;
}
}
public class Example {
static class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
static class PersonCache extends Cache<Person> {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment