0

I have a custom object.

public class StatusItem {

    private final String key;
    private final String value;

    public StatusItem(String key, String value) {
        this.key = key;
        this.value = value;
    }

    public String getKey() {
        return key;
    }

    public String getValue() {
        return value;
    }
}

Then I have a long list of those objects.

List<StatusItem> statusItems;

Currently, I'm just iterating over them and acting on the matches I find.

for (StatusItem statusItem : mStatusItems()) {
        switch (statusItem.getKey()) {
            case "apple":
                doStuff();
                break;
            case "oranges":
                doSomethingElse();
                break;
}

Is this the optimal way of doing things? I'm interested to know whether there are more efficient ways of accomplishing the same thing or at least a way that results in less code.

I know I could use the Stream API or equivalent methods to obtain specific items in a list, but since I'm looking up multiple items I'm assuming it would be inefficient since it would effectively look through the entire list for every item instead of just iterating through it once like I currently do.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
ardevd
  • 3,329
  • 5
  • 30
  • 55

0 Answers0