Skip to main content
added 233 characters in body
Source Link
Juned Ahsan
  • 68.9k
  • 12
  • 101
  • 138

For a hashed collection such as HashSet, the key should be immutable, otherwise. Hashset uses hashing internally to decide the bucket to store the object. And also while retrieving the object it will use hash to find the object bucket. If you are changing the object after storing, it may lose link tochange the value associatedhashcode of the object and Set may not be able to your keyretrieve the correct object. If you need to change the object even after adding it to the collection then using a hashed collection is not a good choice. Rather go for Arraylist, but note that with ArrayList you will lose the advantage to retrieve the desired Student quickly, as it could be with a Set.

For a hashed collection such as HashSet, the key should be immutable, otherwise you may lose link to the value associated to your key. If you need to change the object even after adding it to the collection then using a hashed collection is not a good choice. Rather go for Arraylist, but note that with ArrayList you will lose the advantage to retrieve the desired Student quickly, as it could be with a Set.

For a hashed collection such as HashSet, the key should be immutable. Hashset uses hashing internally to decide the bucket to store the object. And also while retrieving the object it will use hash to find the object bucket. If you are changing the object after storing, it may change the hashcode of the object and Set may not be able to retrieve the correct object. If you need to change the object even after adding it to the collection then using a hashed collection is not a good choice. Rather go for Arraylist, but note that with ArrayList you will lose the advantage to retrieve the desired Student quickly, as it could be with a Set.

Source Link
Juned Ahsan
  • 68.9k
  • 12
  • 101
  • 138

For a hashed collection such as HashSet, the key should be immutable, otherwise you may lose link to the value associated to your key. If you need to change the object even after adding it to the collection then using a hashed collection is not a good choice. Rather go for Arraylist, but note that with ArrayList you will lose the advantage to retrieve the desired Student quickly, as it could be with a Set.