Note that there are some explanatory texts on larger screens.

plurals
  1. POSet of custom objects contains the same object
    primarykey
    data
    text
    <p>I am seeing some weird behaviour of Java <code>Set</code>. Here is the very simple code snippet I'm having issues with:</p> <pre><code>public Set&lt;StopSequence&gt; reduce() { Set&lt;StopSequence&gt; stopSequences = new HashSet&lt;StopSequence&gt;(); StopSequence currentSequence = null; for (StopTime stopTime: mStopTimes) { if (stopTime.getStopSequence() == 0) { //beginning of a new sequence if (currentSequence != null) { if (stopSequences.add(currentSequence)) { System.out.println("Added sequence:"); System.out.println(currentSequence.toString()); } currentSequence.clear(); } else { currentSequence = new StopSequence(); } currentSequence.add(stopTime.getStopId()); } else { currentSequence.add(stopTime.getStopId()); } } Iterator&lt;StopSequence&gt; iterator = stopSequences.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } return stopSequences; } </code></pre> <p>Now have a look at the console output and you will see the problem:</p> <p>Output of the first <code>System.out.println</code>, everything seems fine:</p> <pre><code>Added sequence: com.bviproject.gtfsobjects.StopSequence@d48dbbe6 Added sequence: com.bviproject.gtfsobjects.StopSequence@d5e1c8a1 Added sequence: com.bviproject.gtfsobjects.StopSequence@c98eb21 Added sequence: com.bviproject.gtfsobjects.StopSequence@79baf504 Added sequence: com.bviproject.gtfsobjects.StopSequence@6617f5f6 Added sequence: com.bviproject.gtfsobjects.StopSequence@7d7554ff </code></pre> <p>Output of the second <code>System.out.println</code>, my <code>Set</code> only contains the same object. What is happening??</p> <pre><code>com.bviproject.gtfsobjects.StopSequence@dd017ba7 com.bviproject.gtfsobjects.StopSequence@dd017ba7 com.bviproject.gtfsobjects.StopSequence@dd017ba7 com.bviproject.gtfsobjects.StopSequence@dd017ba7 com.bviproject.gtfsobjects.StopSequence@dd017ba7 com.bviproject.gtfsobjects.StopSequence@dd017ba7 com.bviproject.gtfsobjects.StopSequence@dd017ba7 com.bviproject.gtfsobjects.StopSequence@dd017ba7 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload