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 String constructQueryString(final Map<String, String> query) {
    final Set<Map.Entry<String, String>> mapEntries = query.entrySet();
    final Iterator<Map.Entry<String, String>> iterator = mapEntries.iterator();
    final StringBuilder queryStringBuilder = new StringBuilder();

    while (iterator.hasNext()) {
        final Map.Entry<String, String> entry = iterator.next();
        final String pair = String.format("%s=%s", entry.getKey(), entry.getValue());
        queryStringBuilder.append(pair);
        if (iterator.hasNext()) {
            queryStringBuilder.append('&');
        }// w ww .j av  a 2 s .  c om

    }
    return queryStringBuilder.toString();
}

From source file:Main.java

/**
 * Parse Set to ArrayList/*from w w  w  . ja va  2s  .co  m*/
 * 
 * @param set
 * @return ArrayList
 */
public static <T> ArrayList<T> setToArrayList(Set<T> set) {
    if (set == null)
        return null;
    ArrayList<T> tArray = new ArrayList<T>();
    Iterator<T> it = set.iterator();
    while (it.hasNext()) {
        T t = it.next();
        tArray.add(t);
    }
    return tArray;
}

From source file:Main.java

/**
 * Returns a {@link Collection} containing the intersection
 * of the given {@link Collection}s.//  ww  w.  j  ava 2s  .c  o  m
 * <p>
 * The cardinality of each element in the returned {@link Collection}
 * will be equal to the minimum of the cardinality of that element
 * in the two given {@link Collection}s.
 *
 * @param a  the first collection, must not be null
 * @param b  the second collection, must not be null
 * @return the intersection of the two collections
 */
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 HashSet(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 void vectorToXML222(String xmlFile, String xpath, String parentNodeName, Vector<HashMap> vector) {
    File file = new File(xmlFile);
    try {//from  w ww.  j  a  v  a 2  s. co m
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

        Document document;
        Element rootNode;
        if (file.exists()) {
            document = documentBuilder.parse(new File(xmlFile));
            rootNode = document.getDocumentElement();
        } else {
            document = documentBuilder.newDocument();
            rootNode = document.createElement(xpath);
            document.appendChild(rootNode);
        }

        for (int x = 0; x < vector.size(); x++) {
            Element parentNode = document.createElement(parentNodeName);
            rootNode.appendChild(parentNode);
            HashMap hashmap = vector.get(x);
            Set set = hashmap.entrySet();
            Iterator i = set.iterator();

            while (i.hasNext()) {
                Map.Entry me = (Map.Entry) i.next();
                // System.out.println("key=" +
                // me.getKey().toString());
                Element em = document.createElement(me.getKey().toString());
                em.appendChild(document.createTextNode(me.getValue().toString()));
                parentNode.appendChild(em);
                // System.out.println("write " +
                // me.getKey().toString() +
                // "="
                // + me.getValue().toString());
            }
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        FileOutputStream fo = new FileOutputStream(xmlFile);
        StreamResult result = new StreamResult(fo);
        transformer.transform(source, result);
    } catch (Exception ex) {
        file.delete();
        ex.printStackTrace();
    }
}

From source file:de.mpg.imeji.logic.search.util.CollectionUtils.java

@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);/*from   ww  w.  j a  v a 2s.c  o  m*/
    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:de.mpg.imeji.logic.search.util.CollectionUtils.java

@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);//from www  .  j a  va 2  s.c o m
    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

/**
 * Use the transform specified by xslSrc and transform the document specified by docSrc, and return the resulting
 * document.//from w ww. ja va 2 s.  com
 * 
 * @param xslSrc
 *          StreamSrc containing the xsl transform
 * @param docSrc
 *          StreamSrc containing the document to be transformed
 * @param params
 *          Map of properties to set on the transform
 * @param resolver
 *          URIResolver instance to resolve URI's in the output document.
 * 
 * @return StringBuffer containing the XML results of the transform
 * @throws TransformerConfigurationException
 *           if the TransformerFactory fails to create a Transformer.
 * @throws TransformerException
 *           if actual transform fails.
 */
protected static final StringBuffer transformXml(final StreamSource xslSrc, final StreamSource docSrc,
        final Map params, final URIResolver resolver)
        throws TransformerConfigurationException, TransformerException {

    StringBuffer sb = null;
    StringWriter writer = new StringWriter();

    TransformerFactory tf = TransformerFactory.newInstance();
    if (null != resolver) {
        tf.setURIResolver(resolver);
    }
    // TODO need to look into compiling the XSLs...
    Transformer t = tf.newTransformer(xslSrc); // can throw
    // TransformerConfigurationException
    // Start the transformation
    if (params != null) {
        Set<?> keys = params.keySet();
        Iterator<?> it = keys.iterator();
        String key, val;
        while (it.hasNext()) {
            key = (String) it.next();
            val = (String) params.get(key);
            if (val != null) {
                t.setParameter(key, val);
            }
        }
    }
    t.transform(docSrc, new StreamResult(writer)); // can throw
    // TransformerException
    sb = writer.getBuffer();

    return sb;
}

From source file:Main.java

/**
 * patterns: ["/util//note"]/*w  w w .  j  av a 2  s .c  o m*/
 * @param Set<String> patterns
 * @return /util/note
 */
public static String first(Set<String> patterns) {
    String str = "";
    if (patterns != null && patterns.size() > 0) {
        Iterator<String> iterator = patterns.iterator();
        while (iterator.hasNext()) {
            str = (String) iterator.next();
            str = str.replaceAll("//", "/");
            break;
        }
    }
    return str;
}

From source file:Main.java

public static String encode(Map<String, String> params, String encoding) {
    StringBuilder encodedParams = new StringBuilder();

    try {/*from   ww  w. j av  a  2 s. com*/
        Set uee = params.entrySet();
        int size = uee.size();
        int index = 0;
        Iterator iterator = uee.iterator();

        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            encodedParams.append(URLEncoder.encode((String) entry.getKey(), encoding));
            encodedParams.append('=');
            encodedParams.append(URLEncoder.encode((String) entry.getValue(), encoding));
            ++index;
            if (index < size) {
                encodedParams.append('&');
            }
        }

        return encodedParams.toString();
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Encoding not supported: " + encoding, e);
    }
}

From source file:Main.java

public static <V, T> List<Map.Entry<V, T>> MapToList(Map<V, T> map) {
    Set<Map.Entry<V, T>> set = map.entrySet();
    List<Map.Entry<V, T>> list = new ArrayList<Map.Entry<V, T>>();
    for (Iterator<Map.Entry<V, T>> it = set.iterator(); it.hasNext();) {
        Map.Entry<V, T> entry = it.next();
        list.add(entry);//from www .ja v  a  2  s .  c om
    }
    return list;
}