Forum Stats

  • 3,768,635 Users
  • 2,252,825 Discussions
  • 7,874,661 Comments

Discussions

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

2803364
2803364 Member Posts: 10
edited Nov 26, 2014 8:38AM 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;"><</span><span class="typ" style="color: #2b91af; background: transparent;">String</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;"> </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;">-></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;">-></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;">-></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.