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

protected static Set intersectEmail(Set permitted, String email) {
    String _sub = email.substring(email.indexOf('@') + 1);

    if (permitted.isEmpty()) {
        permitted.add(_sub);/*www  .  j ava2s  .co m*/

        return permitted;
    } else {
        Set intersect = new HashSet();

        Iterator _iter = permitted.iterator();
        while (_iter.hasNext()) {
            String _permitted = (String) _iter.next();

            if (_sub.endsWith(_permitted)) {
                intersect.add(_sub);
            } else if (_permitted.endsWith(_sub)) {
                intersect.add(_permitted);
            }
        }

        return intersect;
    }
}

From source file:com.bsb.cms.commons.web.MossActionUtils.java

/**
 * ?? ???<s:debug></s:debug> 
 * /*from   www.  j av  a  2 s.  c  o  m*/
 * @param req
 */
@SuppressWarnings("all")
@Deprecated
public static void print(HttpServletRequest req) {
    // Application
    Map<String, Object> parameters = new WeakHashMap<String, Object>();

    // attributes in scope RequestParameter
    for (Enumeration e = req.getParameterNames(); e.hasMoreElements();) {
        String name = (String) e.nextElement();
        String[] v = req.getParameterValues(name);
        if (v.length == 1) {
            if (v[0].equals(""))
                continue;
            parameters.put(name, v[0]);
        } else
            parameters.put(name, v);
    }

    // attributes in scope Request
    for (Enumeration e = req.getAttributeNames(); e.hasMoreElements();) {
        String name = (String) e.nextElement();
        Object v = req.getAttribute(name);
        parameters.put(name, v);
    }

    // attributes in scope Session
    HttpSession session = req.getSession();
    for (Enumeration e = session.getAttributeNames(); e.hasMoreElements();) {
        String name = (String) e.nextElement();
        Object v = session.getAttribute(name);
        parameters.put(name, v);
    }

    Set keys = parameters.keySet();
    Iterator it = keys.iterator();
    String key;
    Object value;
    while (it.hasNext()) {
        key = (String) it.next();
        value = parameters.get(key);
        System.out.println("key:" + key + ", value:" + value);
    }

}

From source file:com.apptentive.android.sdk.util.JsonDiffer.java

public static JSONObject getDiff(JSONObject original, JSONObject updated) {
    JSONObject ret = new JSONObject();

    Set<String> originalKeys = getKeys(original);
    Set<String> updatedKeys = getKeys(updated);

    Iterator<String> it = originalKeys.iterator();
    while (it.hasNext()) {
        String key = it.next();//from  w w w. j  a v  a  2 s . c o  m
        updatedKeys.remove(key);
        try {
            Object oldValue = original.opt(key);
            Object newValue = updated.opt(key);

            if (isEmpty(oldValue)) {
                if (!isEmpty(newValue)) {
                    // Old is empty. New is not. Update.
                    ret.put(key, newValue);
                }
            } else if (isEmpty(newValue)) {
                // Old is not empty, but new is empty. Clear value.
                ret.put(key, JSONObject.NULL);
            } else if (oldValue instanceof JSONObject && newValue instanceof JSONObject) {
                // Diff JSONObjects
                if (!areObjectsEqual(oldValue, newValue)) {
                    ret.put(key, newValue);
                }
            } else if (oldValue instanceof JSONArray && newValue instanceof JSONArray) {
                // Diff JSONArrays
                // TODO: At least check for strict equality. Right now, we always send nested JSONArrays.
                ret.put(key, newValue);
            } else if (!oldValue.equals(newValue)) {
                // Diff primitives
                ret.put(key, newValue);
            } else if (oldValue.equals(newValue)) {
                // Do nothing.
            }
        } catch (JSONException e) {
            Log.w("Error diffing object with key %s", e, key);
        } finally {
            it.remove();
        }
    }

    // Finally, add in the keys that were added in the new object.
    it = updatedKeys.iterator();
    while (it.hasNext()) {
        String key = it.next();
        try {
            ret.put(key, updated.get(key));
        } catch (JSONException e) {
            // This can't happen.
        }
    }

    // If there is no difference, return null.
    if (ret.length() == 0) {
        ret = null;
    }
    Log.v("Generated diff: %s", ret);
    return ret;
}

