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:biz.c24.io.spring.util.C24Utils.java

/**
 * Returns all the classes supported by the given model {@link Element}.
 * /*from  ww  w. ja va  2 s  . c o  m*/
 * @param element
 * @return
 */
@SuppressWarnings("unchecked")
public static Set<Class<? extends ComplexDataObject>> getSupportedTypes(Element element) {

    Class<? extends ComplexDataObject> type = element.getType().getValidObjectClass();
    HashSet<Class<? extends ComplexDataObject>> result = new HashSet<Class<? extends ComplexDataObject>>();

    if (!DocumentRoot.class.isAssignableFrom(type)) {
        result.add(type);
        return result;
    }

    DocumentRoot root = (DocumentRoot) BeanUtils.instantiateClass(type);

    for (int i = 0; i < root.getElementDeclCount(); i++) {
        Element declaredElement = root.getElementDecl(i);
        result.add(declaredElement.getType().getValidObjectClass());
    }

    return result;
}

From source file:edu.msu.cme.rdp.readseq.utils.ResampleSeqFile.java

/**
 * random select (without replacement) a fraction of indices from an array
 * @param object[]/*from w  w w  .j a v a2  s . c  o  m*/
 * @param num_of_selection
 * @return 
 * @throws IOException
 */
public static HashSet<Integer> randomSelectIndices(Object[] seqIdSet, int num_of_selection) throws IOException {
    int n = Math.min(num_of_selection, seqIdSet.length);
    HashSet<Integer> selectedIndexSet = new HashSet<Integer>();
    while (selectedIndexSet.size() < n) {
        selectedIndexSet.add(random.nextInt(n));
    }
    return selectedIndexSet;
}

From source file:Main.java

/**
 * Get the interfaces for the specified class.
 *
 * @param cls  the class to look up, may be {@code null}
 * @param interfacesFound the {@code Set} of interfaces for the class
 *//*w w  w  .  j  a  va2 s.c  o m*/
private static void getAllInterfaces(Class<?> cls, final HashSet<Class<?>> interfacesFound) {
    while (cls != null) {
        final Class<?>[] interfaces = cls.getInterfaces();

        for (final Class<?> i : interfaces) {
            if (interfacesFound.add(i)) {
                getAllInterfaces(i, interfacesFound);
            }
        }

        cls = cls.getSuperclass();
    }
}

From source file:com.surevine.alfresco.repo.delete.PerishReason.java

/**
 * Creates a new {@link PerishReason} instance from the given {@link JSONObject}.
 * @param json the json representing the perish reason
 * @return/*  w ww  . ja  v a  2s. c o  m*/
 * @throws JSONException
 */
public static PerishReason fromJSON(JSONObject json) throws JSONException {
    String code = json.getString("code");
    int perishDays = json.getInt("perishDays");
    String title = json.getString("title");
    String description = json.getString("description");
    JSONArray sites = json.getJSONArray("sites");

    HashSet<String> sitesSet = new HashSet<String>(sites.length());

    for (int i = 0; i < sites.length(); ++i) {
        sitesSet.add(sites.getString(i));
    }

    return new PerishReason(code, perishDays, title, description, sitesSet);
}

From source file:eu.trentorise.smartcampus.permissionprovider.common.Utils.java

/**
 * Generate set of strings out of specified delimited string. Remove also leading/trailing spaces around the elements.
 * @param input//from  w ww.j  ava2 s  . com
 * @param delimiter
 * @return
 */
public static Set<String> delimitedStringToSet(String input, String delimiter) {
    HashSet<String> res = new HashSet<String>();
    String[] arr = null;
    if (delimiter != null) {
        arr = input.split(delimiter);
        for (String s : arr) {
            res.add(s.trim());
        }
    }
    return res;
}

From source file:models.Watch.java

public static Set<User> findWatchers(ResourceType resourceType, String resourceId) {
    HashSet<User> users = new HashSet<>();
    for (Watch watch : Watch.findBy(resourceType, resourceId)) {
        users.add(watch.user);
    }/*from w  w w  .  ja va  2s  .  c o m*/
    return users;
}

