How can I extend(resolve) a Path with a Stream of String — oracle-tech

    Forum Stats

  • 3,702,040 Users
  • 2,239,552 Discussions
  • 7,835,730 Comments

Discussions

How can I extend(resolve) a Path with a Stream of String

28033642803364 Posts: 10
edited November 2014 in Java Desktop Applications

I have following two variables;

<span class="kwd" style="color: #00008b; background: transparent;">final</span><span class="pln" style="background: transparent;"> </span><span class="typ" style="color: #2b91af; background: transparent;">Path</span><span class="pln" style="background: transparent;"> root</span><span class="pun" style="background: transparent;">;</span><span class="pln" style="background: transparent;">   </span><span class="com" style="color: #808080; background: transparent;">// /a/b/c</span><span class="pln" style="background: transparent;"> </span><span class="kwd" style="color: #00008b; background: transparent;">final</span><span class="pln" style="background: transparent;"> </span><span class="typ" style="color: #2b91af; background: transparent;">Stream</span><span class="pun" style="background: transparent;">&lt;</span><span class="typ" style="color: #2b91af; background: transparent;">String</span><span class="pun" style="background: transparent;">&gt;</span><span class="pln" style="background: transparent;"> split</span><span class="pun" style="background: transparent;">;</span><span class="pln" style="background: transparent;"> </span><span class="com" style="color: #808080; background: transparent;">// 'd', 'e', 'f'</span><span class="pln" style="background: transparent;">  </span><span class="typ" style="color: #2b91af; background: transparent;">Path</span><span class="pln" style="background: transparent;"> path</span><span class="pun" style="background: transparent;">;</span><span class="pln" style="background: transparent;">   </span><span class="com" style="color: #808080; background: transparent;">// /a/b/c/d/e/f</span>

I want to resolve every element in split to the root.

Currently I'm trying to do like this.

<span class="com" style="color: #808080; background: transparent;">// is the final combiner ok?</span><span class="pln" style="background: transparent;"> </span><span class="kwd" style="color: #00008b; background: transparent;">final</span><span class="pln" style="background: transparent;"> </span><span class="typ" style="color: #2b91af; background: transparent;">Path</span><span class="pln" style="background: transparent;"> path </span><span class="pun" style="background: transparent;">=</span><span class="pln" style="background: transparent;"> split</span><span class="pun" style="background: transparent;">.</span><span class="pln" style="background: transparent;">reduce</span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;">root</span><span class="pun" style="background: transparent;">,</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;">p</span><span class="pun" style="background: transparent;">,</span><span class="pln" style="background: transparent;"> s</span><span class="pun" style="background: transparent;">)</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">-&gt;</span><span class="pln" style="background: transparent;"> p</span><span class="pun" style="background: transparent;">.</span><span class="pln" style="background: transparent;">resolve</span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;">s</span><span class="pun" style="background: transparent;">),</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;">p1</span><span class="pun" style="background: transparent;">,</span><span class="pln" style="background: transparent;"> p2</span><span class="pun" style="background: transparent;">)</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">-&gt;</span><span class="pln" style="background: transparent;"> p1</span><span class="pun" style="background: transparent;">);</span>

Is this the right way? Is there any other way to do this?

Is following code better than the above code?

<span class="typ" style="color: #2b91af; background: transparent;">Path</span><span class="pln" style="background: transparent;"> path </span><span class="pun" style="background: transparent;">=</span><span class="pln" style="background: transparent;"> root</span><span class="pun" style="background: transparent;">;</span><span class="pln" style="background: transparent;"> split</span><span class="pun" style="background: transparent;">.</span><span class="pln" style="background: transparent;">forEach</span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;">s </span><span class="pun" style="background: transparent;">-&gt;</span><span class="pln" style="background: transparent;"> path </span><span class="pun" style="background: transparent;">=</span><span class="pln" style="background: transparent;"> path</span><span class="pun" style="background: transparent;">.</span><span class="pln" style="background: transparent;">resolve</span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;">s</span><span class="pun" style="background: transparent;">));</span><span class="pln" style="background: transparent;"> </span><span class="com" style="color: #808080; background: transparent;">// path must be final?</span>

Thanks.

This discussion has been closed.