我在arraylist中有一些数据。示例数据是
这七个数据在一个String类型的arraylist中。我想将arrayList从索引2迭代到6(从Smith到cranston),然后用空格分割这些迭代的数据,并且必须将分割值分配给使用Java流的另一个类中的单个变量。
有人可以帮我解决这个问题吗?
class Person {
private final String name;
private final List<Integer> values;
Person(String name, List<Integer> values) {
this.name = name;
this.values = values;
}
public String getName() {
return name;
}
public List<Integer> getValues() {
return values;
}
}
List<String> data = new ArrayList<>(Arrays.asList(
"These are the students mark details",
"Smith 99 87 88",
"Bravo 90 91 77",
"John 90 90 90",
"Gretchen 80 80 80",
"Bryan 70 70 70",
"Cranston 87 78 98",
"Thank you"));
List<Person> people = data.stream()
.skip(1)
.limit(6)
.map(line -> line.split(" "))
.map(values -> new Person(values[0], Stream.of(values)
.skip(1)
.mapToInt(Integer::parseInt)
.boxed()
.collect(Collectors.toList())))
.collect(Collectors.toList());