From source file:Main.java

public static Integer[] randomIntArray(int size) {

    Integer a[] = new Integer[size];
    HashSet<Integer> set = new HashSet<>();
    for (int x = 0; x < a.length; x++) {

        Random r = new Random();
        Integer i = r.nextInt(size * 10);

        while (set.contains(i)) {
            i = r.nextInt(size);//from  ww  w .j  a v  a 2s.  c  om
        }
        set.add(i);
        a[x] = i;
    }
    return a;
}

From source file:com.facebook.model.JsonUtil.java

static Set<String> jsonObjectKeySet(JSONObject jsonObject) {
    HashSet<String> result = new HashSet<String>();

    @SuppressWarnings("unchecked")
    Iterator<String> keys = jsonObject.keys();
    while (keys.hasNext()) {
        result.add(keys.next());
    }//www. ja  va 2 s.c  o  m

    return result;
}

From source file:models.Watch.java

public static Set<User> findUnwatchers(ResourceType resourceType, String resourceId) {
    HashSet<User> users = new HashSet<>();
    for (Unwatch unwatch : Unwatch.findBy(resourceType, resourceId)) {
        users.add(unwatch.user);
    }//from   ww  w.  j  a  v  a  2 s  .  c  o m
    return users;
}

From source file:Main.java

public static void cosineSimilarityCW() {
    Iterator<Integer> ids = CommentWordCount.keySet().iterator();
    while (ids.hasNext()) {
        int com_id = ids.next();
        Set<String> words1;
        words1 = CommentWordCount.get(com_id).keySet();
        Iterator<Integer> com_iter = CommentWordCount.keySet().iterator();
        while (com_iter.hasNext()) {
            int id = com_iter.next();
            if (com_id < id) {
                Set<String> words2;
                words2 = CommentWordCount.get(id).keySet();

                Vector<Integer> vecA = new Vector<Integer>();
                Vector<Integer> vecB = new Vector<Integer>();

                Iterator<String> w1 = words1.iterator();
                Iterator<String> w2 = words2.iterator();

                HashSet<String> imp = new HashSet<String>();
                while (w1.hasNext()) {
                    String s = w1.next();
                    imp.add(s);
                }//from  w  w w .ja v  a  2 s.  com
                while (w2.hasNext()) {
                    String s = w2.next();
                    imp.add(s);
                }
                for (String s : imp) {
                    if (CommentWordCount.get(com_id).containsKey(s)) {
                        vecA.add(CommentWordCount.get(com_id).get(s));
                    } else
                        vecA.add(0);

                    if (CommentWordCount.get(id).containsKey(s)) {
                        vecB.add(CommentWordCount.get(id).get(s));
                    } else
                        vecB.add(0);
                }

                //System.out.println("Size : A"+vecA.size()+" Size: B"+vecB.size()+"maxLen:"+maxlength);
                double similarity;
                int product = 0;
                double sumA = 0;
                double sumB = 0;
                for (int i = 0; i < vecA.size(); i++) {
                    product += vecA.elementAt(i) * vecB.elementAt(i);
                    sumA += vecA.elementAt(i) * vecA.elementAt(i);
                    sumB += vecB.elementAt(i) * vecB.elementAt(i);
                }
                sumA = Math.sqrt(sumA);
                sumB = Math.sqrt(sumB);
                similarity = product / (sumA * sumB);
                similarity = Math.acos(similarity) * 180 / Math.PI;
                //System.out.println("Result "+com_id+" "+id+" :"+similarity);

                if (similarity < 75) {
                    //System.out.println("Result "+com_id+" "+id);
                    if (Topic.containsKey(com_id)) {
                        int val = Topic.get(com_id);
                        val++;
                        Topic.put(com_id, val);
                    } else
                        Topic.put(com_id, 1);
                    if (Topic.containsKey(id)) {
                        int val = Topic.get(id);
                        val++;
                        Topic.put(id, val);
                    } else
                        Topic.put(id, 1);
                }

            }
        }
    }
}