0

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

4
  • 1
    Without any loop, you have iterate?? Commented Jun 30, 2020 at 5:14
  • Do you want to change only one object's property or all the objects' properties whose type is DISABLED? Commented Jun 30, 2020 at 5:14
  • Well, c.setTitle("Under Maintenance") would work Commented Jun 30, 2020 at 8:41
  • i think iteration is just the same thing like loop, by the way my java 8 code above is works well, now how to do it in java 7, only one object not all Commented Jun 30, 2020 at 12:08

3 Answers 3

1

You can use peek() function:

data.stream()
    .filter(menu -> menu.getType().equalsIgnoreCase("DISABLED"))
    .peek(menuResponse -> menuResponse.setTitle("Under Maintenance"))
    .collect(Collectors.toList());

with map() function:

data.stream()
    .filter(menu -> menu.getType().equalsIgnoreCase("DISABLED"))
    .map(menuResponse -> {
        menuResponse.setTitle("Under Maintenance");
        return menuResponse;
    })
    .collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

Comments

1

It's not very efficient (becuase whole list is sorted), and looks strange, but it's without loop and streams :)

    // 1. Let's sort list by type, ascending.
    Collections.sort(data, new Comparator<MenuResponse>(){
        @Override
        public int compare(MenuResponse o1, MenuResponse o2) {
            return o1.getType().compareTo(o2.getType());
        }
    });        
    // DISABLED
    // OK
    // OK

    // 2. Let's update title of DISABLED element
    if (data.size()>0 && data.get(0).getType().equalsIgnoreCase("DISABLED")) {
        data.get(0).setTitle("Under Maintenance");
    }

1 Comment

why you put loop there, my question is without any loop...
1

One way to achieve it is by overriding equals() (and it is recommended to override hashCode() along with it) in MenuResponse based on just type as follows:

@Override
public int hashCode() {
    return Objects.hash(type);
}

@Override
public boolean equals(Object obj) {
    if (obj == null)
        return false;
    MenuResponse other = (MenuResponse) obj;
    return Objects.equals(type, other.type);
}

Then, you can remove the first occurance of the object with type, DISABLED as shown below:

MenuResponse o = new MenuResponse();
o.setType("DISABLED");
int index = data.indexOf(o);
if (index != -1) {
    data.remove(index);
}

1 Comment

i don't want to remove it, i want to update the title...please see my snippet comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.