英文:
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: 将 p1
、p2
、p3
、p4
封装到 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<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: need convert all Strings from List<String> allGroupsPoint
to List<Point[]> 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<Point[] x>
.
I tried this, but isn't work:
List<Point[]> test = allGroupsPoint.stream()
.map(s -> 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<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)) // or .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));
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)]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论