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 int[] integerSetToInt(Set<Integer> integerSet) {
    if (integerSet == null)
        return new int[0];

    int i = 0;//  w  w w. jav  a  2  s  . c  om
    int[] intArrays = new int[integerSet.size()];
    for (Iterator<Integer> Itr = integerSet.iterator(); Itr.hasNext();) {
        intArrays[i] = Itr.next().intValue();
        i++;
    }
    return intArrays;
}

From source file:Main.java

public static List toList(Set set) {
    if (set == null) {
        return null;
    }/*from   w  w w . j a va  2 s .c o m*/
    if (set.size() == 0)
        return new ArrayList();
    List list = new ArrayList();
    Iterator itor = set.iterator();
    while (itor.hasNext()) {
        Object obj = itor.next();
        list.add(obj);
    }
    return list;
}

From source file:Main.java

public static <T> boolean hasCycle(Collection<T> vertices, Function<T, Collection<T>> neighborExtractor) {
    Map<T, Collection<T>> parents = vertices.stream().collect(toMap(identity(), v -> new ArrayList<T>()));
    vertices.forEach(/*from w w w. ja  v a  2 s  .com*/
            v -> nullSafeCollection(neighborExtractor.apply(v)).forEach(child -> parents.get(child).add(v)));

    Set<T> roots = vertices.stream().filter(v -> parents.get(v).isEmpty()).collect(Collectors.toSet());

    while (!roots.isEmpty()) {
        T root = roots.iterator().next();
        roots.remove(root);
        parents.remove(root);

        nullSafeCollection(neighborExtractor.apply(root)).forEach(child -> {
            parents.get(child).remove(root);
            if (parents.get(child).isEmpty()) {
                roots.add(child);
            }
        });

    }

    return !parents.isEmpty();
}

From source file:com.qingzhi.apps.fax.client.NetworkUtilities.java

private static String executeGet(String url, Map<String, String> map) throws IOException {

    HttpClient httpclient = new DefaultHttpClient();
    HttpParams params = httpclient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, HTTP_CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, HTTP_SO_TIMEOUT);

    HttpPost post = new HttpPost(url);

    ArrayList<BasicNameValuePair> postDate = new ArrayList<BasicNameValuePair>();
    Set<String> set = map.keySet();
    Iterator<String> iterator = set.iterator();
    while (iterator.hasNext()) {
        String key = iterator.next();
        postDate.add(new BasicNameValuePair(key, map.get(key)));
    }//w  w w  .  j  ava  2  s . co  m
    post.setEntity(new UrlEncodedFormEntity(postDate, HTTP.UTF_8));
    HttpResponse httpResponse = httpclient.execute(post);
    throwErrors(httpResponse);

    HttpEntity httpEntity = httpResponse.getEntity();
    if (httpEntity != null) {
        String response = EntityUtils.toString(httpEntity);
        LOGD(TAG, response);
        return response;
    }
    return null;
}

From source file:Main.java

public static Reader getUri(URL url) throws IOException {
    //Log.d(TAG, "getUri: " + url.toString());

    boolean useGzip = false;
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(30 * 1000);/*from w ww  . j a  va  2s.c  om*/
    conn.setRequestProperty("Accept-Encoding", "gzip");
    conn.connect();

    InputStream in = conn.getInputStream();

    final Map<String, List<String>> headers = conn.getHeaderFields();
    // This is a map, but we can't assume the key we're looking for
    // is in normal casing. So it's really not a good map, is it?
    final Set<Map.Entry<String, List<String>>> set = headers.entrySet();
    for (Iterator<Map.Entry<String, List<String>>> i = set.iterator(); i.hasNext();) {
        Map.Entry<String, List<String>> entry = i.next();
        if ("Content-Encoding".equalsIgnoreCase(entry.getKey())) {
            for (Iterator<String> j = entry.getValue().iterator(); j.hasNext();) {
                String str = j.next();
                if (str.equalsIgnoreCase("gzip")) {
                    useGzip = true;
                    break;
                }
            }
            // Break out of outer loop.
            if (useGzip) {
                break;
            }
        }
    }

    if (useGzip) {
        return new BufferedReader(new InputStreamReader(new GZIPInputStream(in)), 8 * 1024);
    } else {
        return new BufferedReader(new InputStreamReader(in), 8 * 1024);
    }
}