From source file:crow.util.JsonUtil.java

private static JSONArray toJSONObject(Set<Object> set) throws JSONException {
    JSONArray jsonOb = new JSONArray();
    if ((set == null) || (set.isEmpty()))
        return jsonOb;
    Iterator<Object> iter = set.iterator();
    while (iter.hasNext()) {
        Object value = iter.next();
        if ((value instanceof String) || (value instanceof Integer) || (value instanceof Double)
                || (value instanceof Long) || (value instanceof Float)) {
            jsonOb.put(value);/*from w w w .  j  av  a 2s . com*/
            continue;
        }
        if ((value instanceof Map<?, ?>)) {
            try {
                @SuppressWarnings("unchecked")
                Map<String, Object> valueMap = (Map<String, Object>) value;
                jsonOb.put(toJSONObject(valueMap));
            } catch (ClassCastException e) {
                Log.w(TAG, "Unknown map type in json serialization: ", e);
            }
            continue;
        }
        if ((value instanceof Set<?>)) {
            try {
                @SuppressWarnings("unchecked")
                Set<Object> valueSet = (Set<Object>) value;
                jsonOb.put(toJSONObject(valueSet));
            } catch (ClassCastException e) {
                Log.w(TAG, "Unknown map type in json serialization: ", e);
            }
            continue;
        }
        Log.e(TAG, "Unknown value in json serialization: " + value.toString() + " : "
                + value.getClass().getCanonicalName().toString());
    }
    return jsonOb;
}

From source file:cooccurrence.Omer_Levy.java

/**
 * Method that will extract top D singular values from CoVariance Matrix 
 * It will then identify the corresponding columns from U and V and add it to new matrices 
 * @param U//from  w w  w.  j av  a2s.c  o m
 * @param V
 * @param coVariance
 * @param matrixUd
 * @param matrixVd
 * @param coVarD
 * @param indicesD 
 */
private static void getTopD(RealMatrix U, RealMatrix V, RealMatrix coVariance, RealMatrix matrixUd,
        RealMatrix matrixVd, RealMatrix coVarD, ArrayList<Integer> indicesD) {
    TreeMap<Double, Set<Integer>> tmap = new TreeMap<>();
    for (int i = 0; i < coVariance.getRowDimension(); i++) {
        double val = coVariance.getEntry(i, i);
        if (tmap.containsKey(val)) {
            Set<Integer> temp = tmap.get(val);
            temp.add(i);
        } else {
            Set<Integer> temp = new HashSet<>();
            temp.add(i);
            tmap.put(val, temp);
        }
    }
    Iterator iter = tmap.keySet().iterator();
    while (iter.hasNext()) {
        Double val = (Double) iter.next();
        Set<Integer> indices = tmap.get(val);
        for (int i = 0; i < indices.size(); i++) {
            Iterator iterIndices = indices.iterator();
            while (iterIndices.hasNext()) {
                int index = (Integer) iterIndices.next();
                indicesD.add(index);
                coVarD.addToEntry(index, index, val);
                matrixUd.setColumnVector(index, U.getColumnVector(index));
                matrixVd.setColumnVector(index, V.getColumnVector(index));
            }
        }
    }

}

From source file:Main.java

