Good day!

ExecutorService executor = Executors.newFixedThreadPool(n); HashSet<Future<Double>> callableSet = new HashSet<Future<Double>>(n); for(;n>0;n--) { Double middleArgument = step/2.0 + step*(n-1); callableSet.add( executor.submit( new rectangleMethodStep( middleArgument , step) ) ); } 

In this code, rectangleMethodStep is Callable , which calculates some value. Every time I read from HashSet , I get the same sequence, with a different order. Based on the specifics of this collection, I do not understand why the order always changes?

After all, the order we have is determined by the hash code of the element, then the order must be constant

  • 3
    if hashcode is not redefined, it changes between runs if cho. - Andrew Frolov
  • one
    We read javadoc :> it makes the order of the set; in particular, it does not guarantee that the order will remain constant over time. That is, no one guarantees you the order of iteration over a set of elements. - Nofate
  • one
    @voipp, You have just described how the basket index is calculated in a hashmap by hash. And here's what you can read about the hashcode itself: / * It makes it possible to make it possible. This is a variable value between the launches and the implementation of the object between the launches. - Nofate
  • one
    What is incomprehensible to you in the phrase "this is a rule?" - Nofate
  • one
    Well, that is, the object hash does not have to be implemented exactly this way; in different implementations of the language this can change and it is better not to rely on it and not to depend on it. The only important thing is that during one run, the hash of the same object cannot change. - Nofate

0