Example usage for java.util HashSet add

List of usage examples for java.util HashSet add

Introduction

In this page you can find the example usage for java.util HashSet add.

Prototype

public boolean add(E e) 

Source Link

Document

Adds the specified element to this set if it is not already present.

Usage

From source file:Main.java

public static <T> Set<T> toUniqueSet(T[] array) {
    HashSet<T> set = new HashSet<T>();

    if (array != null) {
        for (T object : array) {
            if (!set.contains(object)) {
                set.add(object);
            }//from  ww w  .  ja v  a2s  .  c  o m
        }
    }

    return set;
}

From source file:Main.java

public static <T> Set<Set<T>> extractSubSets(Set<T> initialSet, int subSetSize) {
    int nbSources = initialSet.size();
    int expectedNumberOfSets = expectedNumberOfSets(nbSources, subSetSize);
    Set<Set<T>> setOfSets = new HashSet<>(expectedNumberOfSets);
    if (nbSources == subSetSize) {
        // Already OK
        setOfSets.add(initialSet);//from   w w  w.j  a  va  2  s  .c o m
        return setOfSets;
    }
    List<T> setAsList = new ArrayList<>(initialSet);
    int[] iterators = new int[subSetSize];
    for (int i = 0; i < iterators.length; i++) {
        iterators[i] = i;
    }
    while (setOfSets.size() != expectedNumberOfSets) {
        HashSet<T> result = new HashSet<>(subSetSize);
        for (int pos : iterators) {
            result.add(setAsList.get(pos));
        }
        if (result.size() != subSetSize) {
            throw new IllegalStateException("Hard!");
        }
        setOfSets.add(result);
        int maxPos = -1;
        for (int i = 0; i < iterators.length; i++) {
            int pos = iterators[i];
            if (pos == (nbSources - iterators.length + i)) {
                maxPos = i;
                break;
            }
        }
        if (maxPos == -1) {
            // Up last iterator
            iterators[iterators.length - 1]++;
        } else if (maxPos == 0) {
            // Finished
            if (setOfSets.size() != expectedNumberOfSets) {
                System.err.println("Something wrong!");
            }
        } else {
            // Up the one before maxPos and reinit the others
            iterators[maxPos - 1]++;
            for (int i = maxPos; i < iterators.length; i++) {
                iterators[i] = iterators[i - 1] + 1;
            }
        }
    }
    return setOfSets;
}

From source file:models.TextInputCleaner.java

public static String clean(String input) {
    HashSet<String> stoplist = new HashSet<String>();
    for (int i = 0; i < stopwords.length; i++) {
        stoplist.add(stopwords[i]);
    }/*from  w  w  w. j  a  v  a 2  s  .  co  m*/
    String[] tokenSeq = input.toLowerCase().split("\\s");
    ArrayList<String> outputList = new ArrayList<String>();

    for (String str : tokenSeq) {
        if (!stoplist.contains(str)) {
            outputList.add(str);
        }
    }

    //String outStr = StringUtils.join(outputList, " ").replaceAll("[\\W_]+", "");
    String outStr = StringUtils.join(outputList, " ");

    return outStr;
}

From source file:Main.java

private static String[] convertCursorAsStringArrayWithCloseCursor(Cursor cursor, int colIdx) {
    String[] result = null;/*from  w ww  .  ja va 2s  .co  m*/
    try {
        int resultCount = cursor.getCount();
        if (resultCount > 0) {
            HashSet<String> phones = new HashSet<String>(resultCount);
            while (cursor.moveToNext()) {
                String phone = cursor.getString(0);
                phones.add(phone);
            }
            result = phones.toArray(new String[phones.size()]);
        }
        Log.d(TAG,
                "ConvertCursor As StringArray : found " + resultCount + " String converted from idx " + colIdx);
    } finally {
        cursor.close();
    }
    return result;
}

From source file:Main.java

/**
 * Creates policy tree stub containing two <code>PolicyNode</code>s
 * for testing purposes//from  ww w .  j  a  va2 s  .  co m
 *
 * @return root <code>PolicyNode</code> of the policy tree
 */
