Hi Friend

Using HashMap in Java

***************************************

/*
 *
input a character array from the user and output in the following way.
example string is amazon.. then output should be a2m1z1o1n1
 */

package questionaire;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;

public class Amazon1 {
    private String str;
    private HashMap<Character, Integer> checkMap;

    public Amazon1(String s) {
        this.str = s;
        checkMap = new HashMap<Character, Integer>();
    }

    public void checkTheFrequency() {
        for (int i = 0; i < str.length(); i++) {
            char key = str.charAt(i);

            if (checkMap.containsKey(key)!=false) {
                int value = checkMap.get(key);
                checkMap.put(key, ++value);
            } else {
                checkMap.put(key, 1);
            }
        }
        /*
         * VVI
         */
        Set<Entry<Character,Integer>> entry = checkMap.entrySet();
        Iterator<Entry<Character,Integer>> iter = entry.iterator();
        while(iter.hasNext())
        {
            Entry pair = iter.next();
            System.out.print(" "+pair.getKey()+pair.getValue()+" ");

        }
    }

    public static void main(String[] args) {
        Amazon1 test = new Amazon1("amazon");
        test.checkTheFrequency();

    }

}

*******************

Correct me if I am wrong.

On Fri, May 14, 2010 at 3:30 PM, divya jain <sweetdivya....@gmail.com>wrote:

> use binary tree and insert in it every character u come across. if the
> character is already present then increment its count. use this approach if
> u r nt sure that characters will be only 26 or no.
> if u r sure there r 26 char then u cn use hash..
> plz correct me if i m wrong.
> thanks
>
>
> On 14 May 2010 08:50, sharad kumar <aryansmit3...@gmail.com> wrote:
>
>> cant u use a hash map ???? of O(K) where K is distinct elements in
>> string......
>>
>>
>> On Thu, May 13, 2010 at 8:13 PM, jalaj jaiswal <jalaj.jaiswa...@gmail.com
>> > wrote:
>>
>>> input a character array from the user and output in the following way.
>>> example string is amazon.. then output should be a2m1z1o1n1
>>>
>>> i did it taking a 26 size array... some better solution ??
>>>
>>>
>>> --
>>> With Regards,
>>> Jalaj Jaiswal
>>> +919026283397
>>> B.TECH IT
>>> IIIT ALLAHABAD
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Algorithm Geeks" group.
>>> To post to this group, send email to algoge...@googlegroups.com.
>>> To unsubscribe from this group, send email to
>>> algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups.com>
>>> .
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>
>>
>>
>> --
>> yezhu malai vaasa venkataramana Govinda Govinda
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to algoge...@googlegroups.com.
>> To unsubscribe from this group, send email to
>> algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups.com>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algoge...@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to