I keep getting java.lang.UnsupportedOperationException when trying to add a new item to the List e.g. Items.add(p); Could you help me in understanding why I am getting this exception?
import java.util.Arrays;
import java.util.List;
public class Item {
int id; int price;
public Item(int id, int price) {
this.id = id;
this.price = price;
}
@Override
public String toString() {
return id + ":" + price;
}
public static void main(String[] args) {
List<Item> Items = Arrays.asList(new Item(1, 30), new Item(2, 50), new Item(2, 40) );
Item p = Items.stream().reduce(new Item(4,0),(p1, p2) -> {
p1.price += p2.price;
return new Item(p1.id, p1.price);
});
System.out.println(p);
Items.add(p);
Items.stream().parallel().reduce((p1,p2) -> p1.price > p2.price?p1:p2).ifPresent(System.out::println);
}
}