/**
 * Copies the given {@link Set} into a new {@link Set}.<br>
 * /*from   ww w.  j  a va2  s. c  om*/
 * 
 * @param <T>
 *            the type of the entries of the set
 * @param data
 *            the given set
 * @return If the given set was empty, a {@link Collections#emptySet()} is
 *         returned<br>
 *         If the given set contained only one element, a
 *         {@link Collections#singleton(Object)}, containing said element,
 *         is returned <br>
 *         If the given set contained more than one element, a
 *         {@link Collections#unmodifiableSet(Set)}, containing the
 *         elements of the given set, is returned.
 */
public static <T> Set<T> copy(Set<T> data) {
    final int size = data.size();

    switch (size) {
    case 0:
        return Collections.emptySet();
    case 1:
        return Collections.singleton(data.iterator().next());
    default:
        return Collections.unmodifiableSet(new HashSet<T>(data));
    }
}

From source file:de.vandermeer.skb.commons.Predicates.java

/**
 * Returns a predicate that evaluates to true if the set contains strings starting with a given character sequence.
 * @param nodes set of strings as base/*w w w .j  av a2  s . com*/
 * @return predicate that returns true if set contains strings starting with it, false otherwise
 */
final public static Predicate<StrBuilder> CONTAINS_STRINGS_STARTING_WITH(final Set<String> nodes) {
    return new Predicate<StrBuilder>() {
        @Override
        public boolean test(final StrBuilder fqpn) {
            if (fqpn == null) {
                return false;
            }
            String key = fqpn.toString();
            if (!nodes.contains(key)) {
                return false;
            }

            Set<String> tail = new TreeSet<String>(nodes).tailSet(key, false);
            if (tail.size() == 0) {
                return false;
            }

            String child = tail.iterator().next();
            if (child.startsWith(key)) {
                return true;
            }
            return false;
        }
    };
}

From source file:com.opengamma.bbg.test.BloombergTestUtils.java

/**
 * Gets an example equity option ticker directly from Bloomberg reference data.
 * /*from   w w  w  .  ja  va 2s .c o m*/
 * @return the ticker, not null
 */
public static String getSampleEquityOptionTicker() {
    BloombergReferenceDataProvider rdp = new BloombergReferenceDataProvider(getBloombergConnector());
    rdp.start();

    Set<ExternalId> options = BloombergDataUtils.getOptionChain(rdp, "AAPL US Equity");
    assertEquals(false, options.isEmpty());
    ExternalId aaplOptionId = options.iterator().next();

    rdp.stop();

    return aaplOptionId.getValue();
}

From source file:hudson.matrix.MatrixTest.java

private static void expectRejection(MatrixProject project, String combinationFilter, String signature)
        throws IOException {
    ScriptApproval scriptApproval = ScriptApproval.get();
    assertEquals(Collections.emptySet(), scriptApproval.getPendingSignatures());
    try {/*ww w.  ja v  a 2s  .com*/
        project.setCombinationFilter(combinationFilter);
    } catch (RejectedAccessException x) {
        assertEquals(Functions.printThrowable(x), signature, x.getSignature());
    }
    Set<ScriptApproval.PendingSignature> pendingSignatures = scriptApproval.getPendingSignatures();
    assertEquals(1, pendingSignatures.size());
    assertEquals(signature, pendingSignatures.iterator().next().signature);
    scriptApproval.approveSignature(signature);
    assertEquals(Collections.emptySet(), scriptApproval.getPendingSignatures());
}

From source file:net.sourceforge.fenixedu.domain.AlumniIdentityCheckRequest.java

public static Collection<AlumniIdentityCheckRequest> readPendingRequests() {
    Collection<AlumniIdentityCheckRequest> pendingRequests = new ArrayList<AlumniIdentityCheckRequest>();
    Set<AlumniIdentityCheckRequest> requests = Bennu.getInstance().getAlumniIdentityRequestSet();

    AlumniIdentityCheckRequest request;//from  w ww  .j  av a2 s.  co  m
    Iterator iter = requests.iterator();
    while (iter.hasNext()) {
        request = (AlumniIdentityCheckRequest) iter.next();
        if (request.getApproved() == null) {
            pendingRequests.add(request);
        }
    }
    return pendingRequests;
}