Search code examples
scalaconstructorhashsetcapacityload-factor

Initializing initial capacity and load factor of immutable HashSet in Scala


Is there an easy way to initialize initial capacity and load factor of HashSet in Scala? In Java it's very easy, for example:

public HashSet set = new HashSet(1 << 8, 0.6f)

I'd like to know, if there is any equivalent of this in Scala.

I also know that it's easy to use Javas HashSet instead simply by importing java.util.HashSet, but I'm curious if same is possible with scala.collection.immutable.HashSet

EDIT. I've checked from Scala API and HashSet source code, but didn't find anything useful.


Solution

  • Hashsets comes with two flawors in scala.

    mutable.HashSet is pretty like java HashSet and it has sizeHint method which could be used on any (e.g. empty) collection to resize current table.

    immutable.HashSet has different approach. It is implemented via hash trie algorithm instead of hash table so as JimH mentioned methods like sizeHint are meaningless for it.