From source file:Main.java

protected static void checkExcludedEmail(Set excluded, String email) throws CertPathValidatorException {
    if (excluded.isEmpty()) {
        return;// w  ww  . ja  v a 2s.c  o  m
    }

    String sub = email.substring(email.indexOf('@') + 1);
    Iterator it = excluded.iterator();

    while (it.hasNext()) {
        String str = (String) it.next();
        if (sub.endsWith(str)) {
            throw new CertPathValidatorException("Subject email address is from an excluded subtree");
        }
    }
}

From source file:Utils.java

/**
 * Create a typesafe copy of a raw set./*from   w  ww. jav a  2s. com*/
 * @param rawSet an unchecked set
 * @param type the desired supertype of the entries
 * @param strict true to throw a <code>ClassCastException</code> if the raw set has an invalid entry,
 *               false to skip over such entries (warnings may be logged)
 * @return a typed set guaranteed to contain only entries assignable
 *         to the named type (or they may be null)
 * @throws ClassCastException if some entry in the raw set was not well-typed, and only if <code>strict</code> was true
 */
public static <E> Set<E> checkedSetByCopy(Set rawSet, Class<E> type, boolean strict) throws ClassCastException {
    Set<E> s = new HashSet<E>(rawSet.size() * 4 / 3 + 1);
    Iterator it = rawSet.iterator();
    while (it.hasNext()) {
        Object e = it.next();
        try {
            s.add(type.cast(e));
        } catch (ClassCastException x) {
            if (strict) {
                throw x;
            } else {
                System.out.println("not assignable ");
            }
        }
    }
    return s;
}

From source file:Main.java

protected static void checkPermittedEmail(Set permitted, String email) throws CertPathValidatorException {
    if (permitted.isEmpty()) {
        return;// ww  w .java 2s  .  c  o m
    }

    String sub = email.substring(email.indexOf('@') + 1);
    Iterator it = permitted.iterator();

    while (it.hasNext()) {
        String str = (String) it.next();

        if (sub.endsWith(str)) {
            return;
        }
    }

    throw new CertPathValidatorException("Subject email address is not from a permitted subtree");
}

From source file:Main.java

/**
 * Return a new Set with elements that are in the first and second passed collection.
 * If one set is null, an empty Set will be returned.
 * @param  <T>    Type of set// w  w w .j a  v  a  2  s  .  co m
 * @param  first  First set
 * @param  second Second set
 *
 * @return a new set (depending on input type) with elements in first and second
 */
static <T> Set<T> intersection(Set<T> first, Set<T> second) {
    Set<T> result = new HashSet<T>();
    if ((first != null) && (second != null)) {
        result.addAll(first);
        //            result.retainAll(second);
        Iterator<T> iter = result.iterator();
        boolean found;
        while (iter.hasNext()) {
            T item = iter.next();
            found = false;
            for (T s : second) {
                if (s.equals(item))
                    found = true;
            }
            if (!found)
                iter.remove();
        }
    }

    return result;
}

From source file:com.redhat.rhn.domain.monitoring.test.MonitoringTestUtils.java

/**
 * Return a map of parameter names to their values for <code>probe</code>
 * @param probe the probe//w w w  .j a v  a 2 s . c o m
 * @return a map of parameter names to their values
 */
public static Map parameterValueMap(Probe probe) {
    Set ppvSet = probe.getProbeParameterValues();
    Map result = new HashMap();
    for (Iterator i = ppvSet.iterator(); i.hasNext();) {
        ProbeParameterValue ppv = (ProbeParameterValue) i.next();
        result.put(ppv.getParamName(), ppv.getValue());
    }
    return result;
}