Barak
Barak

Reputation: 107

Java - list indexed by text

I am looking a way yo storage array like this:

array['name'] or array.add('name','value').

arrayList doesnt work like that. What's the best way to storage values like that?

Upvotes: 0

Views: 84

Answers (1)

Jon Newmuis
Jon Newmuis

Reputation: 26502

Use a Map. A HashMap is a good choice:

Map<String, String> map = new HashMap<String, String>();

// to add name/value pairs - like your array.add('name','value')
map.put("name", "value");

// to retrieve values - like your array['name']
map.get("name");

Upvotes: 6

Related Questions