0

How to convert Java comma delimited into Array of object using Java 8 Stream ?

 public static void main(String[] args){
    String INPUT = 
        "00:00:01,400-234-090\n" +
            "00:00:01,701-080-080\n" +
            "01:05:00,600-234-090";
    System.out.println(new Solution().solution(INPUT));

  }
mcryan
  • 19
  • 2
  • 5
    `INPUT.split(",")` – Naman Apr 24 '19 at 03:49
  • Please refer [String to array comma delimited](https://stackoverflow.com/questions/27599847/convert-comma-separated-string-to-list-without-intermediate-container) – Another coder Apr 24 '19 at 03:58
  • 1
    Possible duplicate of [convert comma separated string to list without intermediate container](https://stackoverflow.com/questions/27599847/convert-comma-separated-string-to-list-without-intermediate-container) – GBlodgett Apr 24 '19 at 03:59

2 Answers2

0

It's simply:

String[] strings = Arrays.stream(INPUT.split(","))
                         .toArray(String[]::new)

But for what purpose? You can do this action without any streams? Just call INPUT.split(",") and it will return String[]

Scrobot
  • 1,911
  • 3
  • 19
  • 36
0

You may perform by using Java Stream as below :

Arrays.stream(csvString.split("\r?\n"))
                .map(x -> x.split(","))
                .map(Arrays::asList)
                .map(PhoneCall::new) //Stream<PhoneCall>
                .collect(Collectors.groupingBy(x -> x.phoneNumber)) //Map<String, List<PhoneCall>>
                .values() 
                .stream()



    private static class PhoneCall {
        final String phoneNumber;
        final int hours;
        final int minutes;
        final int seconds;


        PhoneCall(List<String> values) {
            phoneNumber = values.get(1);
            String[] durationArray = values.get(0).split(":");
            hours = Integer.valueOf(durationArray[0]);
            minutes = Integer.valueOf(durationArray[1]);
            seconds = Integer.valueOf(durationArray[2]);
        }
    }
AzizSM
  • 6,199
  • 4
  • 42
  • 53