Compare a Path element to a string

Weijun Wang weijun.wang at oracle.com
Tue Mar 21 06:43:16 UTC 2017


Hi Alan

I'm profiling the new FilePermission implementation and found a lot of 
time spending on comparing a Path to a string, and the path has only one 
element.

Here are them:

1. Comparing with "-":

    Path lastName = npath.getFileName();
    if (lastName != null && lastName.toString().equals("-")) { ... }

2. Comparing with "":

    p = p2.relativize(p1).normalize();
    if (p.getName(0).toString().isEmpty()) { ... }

3. Comparing with "..":

    for (Path item: p) {
       String s = item.toString();
       if (!s.equals("..")) { ... }
    }

I can think of comparing with static final Path fields, but is there a 
even faster way?

Thanks
Max


More information about the core-libs-dev mailing list