英文:
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<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));
}
Tester Class
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());
}
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<Stream<Item>> streamSupplier = ()-> itemsList;
streamSupplier.get().filter(t-> t.getItemType().equals(ItemType.GOLD))
.forEach(item->item.setPrice(item.getWeightInGrams()*2700));
The stream is closed.
So the following line
itemsList.forEach(t->System.out.println(t)
calls the foreach
terminal operation on a already closed stream . It is forbidden.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论