tags:

views:

187

answers:

4

Hi, well, I have an ArrayList with this values

ACU
ACU
ACU
ACU
ACY
ACY
AER
AER
AER
AGC

I need to get the number of items of each Word, so for

  • ACU we will get 4,
  • ACY we will get 2,
  • AER we will get 3,
  • AGC we will get 1.

Generally the number a word is repeated is a variable so another time ACU could be 1,and ACY could be 100..

So., I have a class to keep the values "whatWord" AND "howMany"

public class Word {

private String whatWord;
private int howMany;

public  cVOPlaza(String whatWord, int  howMany){
  this.whatWord = whatWord;
  this.howMany= howMany;     
}

public String getwhatWord() {
  return whatWord;
}

public void setwhatWord(String whatWord) {
   this.whatWord = whatWord;
}

public int gethowMany() {
   return howMany;
 }

public void sethowMany(int howMany) {
   this.howMany = howMany;
 } 
}

I am stuck here, because I know the part get(i+1) in the following code will cause error, You know value does not exist, but then I dont know what to do...

ArrayList<Word> arrayWords = new ArrayList<Word>();
 int cuantos = 0;
     for (int i=0;i<OriginalList.size();i++) {
     String word1  = OriginalList.get(i).getWord();
     String word2 = OriginalList.get(i+1).getWord();
             if (word1.equals(word2)){
                       //this counter is bad here... 
                       //where do i increment it??
         howMany++;
         Word a = new Word(word1,howMany);
         ///....DONT KNOW WHERE TO ADD THE OBJECT 
                        //to the list
                         //arrayWords.add(a)
      }
        }

It is supposed that after the for code I will get

ACU 4,
ACY 2,
AER 3,
AGC 1.

First I tried to do a HashMap try, please help me with this code:

 HashMap table = new HashMap();
    int value=0;
    String key=null;

   //INITIALIZE HASH??? LIKE THIS
    for (int i = 0; i < OriginalList.size; i++) {
        table.put(0,OriginalList.get(i).getWord());      
    }


         String word1=null;
         ArrayList<Word> arrayWords = new ArrayList<Word>();
         //LOOP FOR SEARCHING
         for (int i = 0; i < OriginalList.size(); i++) {
               key = OriginalList.get(i).getWord();
               if (table.containsKey(key)) { 
                       word1 = (String) table.get(key);
                       value++;
               }else {
                      word1 = (String) table.get(key);
                      value=1
               }
             //Add result??
             Word a = new Word(word1,value);
         }

Thanks in advance.

+2  A: 

Iterate over OriginalList and put the word in a HashMap<String, Integer>. If it is not there start with count 1, otherwise increment it.

fastcodejava
+1 Good idea, but for counting the number I would suggest using AtomicInteger (since it's mutable).
Helper Method
@Helper: Well, actually AtomicXXXXX doesn't seem to do much better, see http://stackoverflow.com/questions/81346/most-efficient-way-to-increment-a-map-value-in-java
Kevin Brock
A: 

Why not set your loop counter at 1?

String word1  = OriginalList.get(i-1).getWord();
String word2 = OriginalList.get(i).getWord();

This way, you never run out of bounds.

Jason
+1  A: 

That might be overkill for what you want to do. You could more simply create a map that has the three-letter string as the key and the count as the value. Then just iterate over your ArrayList:

Map<String, Integer>wordCount = new HashMap<String, int>();
for(String seq : yourWordList){
    wordCount.put(seq, wordCount.get(seq++));
}
darren
Ok!!!, thanks for the help but still dont understand where to add to my ArrayList<Word> the element with the correct values,also have dounbt where you put "wordCount.get(seq++)"Could you explain this, ´please??Thanks
Edgar
So, wordCount will contain the values!!!, but what will do "wordCount.get(seq++)" ??
Edgar
wordCount.put(seq, wordCount.get(Integer.parseInt(seq)+1)); ????
Edgar
get an error in wordCount.put(seq, wordCount.get(seq++));"cannot convert from String to int"How to solve this, thanks!! :)
Edgar
If I delete de increment wordCount.put(seq, wordCount.get(seq));I get the list, but their values are nullACU = nullACY = nullPlease help
Edgar
Costly due to autoboxing, for better ways, see http://stackoverflow.com/questions/81346/most-efficient-way-to-increment-a-map-value-in-java.@darren: Also, your code should be `wordCount.put(seq, wordCount.get(seq)++);`
Kevin Brock
A: 

I think the answer your looking for is common-collections' CollectionUtils.getCardinalityMap().

JavaDoc: Returns a Map mapping each unique element in the given Collection to an Integer representing the number of occurrences of that element in the Collection.

So for example....

List<String> words = new ArrayList();
Map counts = CollectionUtils.getCardinalityMap(words);
Tim R