Reputation: 145
I have an object I searched for and said object is stored in a stream. I'd like to give the user the ability to modify the elements within the existing stream and update the existing list with the modified elements, instead of creating a new list altogether. The question is similar to this one, except the user is adding the modified elements to a new list
Here's what I have so far. I'm currently getting this error
Operator '+' cannot be applied to 'void', 'void'
If you need the entire code, or the inclusion of other classes, please let me know.
Main Class
else if (choice == 2){
System.out.println("\nEnter the name of the movie you would like to modify (Case Sensitive): ");
String search = scan.nextLine();
Optional<Item> item = items.stream()
.map(x->{
if(x.getItems()==null){
return Collections.singletonList(x);
}
x.getItems().getItem().add(x);
return x.getItems().getItem();
})
.flatMap(Collection::stream)
.filter(x -> x.getName().equals(search))
.peek(x-> x.setItems(null))//Peek exists mainly to support debugging
.findFirst();
System.out.println(item.toString());
System.out.println("\nEnter the new datatype of the movie (Number): ");
String userChoice = scan.nextLine();
System.out.println("\nEnter the new name of the movie: ");
String userChoice1 = scan.nextLine();
System.out.println("\nEnter the new data ID for the movie (Number): ");
int userChoice2 = scan.nextInt();
scan.nextLine();
System.out.println("\nEnter the new group for the movie: ");
String userChoice3 = scan.nextLine();
Collection::stream =
item.map(s -> s.setDataType(userChoice) + s.setName(userChoice1) + s.setData(userChoice2) + s.setGroup(userChoice3));
}
Items Class
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
@XmlRootElement(name = "items")
public class Items {
private List<Item> item;
@XmlElement(name = "item")
public List<Item> getItem(){
return item;
}
public void setItem(List<Item> itemList){
this.item = itemList;
}
@Override
public String toString() {
return "\n" + item ;
}
}
Item Class
import javax.xml.bind.annotation.XmlElement;
public class Item {
private String dataType;
private String name;
private int data;
private String group;
private Items items;
@XmlElement
public String getDataType(){
return dataType;
}
public void setDataType(String dataType){
this.dataType = dataType;
}
@XmlElement
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
@XmlElement
public int getData(){
return data;
}
public void setData(int data){
this.data = data;
}
@XmlElement
public String getGroup(){
return group;
}
public void setGroup(String group){
this.group = group;
}
public Items getItems() {
return items;
}
public void setItems(Items items) {
this.items = items;
}
@Override
public String toString() {
return
"\n\tDataType: " + dataType +
"\n\tName: " + name +
"\n\tData: " + data +
"\n\tGroup: " + group +
"\n\tItems: " + items + "\n";
}
public String singleObject(){
return
"\n\tDataType: " + dataType +
"\n\tName: " + name +
"\n\tData: " + data +
"\n\tGroup: " + group;
}
}
Upvotes: 3
Views: 616
Reputation: 434
In item.map()
statement you are calling setter which returns void hence you are getting issue. Void is not valid return type for map method. + Operator can also not be applied to void operand.
Couple of observations
if(choice == 2)
Items class
contain List<Item>
Below is the sample code I tried
It finds the movie name entered by user in Items and modifies it there instead of creating a completely new list.
. items.getItem().stream()
.filter(x -> x.getName.equals(search))
.forEach(x -> {x.setName(userInputName); x.setDatType(userInputDataType)});
Upvotes: 1
Reputation: 434
You have written very complicated code. Couple of Observations :
Anyway below is code snippet, hope it helps.
items.getItem().stream().filter(x -> x.getName.equals(search)).forEach(x -> {x.setName(userInputName); x.setDatType(userInputDataType)});
Upvotes: 0
Reputation: 3285
I see that in your main function you ask the user to provide the name of the movie and then about the fields to modify. So after asking the user all the questions, you can use the following operation:
items.stream()
// search for the movie by name
.filter(x -> x.getName().equals(search))
.findFirst()
// if present then modify it
.ifPresent(i -> {
i.setDataType(userChoice);
i.setName(userChoice1);
i.setData(userChoice2);
i.setGroup(userChoice3);
i.setItems(null);
});
In this way the list is modified in place.
Upvotes: 1