public static PolicyNode getPolicyTree() {
    return new PolicyNode() {
        final PolicyNode parent = this;

        public int getDepth() {
            // parent
            return 0;
        }

        public boolean isCritical() {
            return false;
        }

        public String getValidPolicy() {
            return null;
        }

        public PolicyNode getParent() {
            return null;
        }

        public Iterator<PolicyNode> getChildren() {
            PolicyNode child = new PolicyNode() {
                public int getDepth() {
                    // child
                    return 1;
                }

                public boolean isCritical() {
                    return false;
                }

                public String getValidPolicy() {
                    return null;
                }

                public PolicyNode getParent() {
                    return parent;
                }

                public Iterator<PolicyNode> getChildren() {
                    return null;
                }

                public Set<String> getExpectedPolicies() {
                    return null;
                }

                public Set<? extends PolicyQualifierInfo> getPolicyQualifiers() {
                    return null;
                }
            };
            HashSet<PolicyNode> s = new HashSet<PolicyNode>();
            s.add(child);
            return s.iterator();
        }

        public Set<String> getExpectedPolicies() {
            return null;
        }

        public Set<? extends PolicyQualifierInfo> getPolicyQualifiers() {
            return null;
        }
    };
}

From source file:Main.java

public final static Set<String> getMethods(Class<?> clazz) {
    HashSet<String> methodSet = new HashSet<String>();
    Method[] methodArray = clazz.getMethods();
    for (Method method : methodArray) {
        String methodName = method.getName();
        methodSet.add(methodName);
    }/* www .  ja  va 2 s.c  o  m*/
    return methodSet;
}

From source file:edu.cornell.mannlib.vitro.webapp.utils.fields.FieldUtils.java

public static List<Individual> removeIndividualsAlreadyInRange(List<Individual> individuals,
        List<ObjectPropertyStatement> stmts, String predicateUri, String objectUriBeingEdited) {
    HashSet<String> range = new HashSet<String>();

    for (ObjectPropertyStatement ops : stmts) {
        if (ops.getPropertyURI().equals(predicateUri))
            range.add(ops.getObjectURI());
    }/* w  w w.java 2 s .  co m*/

    int removeCount = 0;
    ListIterator<Individual> it = individuals.listIterator();
    while (it.hasNext()) {
        Individual ind = it.next();
        if (range.contains(ind.getURI()) && !(ind.getURI().equals(objectUriBeingEdited))) {
            it.remove();
            ++removeCount;
        }
    }

    return individuals;
}

From source file:Main.java

/**
 * Returns a list containing only unique values in a collection
 *///from w w w.j  a va2  s .  com
public static <T> List<T> getUnique(Collection<T> collection) {
    HashSet<T> set = new HashSet<>();
    List<T> out = new ArrayList<>();
    for (T t : collection) {
        if (!set.contains(t)) {
            out.add(t);
            set.add(t);
        }
    }
    return out;
}

From source file:com.evolveum.midpoint.model.common.stringpolicy.StringPolicyUtils.java

/**
 * Prepare usable list of strings for generator
 * //from  w  w  w . ja v  a 2s  . c om
 */

public static String collectCharacterClass(CharacterClassType cc, QName ref) {
    StrBuilder l = new StrBuilder();
    if (null == cc) {
        throw new IllegalArgumentException("Character class cannot be null");
    }

    if (null != cc.getValue() && (null == ref || ref.equals(cc.getName()))) {
        l.append(cc.getValue());
    } else if (null != cc.getCharacterClass() && !cc.getCharacterClass().isEmpty()) {
        // Process all sub lists
        for (CharacterClassType subClass : cc.getCharacterClass()) {
            // If we found requested name or no name defined
            if (null == ref || ref.equals(cc.getName())) {
                l.append(collectCharacterClass(subClass, null));
            } else {
                l.append(collectCharacterClass(subClass, ref));
            }
        }
    }
    // Remove duplicity in return;
    HashSet<String> h = new HashSet<>();
    for (String s : l.toString().split("")) {
        h.add(s);
    }
    return new StrBuilder().appendAll(h).toString();
}

From source file:com.evolveum.liferay.usercreatehook.password.StringPolicyUtils.java

/**
 * Prepare usable list of strings for generator
 *//*  w  w  w  .  j a  v a 2 s  .c  om*/

public static String collectCharacterClass(CharacterClassType cc, QName ref) {
    StrBuilder l = new StrBuilder();
    if (null == cc) {
        throw new IllegalArgumentException("Character class cannot be null");
    }

    if (null != cc.getValue() && (null == ref || ref.equals(cc.getName()))) {
        l.append(cc.getValue());
    } else if (null != cc.getCharacterClass() && !cc.getCharacterClass().isEmpty()) {
        // Process all sub lists
        for (CharacterClassType subClass : cc.getCharacterClass()) {
            // If we found requested name or no name defined
            if (null == ref || ref.equals(cc.getName())) {
                l.append(collectCharacterClass(subClass, null));
            } else {
                l.append(collectCharacterClass(subClass, ref));
            }
        }
    }
    // Remove duplicity in return;
    HashSet<String> h = new HashSet<String>();
    for (String s : l.toString().split("")) {
        h.add(s);
    }
    return new StrBuilder().appendAll(h).toString();
}