HashMap example - 1

23 July 2008

Following example creates a new HashMap object where keys are String and values are Integer.

        HashMap<String, Integer> wordcount = new HashMap<String, Integer>();
 
        try {
            // Opening file
            // change "/Users/anyexample/input.txt" to path to your test file 
            BufferedReader in = new BufferedReader(new FileReader(
                    "/Users/input.txt"));
            // string buffer for file reading  
            String str;
 
            // reading line by line from file   
            while ((str = in.readLine()) != null) {
                str = str.toLowerCase(); // convert to lower case
 
                // starting index, we'll use this to copy words from string
                int idx1 = -1;
                // process each characters 
                for (int i = 0; i < str.length(); i++) {
                    // trigger condition if current character is not letter
                    // or it is the end of line 
                    if ((!Character.isLetter(str.charAt(i))) || (i + 1 == str.length())) {
                        // do nothing if previous character was also non-letter
                        if (i - idx1 > 1) {
                            // copy word from input string buffer to new variable 
                            // from previous non-letter symbol 
                            // to current symbol which is also non-letter
 
                            // if this is a letter(than it is last character in the line 
                            // and we should copy it to word)  
                            if (Character.isLetter(str.charAt(i)))
                                i++;
 
                            // copying... 
                            String word = str.substring(idx1 + 1, i);
 
                            // Check if word is in HashMap
                            if (wordcount.containsKey(word)) {
                                // get number of occurrences for this word
                                // increment it 
                                // and put back again 
                                wordcount.put(word, wordcount.get(word) + 1);
                            } else {
                                // this is first time we see this word, set value '1'
                                wordcount.put(word, 1);
                            }
                        }
 
                        // remember current position as last non-letter symbol                        
                        idx1 = i;
                    }
                }
            }
            // Close buffered reader
            in.close();
        } catch (Exception e) {
            // If something unexpected happened 
            // print exception information and quit 
            e.printStackTrace();
            System.exit(1);
        }

continued…

del.icio.us:HashMap example - 1  digg:HashMap example - 1  spurl:HashMap example - 1  wists:HashMap example - 1  simpy:HashMap example - 1  newsvine:HashMap example - 1  blinklist:HashMap example - 1  furl:HashMap example - 1  reddit:HashMap example - 1  fark:HashMap example - 1  blogmarks:HashMap example - 1  Y!:HashMap example - 1  smarking:HashMap example - 1  magnolia:HashMap example - 1  segnalo:HashMap example - 1  gifttagging:HashMap example - 1

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: HashMap example - 1

Leave a Reply