Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要时逐个获取集合元素_Java_Collections_Java Stream - Fatal编程技术网

Java 需要时逐个获取集合元素

Java 需要时逐个获取集合元素,java,collections,java-stream,Java,Collections,Java Stream,我有一个类游戏,其中包含一个类板,其中包含一行的数组列表 当前游戏可以getCurrentRow(),当游戏进行时,它会执行getNextRow()。线路板从错误的一端获取currentRow,在ArrayList上循环,并在每一步将该行保存在lastCheckedRow中,并在找到currentRow时中断。下一步将成为lastCheckedRow。很简单,但很难看 我想将这种方法改为流。是否可以使流保持可访问性,在调用时一次只返回一个元素 public class Board impleme

我有一个类游戏,其中包含一个类板,其中包含一行的数组列表

当前游戏可以getCurrentRow(),当游戏进行时,它会执行getNextRow()。线路板从错误的一端获取currentRow,在ArrayList上循环,并在每一步将该行保存在lastCheckedRow中,并在找到currentRow时中断。下一步将成为lastCheckedRow。很简单,但很难看

我想将这种方法改为流。是否可以使流保持可访问性,在调用时一次只返回一个元素

public class Board implements Skinnable{

    private Stream<Skinnable> stream;

    protected List<Skinnable> rowItems = new ArrayList<>();

    private BoardRow currentRow;

    private final BoardSkin skin;

    public Board(Game game) {

        for (int i = 0; i < 10; i++) {
            rowItems.add(new BoardRow(game));
            if (i == 9) {
                setCurrentRow((BoardRow) rowItems.get(rowItems.size() - 1));
            }
        }

        stream = rowItems.stream();
        skin = new BoardSkin(this);
    }

    public void setCurrentRow(BoardRow row) {
        currentRow = row;
        currentRow.activate();
    }

    public Row getStreamedRowItem () {

        List<Skinnable> collect = stream.limit(1).collect(Collectors.toList());

        return (Row) collect.get(0);
    }
}
公共类板实现可蒙皮{
私有流;
受保护的列表行项目=新建ArrayList();
私人董事会;
私人最终木板皮;
公共棋盘(游戏){
对于(int i=0;i<10;i++){
添加(新的BoardRow(游戏));
如果(i==9){
setCurrentRow((BoardRow)rowItems.get(rowItems.size()-1));
}
}
stream=rowItems.stream();
皮肤=新木板皮肤(本);
}
公共无效setCurrentRow(BoardRow){
当前行=行;
currentRow.activate();
}
公共行getstreamdrowitem(){
List collect=stream.limit(1.collect)(Collectors.toList());
返回(行)收集。获取(0);
}
}

这样做,一次,然后关闭流。这是一个更一般的问题。我在这里和其他地方都搜索过,但这对我来说太新了,所以我甚至不能正确地表达我的问题,因此我被卡住了。

流只能遍历一次。因此,您无法将其保持打开状态,然后运行
.collect
终端操作

每次检索元素的最佳替代方法是使用迭代器:

private Iterator<Skinnable> stream; //Please rename the variable
检索下一个元素将更改为:

public Row getStreamedRowItem () {

    //you'll need to check if there's a next element
    //if(stream.hasNext())
    return stream.next();
}

如果我是你,我会遵循JB Nizet的想法,这会给你更多的控制空间/机会

由于您已经有了
行项目
,只需添加另一个
索引
,您就可以在
集合
中飞往任何您想要的地方

例如:

int index; // always pointing to the element to be fetched;

public Board(Game game) { {
    index = 0; // pointing to the first element;
    // or if you want it to point to the last using rowItems.size() - 1 instead;

}

public boolean hasNext() {
    return index < rowItems.size();
}

public Row next() {
    return (Row) rowItems.get(index++);
}

public boolean hasPrevious() {
    return index > -1;
}

public Row previous() {
    return (Row) rowItems.get(index--);
}
int索引;//始终指向要获取的元素;
公众棋盘(游戏){{
index=0;//指向第一个元素;
//或者,如果希望它指向最后一个,请改用rowItems.size()-1;
}
公共布尔hasNext(){
返回索引-1;
}
公共行上一行(){
返回(行)行项目。获取(索引--);
}

您所需要的只是一个迭代器,而不是流。但即使如此,如果您只是使用indexOf()来查找当前行的索引,而不是自己循环,或者如果您存储的是当前行的索引,而不是当前行本身(迭代器将在内部执行此操作),代码也会简单得多.GetStreamDrowItem是否应该始终返回第一个?或者像队列一样处理负载?@Lino测试时,它现在从第一个开始,但我打算从末尾开始,否则我将翻转插入行并从头开始。@jbniset是的,索引会工作得很好,我就是从这个开始的。尝试使用newer’s things to practice,所以我将其改为使用整个对象,希望“流式传输”“惰性地”在阅读了一些关于从生产者和消费者那里获取无限流条目的内容后,我认为这正是我所需要的。你们让我意识到我对这个过程感到困惑,而ernest_k的代码示例帮助我实现了一个迭代器。所以我的搜索方向不对。非常感谢你们在som方面的帮助e代码,已经可以运行了。需要重构一些,因为当next()说不再有行时,很难返回一行。next()和previous()会不会返回同一行,因为增量/减量在变量后面?++index和--index?我喜欢这种方法,它让我可以按照我认为流的方式使用行项。谢谢。@JohanLarsson实际上正如您所提到的,您可以调整它以满足您的需要:)但是您必须小心使用检查方法以保持关联d检查始终有效。此外,您还应该了解hasX()如何处理X()。
int index; // always pointing to the element to be fetched;

public Board(Game game) { {
    index = 0; // pointing to the first element;
    // or if you want it to point to the last using rowItems.size() - 1 instead;

}

public boolean hasNext() {
    return index < rowItems.size();
}

public Row next() {
    return (Row) rowItems.get(index++);
}

public boolean hasPrevious() {
    return index > -1;
}

public Row previous() {
    return (Row) rowItems.get(index--);
}