A map stores values under unique keys. For account lookup, indexing accounts by ID avoids repeatedly scanning the collection; a benchmark found the HashMap approach much faster than the alternatives tested. section 7.1.3
Maps can also support tokenization: collect the words in medicine descriptions, assign each a dictionary index, then encode each description as a vector of word-presence values. Real tokenization adds normalization and typo handling, so a tokenizer library is recommended. section 4.6.2
For word-existence checks, a prebuilt map or cache trades lookup work for startup time and memory. Eager construction can reserve space for unused entries; lazy caching builds entries on demand and can evict stale data when the dataset or workload warrants it. section 7.4
this.accountMap = new HashMap<>(accounts.size() * 4 / 3 + 1); #A
for (Account account : accounts) {
accountMap.put(account.getId(), account);
}
#B
public Optional<Account> accountHashMap(Integer id) {
return Optional.ofNullable(accountMap.get(id));
}
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public Optional<Account> hashMapSampleTime() {
Integer id = searchIds.get(random.nextInt(searchIds.size()));
return accountFinder.accountHashMap(id);
}
@Benchmark
public boolean hashMapExists() {
Integer id = searchIds.get(random.nextInt(searchIds.size()));
return accountFinder.accountHashMap(id).isPresent();
}The listing shows accounts indexed by ID and benchmark methods for measuring lookup performance. section 7.1.3
















































