HashSet.remove() and Iterator.remove() not working

前端 未结 10 1507
借酒劲吻你
借酒劲吻你 2020-12-13 00:32

I\'m having problems with Iterator.remove() called on a HashSet.

I\'ve a Set of time stamped objects. Before adding a new item to the Set, I loop through the set, i

10条回答
  •  萌比男神i
    2020-12-13 00:45

    Are you absolutely certain that DataResult is immutable? What is the type of the timestamp? If it's a java.util.Date are you making copies of it when you're initializing the DataResult? Keep in mind that java.util.Date is mutable.

    For instance:

    Date timestamp = new Date();
    DataResult d = new DataResult(timestamp);
    System.out.println(d.getTimestamp());
    timestamp.setTime(System.currentTimeMillis());
    System.out.println(d.getTimestamp());
    

    Would print two different times.

    It would also help if you could post some source code.

提交回复
热议问题