gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:54:55 28 4
gpt4 key购买 nike

我有一个项目类和一个测试器类,用于订购项目和设置价格。我使用两种方法,并将流作为参数传递给方法。但执行时获取错误流已被操作或关闭。

项目类别:

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<Item> items) {
items.map(t-> t.getWeightInGrams())
.sorted(Comparator.reverseOrder())
.forEach(item-> System.out.println(item));
}
@Override
public String toString() {
return "Item ID: "+this.getItemId()+" , Item Name : "+this.itemName+" , Item Type : "+this.itemType+
" , Weight in grams : "+this.getWeightInGrams()+" , Price : "+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, "Ring", ItemType.GOLD, 23.3f, 40656.5f);
Item it2 = new Item(11, "Necklace", ItemType.GOLD, 45.3f, 140656.5f);
Item it3 = new Item(12, "Ring", ItemType.SILVER, 25.3f, 956.5f);
Item it4 = new Item(13, "Coin", ItemType.GOLD, 20.3f, 37656.5f);
Item it5 = new Item(14, "Bangle", ItemType.GOLD, 40f, 124200.5f);
Item it6 = new Item(15, "Bangle", 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());

}

我尝试使用供应商流,但方法calculatePricePerItem仍然遇到相同的错误。您能帮忙解决这个问题吗?

最佳答案

之后

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)

在已关闭的流上调用 foreach 终端操作。这是被禁止的。

关于java.lang.IllegalStateException : stream has already been operated upon or closed in Java while working with list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61605504/

28 4 0