Java Array Remove removeCommonWords(String[] words)

Here you can find the source of removeCommonWords(String[] words)

Description

Returns a new String array with some of the most common English words removed.

License

LGPL

Declaration

public static final String[] removeCommonWords(String[] words) 

Method Source Code

//package com.java2s;

import java.util.*;

public class Main {
    /**/*from   w w w.  j a  va 2s .com*/
     * Initialization lock for the whole class. Init's only happen once per
     * class load so this shouldn't be a bottleneck.
     */
    private static Object initLock = new Object();
    /**
     * A list of some of the most common words. For searching and indexing, we
     * often want to filter out these words since they just confuse searches.
     * The list was not created scientifically so may be incomplete :)
     */
    private static final String[] commonWords = new String[] { "a", "and", "as", "at", "be", "do", "i", "if", "in",
            "is", "it", "so", "the", "to" };
    private static Map commonWordsMap = null;

    /**
     * Returns a new String array with some of the most common English words
     * removed. The specific words removed are: a, and, as, at, be, do, i, if,
     * in, is, it, so, the, to
     */
    public static final String[] removeCommonWords(String[] words) {
        // See if common words map has been initialized. We don't statically
        // initialize it to save some memory. Even though this a small savings,
        // it adds up with hundreds of classes being loaded.
        if (commonWordsMap == null) {
            synchronized (initLock) {
                if (commonWordsMap == null) {
                    commonWordsMap = new HashMap();
                    for (int i = 0; i < commonWords.length; i++) {
                        commonWordsMap.put(commonWords[i], commonWords[i]);
                    }
                }
            }
        }
        // Now, add all words that aren't in the common map to results
        ArrayList results = new ArrayList(words.length);
        for (int i = 0; i < words.length; i++) {
            if (!commonWordsMap.containsKey(words[i])) {
                results.add(words[i]);
            }
        }
        return (String[]) results.toArray(new String[results.size()]);
    }
}

Related

  1. removeAll(T[] items, T item)
  2. RemoveArgs(String[] args, int startIndex)
  3. removeAt(T[] array, int index)
  4. removeByPrefix(String[] array, String prefix)
  5. removeByteOrderMark(final byte[] input)
  6. removeElement(final E[] array, final int index)
  7. removeElement(Object[] array, Object entryToRemove)
  8. removeElement(String array[], String element, int occurrences)
  9. removeElement(String[] array, String remove)