I have a similar logic for a method in a Java Class (not the real code, this is simplified for example purposes).
private Boolean method(Boolean booleanValue, SomeObject object) {
return booleanValue ? Arrays.asList(object.getStringsArray()).contains("string") : false;
}
A collaborator who assigned himself to check the PR gave the following comment:
This is inefficient. It is creating a new data structure only to iterate it and check if there is a certain string.
The getStringsArray() method returns a String[], so will using a for-loop be better than Arrays.asList()?
Which way is more efficient to achieve this?
Arrays.asListjust returns a wrapper, so that's not a problem.object.getStringsArraymight be a problem, depending on what that does and whether there might be better ways to check for this string.Listobjects are backed by an array, so there is no performance loss. Even the canonical answer for how to do this uses this method.Booleaninstead ofbooleanhere is a bad idea, andObjecthas nogetStringsArraymethod.