Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

260
Views
HashMap with List as a value in Typescript

I am learning TypeScript and am trying to create a collection similar to the following in Java

HashMap<String, List<String>> hashMap = new HashMap<String,List<String>>();

I am unable to find any examples . Is it possible to do this in Typescript? Is there something similar to this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

HashMap contains only unique keys. So you can use simple object and assign array to key:

let hash = {};
hash["bobtail"] = [];
hash["bobtail"].push({ age: 7, name: "miffy" });
console.log(hash)

Or try to use Record<Keys, Type>. As docs says:

Constructs an object type whose property keys are Keys and whose property values are Type. This utility can be used to map the properties of a type to another type.

interface CatInfo {
  age: number;
  name: string;
}
 
type CatBreed = "bobtail" | "birman" | "maine";
 
const cats: Record<CatBreed, CatInfo[]> = {
  bobtail: [{ age: 7, name: "miffy" }],
  birman: [{ age: 8, name: "moorzik" }],
  maine: [{ age: 17, name: "mordred" }],
};

UPDATE:

This is the way to add an item to Record<Keys, Type>:

cats["bobtail"].push({ age: 11, name: "miffy" });

This is the way to get items from Record<Keys, Type>:

const bobtails: CatInfo[] = cats["bobtail"];
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!