I have a question of how, if possible, one can generate methods automatically by only providing variable of a spefic kind.
Let's illustrate my question with simple example: Assuming I have multiple variables linked to LinkedLists and I would like to give each certain methods; for example setters and getters methods:
private LinkedList football = new LinkedList();
private LinkedList jogging = new LinkedList();
public LinkedList getFootball() {
return football;
}
public void setFootball(LinkedList football) {
this.football = football;
}
public LinkedList getJogging() {
return jogging;
}
public void setJogging(LinkedList jogging) {
this.jogging = jogging;
}
If number of variables gets long (example only contains 2), this will produce very long and rather repetetive peace of code, illustrated above.
Is there any way, if this is indeed a possability, to generalize the above pattern so I can create both methods by just creating an instance of LinkedList?
P.S.: using foreach loop and generalizing it for arbitrary element does NOT WORK.
Map
instead of an object. Possible with anenum
as a key. Otherwise, Lombok is IMHO the only sane solution. +++ Note thatLinkedList
is nearly always much slower thanArrayList
.