4

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.

Tot Zam
  • 8,406
  • 10
  • 51
  • 76
simar kaur
  • 211
  • 3
  • 7
  • 14
  • 1
    Do you mean you want to implement your own associative array that's not a hash map? There are many different map structures. Do some research, pick one and implement it. If you run into problems, ask for help. – Misha Apr 27 '15 at 04:46
  • Well, in Java, the "primitives" don't do much. You have classes for everything. Such as the Collections in this case. The "most basic way" would be to just use them. – Thilo Apr 27 '15 at 04:48
  • @Misha: Yes, I meant exactly that :) – simar kaur Apr 27 '15 at 04:50
  • 1
    @simarkaur Binary search tree is a good "starter" map you can build yourself. – Misha Apr 27 '15 at 04:54
  • @Misha: Thank you for the direction.It sounds good. . I will do the due research to get that working this way.Thanks – simar kaur Apr 27 '15 at 04:57

5 Answers5

4

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];
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
3

HashMap IS basic.

Using two parallel arrays is extremely inefficient. For each word that you read in, you need to search through the keys array, find the index where the word is stored, and then go to the same place in the values array and increase the value in that location by 1.

It doesn't make sense to use a 2D array either, since you are trying to store a String word and an int count frequency, and they are different data types.

If your were counting how many times certain numbers appeared in a document, then you can easily use a single array, but not if you are counting Strings.

In your case, a HashMap is really the ideal data structure to use to keep track of the data. Unless it is a requirement to not use Collections, I would suggest at least trying HashMap.

Scanner file = new Scanner(new File("filename.txt"));
Map<String, Integer> map = new HashMap<String, Integer>();
while (file.hasNext()) {
    String key = file.next();
    Integer value = map.get(key);
    if (value == null) {
        map.put(key, 1);
    }
    else {
        map.put(key, ++value);
    }
}
Tot Zam
  • 8,406
  • 10
  • 51
  • 76
3

If you need to store some Pairs, what about using the Java class Pair? It's really straight forward:

new Pair<String, String>("key", "value").getValue();

There is also the class AbstractMap.SimpleEntry

More on:

dont reinvent the wheel!

Thomas Decaux
  • 21,738
  • 2
  • 113
  • 124
0

Create a wrapper class to handle that:

public class MyKeyValueClass {
    public String key;
    public String value;

    public MyKeyValueClass(String key, String value) {
        this.key = key;
        this.value = value;
    }
}

That is a base class.

Tot Zam
  • 8,406
  • 10
  • 51
  • 76
Francis Nduba Numbi
  • 2,499
  • 1
  • 11
  • 22
0

This could be a good Interview question, where the interviewer wants to know a way to implement key - value pairs without using HashMap or any other collection.

Here, I am storing frequencies of all characters in a string

static void countFrequencies(String str) 
{
  int n = str.length(); //here we are storing frequencies of all characters in string
  int[] count = new int[MAX_CHAR];

  for (int i = 0; i < n; i++)
    count[str.charAt(i) - 'a']++;
}

or could use classes, the base class would look like: -

class Key { 
      int freq; // store frequency of character 
      char ch; 
      Key(int val, char c)  
      { 
          freq = val;  
          ch = c; 
      } 
}  
adiga
  • 34,372
  • 9
  • 61
  • 83
Manny
  • 134
  • 2
  • 15