5

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?

azurefrog
  • 10,785
  • 7
  • 42
  • 56
Alfredo Bejarano
  • 548
  • 1
  • 8
  • 22
  • `Arrays.asList` just returns a wrapper, so that's not a problem. `object.getStringsArray` might be a problem, depending on what that does and whether there might be better ways to check for this string. – user2357112 Jun 17 '16 at 16:22
  • 1
    horrible method. the first argument is totally bogus. – nucleon Jun 17 '16 at 16:22
  • 2
    I disagree with it being inefficient. `List` objects are backed by an array, so there is no performance loss. Even [the canonical answer for how to do this](http://stackoverflow.com/a/4962413/5743988) uses this method. – 4castle Jun 17 '16 at 16:22
  • 2
    We can't really say for sure whether your real code was good or bad, since it probably looked very different from this. Using `Boolean` instead of `boolean` here is a bad idea, and `Object` has no `getStringsArray` method. – user2357112 Jun 17 '16 at 16:24
  • Instead of a ternary operator, just use `return booleanValue && (code here);` – 4castle Jun 17 '16 at 16:32
  • 1
    This is not the real code, is more like a pseudo-code, the real method doesn't looks like this, Im not able to post the original code here. – Alfredo Bejarano Jun 17 '16 at 17:07
  • @4castle I thought about close-voting as a dup against that question, but the OP already has a working solution. I think this question is more about the efficiency of `Arrays.asList()` in particular, than how to search in general for a value in an array. – azurefrog Jun 17 '16 at 17:29
  • @azurefrog Perhaps, but the title is a bit misleading if someone were to search for this question. It would be nice if there was a banner at the top to point to the canonical answer. It's not a big deal though. I made sure it was seen in my comment on the answer. – 4castle Jun 17 '16 at 17:33

1 Answers1

6

Your co-worker is incorrect when they assert that your method is creating a new data structure.

If you look at the API for Arrays.asList(), it says that it

Returns a fixed-size list backed by the specified array.

There is no reason to write your own code to iterate over the array when you can just wrap a List around it, and use its built-in methods.

azurefrog
  • 10,785
  • 7
  • 42
  • 56
  • Hey Thank you! I was worried because I can't come up with another solution. Now with the API documentation I have proof about this is the best method. – Alfredo Bejarano Jun 17 '16 at 17:05
  • If you also want proof from another SO question, see [this popular answer](http://stackoverflow.com/a/1128728/5743988). This might actually be a duplicate of that question... – 4castle Jun 17 '16 at 17:18