当前位置: 首页 > 知识库问答 >
问题:

Java.lang.IllegalStateException:在使用list[duplicate]时,已经在Java中对stream进行了操作或关闭

广绪
2023-03-14
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的相同错误。你能帮我解决这个问题吗?

共有1个答案

通飞尘
2023-03-14

在那之后

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)

 类似资料: