I have a sorted list in java, i just want to split this list into sublists based on the first alphabet at each index of list. For example List contains
{
cal
Something like this could work.
private List> subListOnFirstLetter(List list) {
List> result = new ArrayList<>();
char first = list.get(0).charAt(0);
List subList = new ArrayList<>();
for (String element : list) {
char next = element.charAt(0);
if (next != first) {
result.add(subList);
subList = new ArrayList<>();
} else {
subList.add(element);
}
first = next;
}
return result;
}