java.lang.IllegalStateException: 在使用列表时,流已被操作或关闭。

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

java.lang.IllegalStateException: stream has already been operated upon or closed in Java while working with list

问题

以下是你提供的代码翻译后的内容:

项目类:

public class Item {
    // ... (省略属性和构造函数)

    // 省略 getter 和 setter 方法

    public static void filterItemsWeight(Stream<Item> items) {
        items.map(t -> t.getWeightInGrams())
            .sorted(Comparator.reverseOrder())
            .forEach(item -> System.out.println(item));
    }

    @Override
    public String toString() {
        return "物品ID:" + this.getItemId() + ",物品名称:" + this.itemName + ",物品类型:" + this.itemType +
                ",重量(克):" + this.getWeightInGrams() + ",价格:" + this.getPrice();
    }

    public static void calculatePricePerItem(Stream<Item> itemsList) {
        Supplier<Stream<Item>> streamSupplier = () -> itemsList;
        streamSupplier.get().filter(t -> t.getItemType().equals(ItemType.GOLD))
            .forEach(item -> item.setPrice(item.getWeightInGrams() * 2700));
        itemsList.forEach(t -> System.out.println(t));
        itemsList.filter(t -> t.getItemType().equals(ItemType.SILVER))
            .forEach(item -> item.setPrice(item.getWeightInGrams() * 34));
        itemsList.forEach(t -> System.out.println(t));
    }
}

测试类:

public class Tester {
    public static void main(String[] args) {
        Item it1 = new Item(10, "戒指", ItemType.GOLD, 23.3f, 40656.5f);
        Item it2 = new Item(11, "项链", ItemType.GOLD, 45.3f, 140656.5f);
        Item it3 = new Item(12, "戒指", ItemType.SILVER, 25.3f, 956.5f);
        Item it4 = new Item(13, "硬币", ItemType.GOLD, 20.3f, 37656.5f);
        Item it5 = new Item(14, "手镯", ItemType.GOLD, 40f, 124200.5f);
        Item it6 = new Item(15, "手镯", ItemType.GOLD, 45.3f, 140656.5f);
        List<Item> itemList = new ArrayList<>();
        itemList.add(it1);
        itemList.add(it2);
        itemList.add(it3);
        itemList.add(it4);
        itemList.add(it5);
        itemList.add(it6);
        Supplier<Stream<Item>> streamSupplier = () -> itemList.stream();
        Item.filterItemsWeight(streamSupplier.get());
        Item.calculatePricePerItem(streamSupplier.get());
    }
}

请注意,由于代码中使用了实体名称和方法名,我进行了直接翻译,并保留了方法和属性的结构。如果你在中文代码中使用了其他命名约定,你可以根据需要进行调整。

英文:

I am having an items class and a tester class for ordering the items and setting price. Am using two methods with stream passed as parameter to the methods. But getting error stream has already been operated upon or closed while executing.

Item Class:

public class Item {
int itemId;
String itemName;
ItemType itemType;
float weightInGrams;
float price;
public Item(int itemId, String itemName, ItemType itemType, float weightInGrams, float price) {
this.itemId = itemId;
this.itemName = itemName;
this.itemType = itemType;
this.weightInGrams = weightInGrams;
this.price = price;
}
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public ItemType getItemType() {
return itemType;
}
public void setItemType(ItemType itemType) {
this.itemType = itemType;
}
public float getWeightInGrams() {
return weightInGrams;
}
public void setWeightInGrams(float weightInGrams) {
this.weightInGrams = weightInGrams;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public static void filterItemsWeight(Stream&lt;Item&gt; items) {
items.map(t-&gt; t.getWeightInGrams())
.sorted(Comparator.reverseOrder())
.forEach(item-&gt; System.out.println(item));
}
@Override
public String toString() {
return &quot;Item ID: &quot;+this.getItemId()+&quot; , Item Name : &quot;+this.itemName+&quot; , Item Type : &quot;+this.itemType+
&quot; , Weight in grams : &quot;+this.getWeightInGrams()+&quot; , Price : &quot;+this.getPrice();
}
public static void calculatePricePerItem(Stream&lt;Item&gt; itemsList) {
Supplier&lt;Stream&lt;Item&gt;&gt; streamSupplier = ()-&gt; itemsList;
streamSupplier.get().filter(t-&gt; t.getItemType().equals(ItemType.GOLD))
.forEach(item-&gt;item.setPrice(item.getWeightInGrams()*2700));
itemsList.forEach(t-&gt; System.out.println(t));
itemsList.filter(t-&gt; t.getItemType().equals(ItemType.SILVER))
.forEach(item-&gt;item.setPrice(item.getWeightInGrams()*34));
itemsList.forEach(t-&gt; System.out.println(t));
}

Tester Class

public class Tester {
public static void main(String[] args) {
Item it1 = new Item(10, &quot;Ring&quot;, ItemType.GOLD, 23.3f, 40656.5f);
Item it2 = new Item(11, &quot;Necklace&quot;, ItemType.GOLD, 45.3f, 140656.5f);
Item it3 = new Item(12, &quot;Ring&quot;, ItemType.SILVER, 25.3f, 956.5f);
Item it4 = new Item(13, &quot;Coin&quot;, ItemType.GOLD, 20.3f, 37656.5f);
Item it5 = new Item(14, &quot;Bangle&quot;, ItemType.GOLD, 40f, 124200.5f);
Item it6 = new Item(15, &quot;Bangle&quot;, ItemType.GOLD, 45.3f, 140656.5f);
List&lt;Item&gt; itemList = new ArrayList&lt;&gt;();
itemList.add(it1);
itemList.add(it2);
itemList.add(it3);
itemList.add(it4);
itemList.add(it5);
itemList.add(it6);
Supplier&lt;Stream&lt;Item&gt;&gt; streamSupplier = ()-&gt; itemList.stream();
Item.filterItemsWeight(streamSupplier.get());
Item.calculatePricePerItem(streamSupplier.get());
}

I have tried using supplier stream but still getting the same error for method calculatePricePerItem. Could you please help in resolving this?

答案1

得分: 0

之后

供应商<Stream<Item>> streamSupplier = () -> itemsList;
    streamSupplier.get().filter(t -> t.getItemType().equals(ItemType.GOLD))
    .forEach(item -> item.setPrice(item.getWeightInGrams() * 2700));

流被关闭。

因此下面这行
itemsList.forEach(t -> System.out.println(t))

在一个已经关闭的流上调用了forEach终端操作。这是被禁止的。

英文:

After that

Supplier&lt;Stream&lt;Item&gt;&gt; streamSupplier = ()-&gt; itemsList;
    streamSupplier.get().filter(t-&gt; t.getItemType().equals(ItemType.GOLD))
    .forEach(item-&gt;item.setPrice(item.getWeightInGrams()*2700));

The stream is closed.

So the following line
itemsList.forEach(t-&gt;System.out.println(t)

calls the foreach terminal operation on a already closed stream . It is forbidden.

huangapple
  • 本文由 发表于 2020年5月5日 11:55:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/61605504.html
匿名

发表评论

匿名网友

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

确定