Tries can represent sparse arrays by distributing segments of the data into a single vector.
The Trie can determine if an element is present in the table with two read-only array indexings to get the effective position where an element is stored, or to know if it is absent from the underlying store.
Also, Tries provide a default position in the backing store for the default value of the sparsed array, so that you don't need any test on the returned index because the Trie guarantees that all possible source index will map at least to the default position in the backing store (where you'll frequently store a zero, or an empty string or a null object).
Tries are much faster than hashmaps because they don't need any complex hashing function and don't need to handle collisions for reads. Additionally, Java Hashmaps can only index on Objects, and creating an Integer object for each hashed source index is costly in memory operations as it stresses the garbage collector.
The above is the detailed content of How can Tries be used to efficiently represent sparse arrays?. For more information, please follow other related articles on the PHP Chinese website!