如何在Stream Java 8中进行计算操作?

huangapple go评论77阅读模式
英文:

How to do computational operations in Stream Java8?

问题

What I have:

List<String> allGroupsPoint = Arrays.asList(
                "1.0 1.0 1.0 4.0 5.0 4.0 5.0 1.0",
                "1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0",
                "1.0 1.0 1.0 4.0 5.0 4.0 5.0 1.0");

STEP 1: 需要通过我的方法 double[] dataParser.parse(String s)List<String> allGroupsPoint 中的所有字符串转换为 List<Point[]> arrayPoints

STEP 2: 对于每行,我会得到 double[] 数组:

double[] digits = dataParser.parse(String s);

每个数组的大小将为 8(因为每个字符串只有 8 个数字)。

STEP 3: 需要将这些 double 数组转换为对象 Point[],以获得 4 个对象(每个对象包含 2 个数字)。像这样:

Point p1 = new Point(digits[0], digits[1]);
Point p2 = new Point(digits[2], digits[3]);
Point p3 = new Point(digits[4], digits[5]);
Point p4 = new Point(digits[6], digits[7]);

STEP 4:p1p2p3p4 封装到 List<Point[]> x 中。

我尝试了以下代码,但它不能正常工作:

List<Point[]> test = allGroupsPoint.stream()
                .map(s -> dataParser.parseString(s))
                .map(Point[]::new)
                .collect(Collectors.toList());

我不知道如何在 Stream 中执行 STEP 3

英文:

What I have:

List&lt;String&gt; allGroupsPoint = Arrays.asList(
                &quot;1.0 1.0 1.0 4.0 5.0 4.0 5.0 1.0&quot;,
                &quot;1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0&quot;,
                &quot;1.0 1.0 1.0 4.0 5.0 4.0 5.0 1.0&quot;);

STEP 1: need convert all Strings from List&lt;String&gt; allGroupsPoint to List&lt;Point[]&gt; arrayPoints through my method double[] dataParser.parse(String s).
STEP 2: I get for each line arrays double[]:

double[] digits = dataParser.parse(String s)

The size of each array will be 8 (since there are only 8 digits from one String)

STEP 3: Need this arrays double convert to Object Point[] to get 4 objects (2 numbers per 1 object). Like this:

Point p1 = new Point(digits[0], digits[1]);
Point p2 = new Point(digits[2], digits[3]);
Point p3 = new Point(digits[4], digits[5]);
Point p4 = new Point(digits[6], digits[7]);

STEP 4: to wrap up p1, p2, p3, p4 to List&lt;Point[] x&gt;.

I tried this, but isn't work:

List&lt;Point[]&gt; test = allGroupsPoint.stream()
                .map(s -&gt; dataParser.parseString(s))
                .map(Point[]::new)
                .collect(Collectors.toList());

I don't know how in Stream do STEP 3.

答案1

得分: 1

试一试这个。

List<String> allGroupsPoint = Arrays.asList(
    "1.0 1.0 1.0 4.0 5.0 4.0 5.0 1.0",
    "1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0",
    "1.0 1.0 1.0 4.0 5.0 4.0 5.0 1.0");

List<Point[]> arrayPoints = allGroupsPoint.stream()
    .map(line -> dataParser.parse(line)) // 或者 .map(line -> dataParser.parseString(line))
    .map(array -> IntStream.range(0, array.length / 2)
        .mapToObj(i -> new Point(array[i * 2], array[i * 2 + 1]))
        .toArray(Point[]::new))
    .collect(Collectors.toList());

for (Point[] x : arrayPoints)
    System.out.println(Arrays.toString(x));

输出:

[Point(1.0, 1.0), Point(1.0, 4.0), Point(5.0, 4.0), Point(5.0, 1.0)]
[Point(1.0, 2.0), Point(3.0, 4.0), Point(5.0, 6.0), Point(7.0, 8.0)]
[Point(1.0, 1.0), Point(1.0, 4.0), Point(5.0, 4.0), Point(5.0, 1.0)]
英文:

Try this.

List&lt;String&gt; allGroupsPoint = Arrays.asList(
    &quot;1.0 1.0 1.0 4.0 5.0 4.0 5.0 1.0&quot;,
    &quot;1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0&quot;,
    &quot;1.0 1.0 1.0 4.0 5.0 4.0 5.0 1.0&quot;);

List&lt;Point[]&gt; arrayPoints = allGroupsPoint.stream()
    .map(line -&gt; dataParser.parse(line)) // or .map(line -&gt; dataParser.parseString(line))
    .map(array -&gt; IntStream.range(0, array.length / 2)
        .mapToObj(i -&gt; new Point(array[i * 2], array[i * 2 + 1]))
        .toArray(Point[]::new))
    .collect(Collectors.toList());

for (Point[] x : arrayPoints)
    System.out.println(Arrays.toString(x));

output:

[Point(1.0, 1.0), Point(1.0, 4.0), Point(5.0, 4.0), Point(5.0, 1.0)]
[Point(1.0, 2.0), Point(3.0, 4.0), Point(5.0, 6.0), Point(7.0, 8.0)]
[Point(1.0, 1.0), Point(1.0, 4.0), Point(5.0, 4.0), Point(5.0, 1.0)]

huangapple
  • 本文由 发表于 2020年8月31日 22:37:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63672885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定