I want to change some of the object properties inside a list without any loop in efficient way in Java 7, since in Java 8 we can use stream, please don't refer to any third party library like Guava, because i can't add any new library to company old system.
MenuResponse.java
public class MenuResponse {
String id;
String title;
String type;
// Getters and setters omitted for brevity
}
MenuService.java
public List<MenuResponse> getMenu() {
List<MenuResponse> data = new ArrayList<>();
/* sample local data */
MenuResponse a = new MenuResponse();
a.setId("1");
a.setTitle("Time Deposit");
a.setType("OK");
MenuResponse b = new MenuResponse();
b.setId("2");
b.setTitle("Account");
b.setType("OK");
MenuResponse c = new MenuResponse();
c.setId("3");
c.setTitle("Submission Wizard");
c.setType("DISABLED");
data.add(a);
data.add(b);
data.add(c);
/* here i want to alter the title menu to "Under Maintenance" if type = 'DISABLED' */
/* this is my best in Java 8 */
Optional<MenuResponse> dd = data.stream()
.filter(menu -> menu.getType().equalsIgnoreCase("DISABLED"))
.findFirst();
if(dd.isPresent()){
dd.get().setTitle("Under Maintenance");
}
return data;
}
Objective
Based on above snippet i want to update some of the array object properties, in this case i want to change the title of the MenuResponse to "Under Maintenance" in case if the type equals as "DISABLED", but without any loops.
Any help and explanation will be appreciated, thank you
DISABLED?c.setTitle("Under Maintenance")would work