Example usage for java.util Set iterator

List of usage examples for java.util Set iterator

Introduction

In this page you can find the example usage for java.util Set iterator.

Prototype

Iterator<E> iterator();

Source Link

Document

Returns an iterator over the elements in this set.

Usage

From source file:Main.java

public static <T> T getRandomFrom(Set<T> set, Random random) {
    int randomIndex = random.nextInt(set.size());
    Iterator<T> iterator = set.iterator();
    for (int i = 0; i < randomIndex; i++) {
        if (!iterator.hasNext())
            break;
        iterator.next();/*from  w w w . j  a  v  a2s  .  c  o m*/
    }
    return iterator.next();
}

From source file:edu.coeia.charts.PieChartPanel.java

private static PieDataset createSampleDataset(final Map<String, Double> map, int factor) {
    final DefaultPieDataset result = new DefaultPieDataset();

    Set set = map.entrySet();
    Iterator itr = set.iterator();

    while (itr.hasNext()) {
        Map.Entry me = (Map.Entry) itr.next();

        String str = me.getValue().toString();
        double counts = Double.valueOf(str).doubleValue();

        if (counts < factor)
            continue;

        double per = (counts / map.size()) * 100;

        result.setValue((String) me.getKey(), per);
    }//from w  ww  .java 2 s .  co m

    return result;
}

From source file:Main.java

/**
 * Make UNION operation between 2 {@link Collection}
 * //from   ww  w  .jav a  2s .  c o m
 * @param a
 * @param b
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Collection union(final Collection a, final Collection b) {
    ArrayList list = new ArrayList();
    Map mapa = getCardinalityMap(a);
    Map mapb = getCardinalityMap(b);
    Set elts = new LinkedHashSet(a);
    elts.addAll(b);
    Iterator it = elts.iterator();
    while (it.hasNext()) {
        Object obj = it.next();
        for (int i = 0, m = Math.max(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; i++) {
            list.add(obj);
        }
    }
    return list;
}

From source file:Main.java

/**
 * Make INTERSECTION operation between 2 {@link Collection}
 * /*from  w  w  w.j av  a  2s .c  om*/
 * @param a
 * @param b
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Collection intersection(final Collection a, final Collection b) {
    ArrayList list = new ArrayList();
    Map mapa = getCardinalityMap(a);
    Map mapb = getCardinalityMap(b);
    Set elts = new LinkedHashSet(a);
    elts.addAll(b);
    Iterator it = elts.iterator();
    while (it.hasNext()) {
        Object obj = it.next();
        for (int i = 0, m = Math.min(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; i++) {
            list.add(obj);
        }
    }
    return list;
}

From source file:Main.java

public static List<String> getJoins(Uri uri) {
    List<String> result = new ArrayList<String>();
    Set<String> queryParameterNames = getQueryParameterNames(uri);
    Iterator<String> iterator = queryParameterNames.iterator();
    while (iterator.hasNext()) {
        String param = iterator.next();
        if (param.startsWith(TABLE)) {
            result.add(uri.getQueryParameter(param));
        }//from   w w w  .ja  v  a 2s  . c om
    }
    return result;
}

From source file:Main.java

/**
 * Returns String from a map of string of set of string.
 *
 * @param map Map of string of set of string.
 * @param name Key of the map entry./*from  w w  w .  j  ava2  s  .co m*/
 * @return String from a map of string of set of string
 */
public static String getMapAttr(Map map, String name) {
    Set s = (Set) map.get(name);
    String retVal = ((s == null) || s.isEmpty()) ? null : ((String) s.iterator().next());
    return (retVal != null) ? retVal.trim() : null;
}

From source file:Main.java

@SuppressWarnings("rawtypes")
private static void mapToXml(Map map, StringBuffer sb) {
    Set set = map.keySet();
    for (Iterator it = set.iterator(); it.hasNext();) {
        String key = (String) it.next();
        Object value = map.get(key);
        if (null == value)
            value = "";
        if (value instanceof List) {
            ArrayList list = (ArrayList) map.get(key);
            // sb.append("<" + key + ">");
            for (int i = 0; i < list.size(); i++) {
                sb.append("<" + key + ">");
                // Object listi = list.get(i);
                if (list.get(i) instanceof HashMap) {
                    HashMap hm = (HashMap) list.get(i);
                    // sb.append("<" + key + ">");
                    mapToXml(hm, sb);//from w ww . ja va  2 s  .co  m
                    // sb.append("</" + key + ">");
                } else {
                    // sb.append("<" + key + ">" + list.get(i) + "</" + key
                    // + ">");
                    sb.append(list.get(i));
                }
                // else
                // if(listi.getClass().getName().equals("java.util.ArrayList")){
                // sb.append("<" + key + ">" + "??" + "</" + key + ">");}

                sb.append("</" + key + ">");
            }
            // sb.append("</" + key + ">");

        } else {
            if (value instanceof HashMap) {
                sb.append("<" + key + ">");
                mapToXml((HashMap) value, sb);
                sb.append("</" + key + ">");
            } else {
                sb.append("<" + key + ">" + value + "</" + key + ">");
            }

        }

    }
}

From source file:Main.java

/**
 * Make INTERSECTION operation between 2 {@link Collection}
 *
 * @param a//from w ww  . java 2s. c  o m
 * @param b
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Collection intersection(final Collection a, final Collection b) {
    final ArrayList list = new ArrayList();
    final Map mapa = getCardinalityMap(a);
    final Map mapb = getCardinalityMap(b);
    final Set elts = new LinkedHashSet(a);
    elts.addAll(b);
    final Iterator it = elts.iterator();
    while (it.hasNext()) {
        final Object obj = it.next();
        for (int i = 0, m = Math.min(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; i++) {
            list.add(obj);
        }
    }
    return list;
}

From source file:Main.java

/**
 * Make UNION operation between 2 {@link Collection}
 *
 * @param a//  w w w. ja  va  2  s .c o m
 * @param b
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Collection union(final Collection a, final Collection b) {
    final ArrayList list = new ArrayList();
    final Map mapa = getCardinalityMap(a);
    final Map mapb = getCardinalityMap(b);
    final Set elts = new LinkedHashSet(a);
    elts.addAll(b);
    final Iterator it = elts.iterator();
    while (it.hasNext()) {
        final Object obj = it.next();
        for (int i = 0, m = Math.max(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; i++) {
            list.add(obj);
        }
    }
    return list;
}

From source file:Main.java

public static List<String> getAllTables(Uri uri) {
    List<String> result = new ArrayList<String>();

    String mainTable = uri.getLastPathSegment();
    result.add(mainTable);/*from   w  w w .j a va2s  .  com*/

    Set<String> queryParameterNames = getQueryParameterNames(uri);
    Iterator<String> iterator = queryParameterNames.iterator();
    while (iterator.hasNext()) {
        String table = iterator.next();
        if (table.startsWith(TABLE)) {
            result.add(table.replaceFirst(TABLE, ""));
        }
    }
    return result;
}