如何使用Java 8 Stream将定界的Java逗号转换为对象数组?
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));
}
答案 0 :(得分:0)
这很简单:
String[] strings = Arrays.stream(INPUT.split(","))
.toArray(String[]::new)
但是出于什么目的?您可以在没有任何流的情况下执行此操作吗?只需调用INPUT.split(",")
,它将返回String[]
答案 1 :(得分:0)
您可以使用Java Stream来执行以下操作:
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]);
}
}