2

I have a program that count occurrences of words in given array. It keeps words and its quantity. For example, in given array:

String array[] = {"cat", "dog", "cat"}; 

I have 2 cats, and 1 dog. Making it with HashMap is quite simple:

HashMap<String, Integer> map = new HashMap<>();
for (int i = 0; i < wordarray.length; i++) {
    String word = wordarray[i].toLowerCase();
    if (map.containsKey(word)) {
        map.put(word, map.get(word) + 1);
    } else {
        map.put(word, 1);
    }
}

Then I just need to print it out:

for (Map.Entry<String, Integer> entry : map.entrySet()) {
    System.out.println(entry.getKey() + " : " + entry.getValue());
}

But is there any way to make it without HashMap only using arrays of objects?

3
  • 3
    Two dimensions array?? Commented Jun 17, 2013 at 15:48
  • The reason why a HashMap is used is because it has unique keys like a Set. This way you can check if the String you are looking for exists, without having to loop over an entire Array. Commented Jun 17, 2013 at 15:53
  • Implement/reuse hash table with open addressing (en.wikipedia.org/wiki/Open_addressing)? Commented Jun 17, 2013 at 16:00

4 Answers 4

4

Create a custom class to hold your string and int values, and then use an array to hold them. In pseudo-code:

class Myclass
 public int myInt;
 public string MyString;
 //Constructor omited..


//Somewhere else..
MyClass[] my = new Myclass[2];
my[0] = new MyClass("string", 1);
Sign up to request clarification or add additional context in comments.

1 Comment

nice one :) I will try it!
3

You can create a list/array of Map.Entry key/value pairs.

There's already a method to do it for you in HashMap: entrySet()

http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html#entrySet()

1 Comment

Nope, that's lazy - a quality in the best programmers.
1

This much-much slower than using HashMap, but it should work:

Let's say, you sure that no word occurs in your text more than N times (as a upper bound you can choose total number of words in text). Then you can allocate array of that size N of ArrayList elements: a = new ArrayList[N]; for (int i = 0; i < N; i++) a[i] = new ArrayList<String>();

Then, for every word w, you will iterate over that array, find the cell with index c with ArrayList<String> which contains w and move w from c to c+1 (using list.remove(Object) and list.add(Object)). If no cell found, add w to the list in the first cell: a[0].add(w).

Some optimizations:

  • use HashSet in array instead of ArrayList, though it looks inappropriate in your case;
  • use array of arrays of [N][N] size. There will be more memory consumption, but less time spent.

Comments

1

you can check the implementation of Map using arrays. Here is a nice article about this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.