how to split an arraylist in java code example
Example 1: ArrayList spliterator() method in java
import java.util.ArrayList;
import java.util.Spliterator;
public class ArrayListSpliteratorMethodExample
{
public static void main(String[] args)
{
ArrayList<String> al = new ArrayList<String>();
al.add("red");
al.add("blue");
al.add("green");
al.add("yellow");
al.add("orange");
Spliterator<String> iterate = al.spliterator();
System.out.println("ArrayList of colors: ");
iterate.forEachRemaining((n) -> System.out.println(n));
}
}
Example 2: split arraylist items java
List<Integer> numbers = new ArrayList<Integer>(
Arrays.asList(5,3,1,2,9,5,0,7)
);
List<Integer> head = numbers.subList(0, 4);
List<Integer> tail = numbers.subList(4, 8);
System.out.println(head);
System.out.println(tail);
Collections.sort(head);
System.out.println(numbers);
tail.add(-1);
System.out.println(numbers);