fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. import java.util.stream.Collectors;
  9. import java.util.stream.IntStream;
  10.  
  11. /* Name of the class has to be "Main" only if the class is public. */
  12. class Ideone
  13. {
  14. public static void main (String[] args) throws java.lang.Exception
  15. {
  16. List<String> list = Arrays.asList(
  17. "132567", "Amelia", " 123476", "Charlie", " 123516", "Emily", " 143456", "George", " 123466", "Harry", " 123457", "Jack", " 125456", "Joshua", " 132456", "Lily", " 123456", "Oliver"
  18. );
  19. List<String> names = IntStream.range(0, list.size())
  20. .filter(index -> index % 2 == 1)
  21. .mapToObj(index -> list.get(index))
  22. .collect(Collectors.toList());
  23.  
  24. List<String> ids = IntStream.range(0, list.size())
  25. .filter(index -> index % 2 == 0)
  26. .mapToObj(index -> list.get(index))
  27. .collect(Collectors.toList());
  28.  
  29. System.out.println(names);
  30. System.out.println(ids);
  31. }
  32. }
Success #stdin #stdout 0.13s 2184192KB
stdin
Standard input is empty
stdout
[Amelia, Charlie, Emily, George, Harry, Jack, Joshua, Lily, Oliver]
[132567,  123476,  123516,  143456,  123466,  123457,  125456,  132456,  123456]