Kotlin和parallelStream toArray

时间:2017-09-03 12:53:09

标签: kotlin java-stream

我想我已经陷入了困境。我出于性能原因试图利用Java parallelStream。

函数 Specimen.pick()示例并返回 Specimen 的实例。 我想在替换 pool 时使用parallelStream对此进行并行化。

var pool: Array<Specimen> = Array(100_000) .. 

这就是我在Kotlin写的内容:

pool = pool.asList().parallelStream().map { Specimen.pick(pool, wheel, r.split()) }.toArray(Specimen::new)

:: new

上出现了哪些错误

相反,我必须在列表和数组之间来回徘徊:

pool = pool.asList().parallelStream().map { Specimen.pick(pool, wheel, r.split()) }.collect(Collectors.toList()).toTypedArray()

哪个有效,但似乎资源浪费而不是直接进入Array。如果我让IntelliJ尝试Kotlinize这样的Java示例:

爪哇:

Person[] men = people.stream()
                      .filter(p -> p.getGender() == MALE)
                      .toArray(Person[]::new);

IntelliJ转换:

val men = people.stream()
            .filter({ p -> p.getGender() === MALE })
            .toArray(Person[]::new  /* Currently unsupported in Kotlin */)

所以也许Kotlin即将到来?还是有另一种更好的方法吗?

1 个答案:

答案 0 :(得分:4)

您不能使用数组构造函数引用,但每个方法引用都可以使用lambda表达式表示:

import QtQuick 2.7
import QtQuick.Window 2.0
import QtQuick.Controls 2.1

Window {
    id:container
    width: 800
    height: 800
    visible: true

    Component {
        id: rect
        Rectangle {
            property bool imParent: false
            x: 50 + Math.round(Math.random() * 550)
            y: 50 + Math.round(Math.random() * 550)
            width: 100 + Math.round(Math.random() * 100)
            height: 100 + Math.round(Math.random() * 100)
            color: Qt.rgba(Math.random(),Math.random(),Math.random(),1);
            Drag.active: dragArea.drag.active
            MouseArea {
                id: dragArea
                anchors.fill: parent
                drag.target: parent
            }
            Text {
                anchors.centerIn: parent
                text: imParent ? "I'm parent" : "Drag me"
                color: "white"
            }
        }
    }

    Rectangle {
        id: blk
        x: 10
        y: 10
        z: 100
        parent: null
        height: 50
        width: 50
        radius: 5
        border.color: "white"
        color: "black"

    }

    Repeater {
        id: reptr
        model: 5
        property int pos: 0
        Loader {
            id: loader
            sourceComponent: rect
            onLoaded: {
                if(blk.parent == null) {
                    blk.parent = loader.item;
                    loader.item.imParent = true;
                }
            }
        }
    }

    Row {
        anchors.horizontalCenter: container.contentItem.horizontalCenter
        spacing: 2
        Button {
            text: "Reparent relative to parent"
            onClicked: {
                reptr.pos ++;
                if(reptr.pos >= reptr.model) {
                    reptr.pos = 0;
                }
                var item = reptr.itemAt(reptr.pos).item;
                blk.parent.imParent = false;
                blk.parent = item;
                blk.parent.imParent = true;
            }
        }
        Button {
            text: "Reparent relative to scene"
            onClicked: {
                reptr.pos ++;
                if(reptr.pos >= reptr.model) {
                    reptr.pos = 0;
                }
                var item = reptr.itemAt(reptr.pos).item;
                var coord = blk.mapToGlobal(blk.x, blk.y);
                blk.parent.imParent = false;
                blk.parent = item;
                blk.parent.imParent = true;
                coord = blk.mapFromGlobal(coord.x, coord.y);
                blk.x = coord.x;
                blk.y = coord.y;
            }
        }
    }
}