用空格分割数据,并且必须使用JAVA流将分割后的值分配给另一个类中的单个变量

问题描述 投票:-1回答:1

我在arraylist中有一些数据。示例数据是

  1. 这些是学生分数的详细信息
  2. 史密斯99 87 88
  3. Bravo 90 91 77
  4. 约翰90 90 90
  5. Gretchen 80 80 80
  6. 布莱恩70 70 70
  7. Cranston 87 78 98

这七个数据在一个String类型的arraylist中。我想将arrayList从索引2迭代到6(从Smith到cranston),然后用空格分割这些迭代的数据,并且必须将分割值分配给使用Java流的另一个类中的单个变量。

有人可以帮我解决这个问题吗?

java stream
1个回答
0
投票
        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());
© www.soinside.com 2019 - 2024. All rights reserved.