Last active
March 5, 2022 23:36
-
-
Save 1moita/d42caed55b4c9f691055aeb84daee477 to your computer and use it in GitHub Desktop.
Easy data caching in Java using ArrayList and generic type.
This file contains 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 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; | |
} | |
} |
This file contains 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 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