string to arraylist split , 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");
      // creating Spliterator of ArrayList and using spliterator() method
      Spliterator<String> iterate = al.spliterator();
      System.out.println("ArrayList of colors: ");
      // forEachRemaining method of Spliterator
      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); // prints "[5, 3, 1, 2]"
System.out.println(tail); // prints "[9, 5, 0, 7]"

Collections.sort(head);
System.out.println(numbers); // prints "[1, 2, 3, 5, 9, 5, 0, 7]"

tail.add(-1);
System.out.println(numbers); // prints "[1, 2, 3, 5, 9, 5, 0, 7, -1]"

Tags:

Java Example