Example usage for java.util HashSet contains

List of usage examples for java.util HashSet contains

Introduction

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

Prototype

public boolean contains(Object o) 

Source Link

Document

Returns true if this set contains the specified element.

Usage

From source file:Main.java

/**
 * Keeps and returns the original list.//from  w w  w.  j a  v  a  2 s. c  om
 * 
 * @param <T>
 * @param list
 * @return removes any duplicates in the list. Keeps the first occurrence of duplicates.
 */
public static <T> List<T> removeDuplicates(List<T> list) {
    HashSet<T> set = new HashSet<T>(list.size());
    ListIterator<T> i = list.listIterator();
    while (i.hasNext()) {
        T cur = i.next();
        if (set.contains(cur)) {
            i.remove();
        } else {
            set.add(cur);
        }
    }

    return list;
}

From source file:Main.java

public static HashSet<String> intersectionSet(HashSet<String> setA, HashSet<String> setB) {
    HashSet<String> intersectionSet = new HashSet<String>();
    Iterator<String> iterA = setA.iterator();
    while (iterA.hasNext()) {
        String tempInner = iterA.next();
        if (setB.contains(tempInner)) {
            intersectionSet.add(tempInner);
        }//from ww  w. ja va  2s . c o  m
    }
    return intersectionSet;
}

From source file:Main.java

public static ArrayList<Double> stringToDoubleListRemoved(String s, String delimeter, int[] fieldsToBeRemoved) {
    ArrayList<Double> ret = new ArrayList<Double>();

    ArrayList<Double> allFields = stringToDoubleList(s, delimeter);

    HashSet<Integer> toBeRemoved = new HashSet<Integer>();
    for (int i : fieldsToBeRemoved)
        toBeRemoved.add(i);//  w  w w .  j  av  a 2 s. com

    for (int i = 0; i < allFields.size(); i++) {
        if (toBeRemoved.contains(i))
            continue;
        ret.add(allFields.get(i));
    }
    return ret;
}

From source file:Main.java

private static void addNumberToBase(StringBuffer base, boolean bracketed, HashSet<Integer> set) {
    if (set.size() > 0) {
        // Limit on the number of auto-generated item numbers to check for
        int limit = 100;
        // Check the set for the numbers encountered and generate the 
        // lowest number accordingly
        if (set.contains(new Integer(0)) == false) {
            // Use base
        } else {/*  w  w w.j  a v a2s.  c  om*/
            for (int x = 1; x < limit; x++) {
                // Check if the number was already used to auto-generate an
                // existing item
                if (set.contains(new Integer(x)) == false) {
                    if (bracketed)
                        base.append(" ("); //$NON-NLS-1$
                    base.append(x);
                    if (bracketed)
                        base.append(")"); //$NON-NLS-1$
                    break;
                }
            }
        }
    }
}

From source file:Main.java

public static void getTop(double[] array, ArrayList<Integer> rankList, int i) {
    int index = 0;
    HashSet<Integer> scanned = new HashSet<Integer>();
    double max = Double.MIN_VALUE;
    for (int m = 0; m < i && m < array.length; m++) {
        max = Double.MIN_VALUE;/*from w ww  .ja  v  a 2  s  .c  o  m*/
        for (int no = 0; no < array.length; no++) {
            if (!scanned.contains(no)) {
                if (array[no] > max) {
                    index = no;
                    max = array[no];
                } else if (array[no] == max && Math.random() > 0.5) {
                    index = no;
                    max = array[no];
                }
            }
        }
        if (!scanned.contains(index)) {
            scanned.add(index);
            rankList.add(index);
        }
        //System.out.println(m + "\t" + index);
    }
}

From source file:Main.java

public static void getTop(ArrayList<Double> array, ArrayList<Integer> rankList, int i) {
    int index = 0;
    HashSet<Integer> scanned = new HashSet<Integer>();
    Double max = Double.MIN_VALUE;
    for (int m = 0; m < i && m < array.size(); m++) {
        max = Double.MIN_VALUE;/* ww w .j  a va2s  .c o  m*/
        for (int no = 0; no < array.size(); no++) {
            if (!scanned.contains(no)) {
                if (array.get(no) > max) {
                    index = no;
                    max = array.get(no);
                } else if (array.get(no).equals(max) && Math.random() > 0.5) {
                    index = no;
                    max = array.get(no);
                }
            }
        }
        if (!scanned.contains(index)) {
            scanned.add(index);
            rankList.add(index);
        }
        //System.out.println(m + "\t" + index);
    }
}

From source file:Main.java

public static void getTop(float[] array, ArrayList<Integer> rankList, int i) {
    int index = 0;
    int count = 0;
    HashSet<Integer> scanned = new HashSet<Integer>();
    float max = Float.MIN_VALUE;
    for (int m = 0; m < i && m < array.length; m++) {
        max = Float.MIN_VALUE;//w w w . j a  v  a  2  s  . c  o m
        for (int no = 0; no < array.length; no++) {
            if (array[no] >= max && !scanned.contains(no)) {
                index = no;
                max = array[no];
            }
        }
        scanned.add(index);
        rankList.add(index);
        //System.out.println(m + "\t" + index);
    }
}

From source file:Main.java

public static void getTop(float[] array, ArrayList<Integer> rankList, int i) {
    rankList.clear();/*from   w w  w .j a v a2s. co  m*/

    int index = 0;
    HashSet<Integer> scanned = new HashSet<Integer>();
    float max = Float.MIN_VALUE;
    for (int m = 0; m < i && m < array.length; m++) {
        boolean flag = false;
        max = Float.MIN_VALUE;
        for (int no = 0; no < array.length; no++) {
            if (!scanned.contains(no) && array[no] >= max) {
                index = no;
                max = array[no];
                flag = true;
            }
        }
        if (flag) { // found value
            scanned.add(index);
            rankList.add(index);
        }
    }
}

From source file:Main.java

public static void getTop(float[] array, ArrayList<Integer> rankList, int i) {
    rankList.clear();/*from ww w .  j  a va2 s  .  c om*/

    int index = 0;
    HashSet<Integer> scanned = new HashSet<Integer>();
    float max = Float.MIN_VALUE;
    for (int m = 0; m < i && m < array.length; m++) {
        boolean flag = false;
        max = Float.MIN_VALUE;
        for (int no = 0; no < array.length; no++) {
            if (!scanned.contains(no) && array[no] >= max) {
                index = no;
                max = array[no];
                flag = true;
            }
        }
        if (flag) { // found value
            scanned.add(index);
            rankList.add(index);
            // rankProbs.add(array[index]);
        }
        // System.out.println(m + "\t" + index);
    }
}

From source file:Main.java

public static void getTop(float[] array, ArrayList<Integer> rankList, ArrayList<Float> rankProbs, int i) {
    // clear//from  www .  j  a v  a  2  s . c om
    rankList.clear();
    rankProbs.clear();
    //
    int index = 0;
    HashSet<Integer> scanned = new HashSet<Integer>();
    float max = Float.MIN_VALUE;
    for (int m = 0; m < i && m < array.length; m++) {
        boolean flag = false;
        max = Float.MIN_VALUE;
        for (int no = 0; no < array.length; no++) {
            if (array[no] >= max && !scanned.contains(no)) {
                index = no;
                max = array[no];
                flag = true;
            }
        }
        if (flag) { // found value
            scanned.add(index);
            rankList.add(index);
            rankProbs.add(array[index]);
        }
        // System.out.println(m + "\t" + index);
    }
}