You can try something as follows:
final int size_split = 4;
List<List<String>> result = new ArrayList<>();
for(int i = 0; i < size_split; i++)
result.add(new ArrayList<>());
int count = 0;
for(String s : list){
result.get(count % size_split).add(s);
count++;
}
System.out.println(result);
First initialize the structure that will hold the four lists:
List<List<String>> result = new ArrayList<>();
for(int i = 0; i < size_split; i++)
result.add(new ArrayList<>());
Then iterate over the elements of the original list, and add them to the positions 0
, then 1
, then 2
, then 3
(and start over again) of the final list i.e., round-robin fashion:
int count = 0;
for(String s : list){
result.get(count % size_split).add(s);
count++;
}
A Running example:
public class Main {
public static void main(String[] arg) {
List<String> list = new ArrayList<>();
list.add("jdjdb");
list.add("jsid");
list.add("hsisi");
list.add("hri");
list.add("idt");
final int size_split = 4;
List<List<String>> result = new ArrayList<>();
for(int i = 0; i < size_split; i++)
result.add(new ArrayList<>());
int count = 0;
for(String i : list){
result.get(count % size_split).add(i);
count++;
}
System.out.println(result);
}
}
Output:
[[jdjdb, idt], [jsid], [hsisi], [hri]]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…