Add Iterables.intersperse()

Issue #22 resolved
John Kozlov created an issue

Add a function intersperse, that takes an element and an Iterable and "intersperses" that element between the elements of the Iterable. For example:

Iterables.intersperse(Arrays.asList(1, 2, 3), 0)

will return 1, 0, 2, 0, 3.

Comments (2)

  1. John Kozlov reporter

    A good use case: you want to print multiple strings with a line between each two strings:

    List<String> lines = Arrays.asList("Line1", "Line2", "Line3");
    for (String s : intersperse(lines, "-------")) {
        System.out.println(s);
    }
    

    This will print:

    Line1
    -------
    Line2
    -------
    Line3
    
  2. Log in to comment