I want to split list of strings into multiple list based on input value dynamically using java program.
For eg.
If Im having the below list of string
List<String> messages = Arrays.asList("Hello", "World!", "How", "Are", "You");
I have to split the list of string into multiple lists with the condtion if i entered 2 as input value each splited list should contain 2 values in it.
Note: How many values the list should contain be based on input value
outputshould be:
list1 contains-> Hello,world
list2 contains -> How,Are
list3 contains -> you
As another answer suggests List.subList() is the easiest way. I’d use Math.min to cover the last partition case.