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.  
  10. /* Name of the class has to be "Main" only if the class is public. */
  11. class Ideone
  12. {
  13. public static void main(String[] args) {
  14. String[][] array = Arrays.stream(("John,Car,4324,4944\n"
  15. + "Jill & Peter,Bus,5433,6544\n"
  16. + "Greg,Bus,9384,4329\n"
  17. + "Jill & Greg and Bill,Truck,3213,4324\n"
  18. + "Mike,Bus,4324,3424\n"
  19. + "Greg & Lisa & John,bus,4324,4334").split("\n"))
  20. .map(s -> s.split(","))
  21. .toArray(String[][]::new);
  22. List<MyObject> result = Arrays.stream(array)
  23. .map(t -> Arrays.stream(t[0].split("\\s(&|and)\\s"))
  24. .map(v -> new MyObject(v, t[1], Integer.valueOf(t[2]), Integer.valueOf(t[3])))
  25. .collect(Collectors.toList())
  26. ).flatMap(List::stream)
  27. .collect(Collectors.toList());
  28.  
  29. result.forEach(System.out::println);
  30. }
  31. }
  32.  
  33. class MyObject {
  34.  
  35. private String s1;
  36. private String s2;
  37. private Integer i1;
  38. private Integer i2;
  39.  
  40. public MyObject(String s1, String s2, Integer i1, Integer i2) {
  41. this.s1 = s1;
  42. this.s2 = s2;
  43. this.i1 = i1;
  44. this.i2 = i2;
  45. }
  46.  
  47. //getters and setters
  48. @Override
  49. public String toString() {
  50. return "MyObject{" + "s1=" + s1 + ", s2=" + s2 + ", i1=" + i1 + ", i2=" + i2 + '}';
  51. }
  52.  
  53. }
  54.  
Success #stdin #stdout 0.15s 2184192KB
stdin
Standard input is empty
stdout
MyObject{s1=John, s2=Car, i1=4324, i2=4944}
MyObject{s1=Jill, s2=Bus, i1=5433, i2=6544}
MyObject{s1=Peter, s2=Bus, i1=5433, i2=6544}
MyObject{s1=Greg, s2=Bus, i1=9384, i2=4329}
MyObject{s1=Jill, s2=Truck, i1=3213, i2=4324}
MyObject{s1=Greg, s2=Truck, i1=3213, i2=4324}
MyObject{s1=Bill, s2=Truck, i1=3213, i2=4324}
MyObject{s1=Mike, s2=Bus, i1=4324, i2=3424}
MyObject{s1=Greg, s2=bus, i1=4324, i2=4334}
MyObject{s1=Lisa, s2=bus, i1=4324, i2=4334}
MyObject{s1=John, s2=bus, i1=4324, i2=4334}