Reputation: 577
In Javascript we can store values in array like
var arr=[];
arr["name"]="test";
arr["value"]="result";
console.log(arr["name"]);
above mentioned array is storing values like key value pair,my question is this we can achieve in java without using hashmap ? so that we can get value by key in java
Upvotes: 0
Views: 1569
Reputation: 3669
It's not possible. Arrays are Integer Index based in Java.
HashMap
is specifically designed for that purpose.
Upvotes: 0
Reputation: 4638
I mean you could achieve it that way, but it is much less efficient. Searching an array for a specific value will be of O(n) complexity while hashmaps only take O(1) in the best case scenario (with no chaining).
Upvotes: 2