Which way better?
if ("Pablo".equals(name) || "Marko".equals(name) || .. );
or
if (Sets.newHashSet("Pablo", "Marko", ..).contains(name));
Interest about memory using, performance, readabilty
Which way better?
if ("Pablo".equals(name) || "Marko".equals(name) || .. );
or
if (Sets.newHashSet("Pablo", "Marko", ..).contains(name));
Interest about memory using, performance, readabilty
The if statement will take linear time (O(n)), since it has n
conditions to evaluate at the worst case.
The Set.contains
variant will take linear time to construct the Set
(adding n
elements), but only constant time to search the Set
for a specific value.
Therefore, if you run this condition multiple times (for the same set of values),
creating the Set
once and reusing it multiple times will be more efficient than running the if statement multiple times.