<div dir="ltr"><div>Argh, sorry I pasted the wrong example. The Widget fields "color" and "weight" are supposed to NOT be final:</div><div><br></div><div style="margin-left:40px"><span style="font-family:monospace">@@ -5,8 +5,8 @@<br> <br> public class Widget {<br> <br>- private final Color color;<br>- private final int weight;<br>+ private Color color;<br>+ private int weight;<br> <br> public Widget(Color color, int weight) {<br> this.color = color;</span></div><div><br></div><div>The question remains the same - is the program deterministic?</div><div><br></div><div>-Archie<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Nov 22, 2023 at 10:06 AM Archie Cobbs <<a href="mailto:archie.cobbs@gmail.com">archie.cobbs@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>I have an API design question about Streams, but instead of asking the question outright I thought I'd ask it indirectly with a simple example.<br></div><div><br></div><div>The Javadoc for <span style="font-family:monospace">Stream</span> opens with this example:</div><div><pre><code> int sum = widgets.stream()
.filter(w -> w.getColor() == RED)
.mapToInt(w -> w.getWeight())
.sum();<br></code></pre></div><div><div><span class="gmail_signature_prefix">OK, looks reasonable - we're computing the sum of the weights of all the red widgets.<br></span></div><div><span class="gmail_signature_prefix"><br></span></div><div><span class="gmail_signature_prefix">Here's the question: Is the following program deterministic?<br></span></div><div><span class="gmail_signature_prefix"><br></span></div><div style="margin-left:40px"><span style="font-family:monospace">import java.awt.*;<br>import java.util.function.*;<br>import java.util.stream.*;<br>import static java.awt.Color.*;<br><br>public class Widget {<br><br> private final Color color;<br> private final int weight;<br><br> public Widget(Color color, int weight) {<br> this.color = color;<br> this.weight = weight;<br> }<br><br> public Color getColor() {<br> return this.color;<br> }<br><br> public int getWeight() {<br> return this.weight;<br> }<br><br> public static int totalRedWeight(Widget... widgets) {<br> return Stream.of(widgets)<br> .filter(w -> w.getColor() == RED)<br> .mapToInt(w -> w.getWeight())<br> .sum();<br> }<br><br>// Test method<br><br> public static void main(String[] args) {<br> final Widget w1 = new Widget(RED, 123);<br> final Widget w2 = new Widget(BLUE, 456);<br> final int total = Widget.totalRedWeight(w1, w2);<br> System.out.println("Total red weight = " + total);<br> }<br>}</span></div><div><br></div><div>-Archie<br></div><div><span class="gmail_signature_prefix"><br></span></div><div><span class="gmail_signature_prefix">-- </span></div><div><div dir="ltr" class="gmail_signature">Archie L. Cobbs<br></div></div></div></div>
</blockquote></div><br clear="all"><br><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">Archie L. Cobbs<br></div>