HashSet order and difference with JDK 7 / 8

前端 未结 4 1582
悲&欢浪女
悲&欢浪女 2021-01-13 18:30

This is a two part question:

  1. Does HashSet implement some hidden ordering mechanic or it just, to quote the documentation:It makes no guarantees as to the
4条回答
  •  天命终不由人
    2021-01-13 19:02

    Ans 1 : Yes Hashset doesn't maintain insertion order but after that if you iterate it you will get same order every time.

    Ans 2: iteration result may differ with java versions because it depends on hashcode implementation of that version. but Hashset provides one surety that iteration order will never get change means after insertion of elements if you iterate it every time you will get same order in that java version.

提交回复
热议问题