You can not address a variable with its name as string (except with reflection, but that's an overkill for this task).
You can add all these ArrayList's to another List. This is more flexible and, thus, a better option.
List<List<ContactDetails>> lists2 = new ArrayList<List<String>>();
for (int i = 0; i < 10; i++) {
lists2.add(new ArrayList<ContactDetails>());
}
Or add them to an array and access them by array indexes:
@SuppressWarnings("unchecked")
List<ContactDetails>[] lists = new List[10];
for (int i = 0; i < lists.length; i ++) {
lists[i] = new ArrayList<ContactDetails>();
}
Now:
for (int i = 1; i < 5; i++)
{
lists[i].clear();
lists2.get(i).clear();
}
As both List and array are Iterable, you can use the foreach syntax:
for (List list : lists)
{
list.clear();
}
for (List list : lists2) {
list.clear();
}
If you know how to achieve the same with Lambdas / Stream API, please add to the answer.