Java の奇妙な動作が見られますSet
。これは、私が問題を抱えている非常に単純なコード スニペットです。
public Set<StopSequence> reduce() {
Set<StopSequence> stopSequences = new HashSet<StopSequence>();
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<StopSequence> iterator = stopSequences.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
return stopSequences;
}
コンソール出力を見ると、問題が表示されます。
最初の の出力System.out.println
、すべて問題ないようです:
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
2 番目の出力System.out.println
、 my Set
only には同じオブジェクトが含まれています。何が起こっている??
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