Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store key value pairs in Java without using HashMap or any other collection?

I want to be able to associate values to some keys without using collections. I am aware of HashMaps, but I am trying to explore a more basic way to do it.

For example, if I was to count the frequency of each word in a file, I would take words as keys and frequency as values.

Some languages allow accessing arrays using values as well . For example, Lua.

I also want to be able to access every key with its value.

like image 566
simar kaur Avatar asked Oct 23 '25 18:10

simar kaur


1 Answers

Have two arrays of equal size

String [] keys = new String [5];
String [] values = new String [5];

keys [0] = "name";
values [0] = "Fred";

String getValue (String key) {

   // loop around keys array to get index of key

   return values [index];
}
like image 196
Scary Wombat Avatar answered Oct 26 '25 06:10

Scary Wombat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!