Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How transform String[] args with "=" split in java 8

I need to transform

String[] args = {"--path=C:/log", "--time=hourly"};

into

String[] args = {"--path", "C:/log", "--time", "hourly"};

How can I do this in Java 8, in an elegant way?

List<String> newArgs = Lists.newArrayList();

for (String s : args) {
    String[] split = s.split("=");
    newArgs.add(split[0]);
    newArgs.add(split[1]);
}

String[] strarray = new String[newArgs.size()];
return newArgs.toArray(strarray);
like image 731
Mister B Avatar asked Dec 03 '22 21:12

Mister B


1 Answers

String[] result = Stream.of(args)
        .flatMap(a -> Stream.of(a.split("=")))
        .toArray(String[]::new);
like image 186
Andrew Tobilko Avatar answered Dec 25 '22 12:12

Andrew Tobilko