Example usage for java.util Collection iterator

List of usage examples for java.util Collection iterator

Introduction

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

Prototype

Iterator<E> iterator();

Source Link

Document

Returns an iterator over the elements in this collection.

Usage

From source file:Main.java

/**
 * Removes the element at the specified position in 
 * the collection. //from w w  w. j  a v a2 s  .co m
 * Shifts any subsequent elements to the left 
 * (subtracts one from their indices). 
 * Returns the element that was removed from the Collection.
 * @param p_collection a Collection
 * @param p_index the index of the element to removed.
 * @param p_numberOfObjects the number of objects to remove.
 * @returns a collection with the elements at p_index removed.
 */
public static Collection remove(Collection p_collection, int p_index, int p_numberOfObjects) {
    if (p_collection == null) {
        return null;
    }
    List returnList = new ArrayList(p_collection.size() - p_numberOfObjects);
    Iterator it = p_collection.iterator();
    for (int i = 0; it.hasNext(); i++) {
        if (i < p_index || i >= p_index + p_numberOfObjects) {
            returnList.add(it.next());
        } else {
            it.next();
        }
    }
    return returnList;
}

From source file:$.Collections3.java

/**
     * ?Collectioncollectionnull./*ww  w. j  a  va 2s  .com*/
     */
    public static <T> T getFirst(Collection<T> collection) {
        if (isEmpty(collection)) {
            return null;
        }

        return collection.iterator().next();
    }

From source file:Main.java

/**
 * //w  ww  .j  a va2s  .com
 * 
 */
@SuppressWarnings("unchecked")
public static <T> T[] toArray(Collection<T> collection, Class<T> elementClass, int index, int count) {
    int n = collection.size();
    int end = Math.min(index + count, n);
    Object result = Array.newInstance(elementClass, end - index);
    Iterator<T> it = collection.iterator();
    int resultIndex = 0;
    for (int i = 0; i < end; i++) {
        if (!it.hasNext())
            break;
        Object value = it.next();
        if (i < index)
            continue;
        Array.set(result, resultIndex, value);
        resultIndex++;
    }
    return (T[]) result;
}

From source file:Main.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static Collection in(Collection source, Collection target) {
    if (source == null || source.size() == 0) {
        return null;
    }//from   w  w w  .ja v  a2  s  . c om
    if (target == null || target.size() == 0) {
        return null;
    }
    Collection result = new ArrayList();
    for (Iterator it = source.iterator(); it.hasNext();) {
        Object candidate = it.next();
        if (target.contains(candidate)) {
            result.add(candidate);
        }
    }
    return result;
}

From source file:com.lithium.flow.matcher.StringMatchers.java

@Nonnull
public static StringMatcher fromList(@Nonnull List<String> list) {
    Multimap<String, String> multimap = HashMultimap.create();
    for (String value : list) {
        int index = value.indexOf(':');
        if (index == -1 || index >= value.length() - 1) {
            multimap.put("exact", value);
        } else {// www. j  av a2  s  .c o m
            int index2 = value.indexOf("?[");
            int index3 = value.indexOf("]:", index2);
            if (index2 > -1 && index3 > -1 && index2 < index) {
                index = index2 + 1;
            }

            String type = value.substring(0, index);
            String param = value.substring(index + 1);
            multimap.put(type, param);
        }
    }

    List<StringMatcher> quickMatchers = Lists.newArrayList();
    quickMatchers.addAll(buildList(multimap, "len", LenStringMatcher::new));
    Collection<String> exacts = multimap.get("exact");
    if (exacts.size() == 1) {
        quickMatchers.add(new ExactStringMatcher(exacts.iterator().next()));
    } else if (exacts.size() > 1) {
        quickMatchers.add(new ExactSetStringMatcher(Sets.newHashSet(exacts)));
    }
    quickMatchers.addAll(buildList(multimap, "prefix", PrefixStringMatcher::new));
    quickMatchers.addAll(buildList(multimap, "suffix", SuffixStringMatcher::new));
    quickMatchers.addAll(buildList(multimap, "contains", ContainsStringMatcher::new));

    List<StringMatcher> lowerMatchers = Lists.newArrayList();
    lowerMatchers.addAll(buildList(multimap, "lower.prefix", PrefixStringMatcher::new));
    lowerMatchers.addAll(buildList(multimap, "lower.suffix", SuffixStringMatcher::new));
    lowerMatchers.addAll(buildList(multimap, "lower.contains", ContainsStringMatcher::new));

    List<StringMatcher> regexMatchers = Lists.newArrayList();
    regexMatchers.addAll(buildList(multimap, "regex", RegexStringMatcher::new));
    regexMatchers.addAll(buildList(multimap, "lower.regex", LowerRegexStringMatcher::new));

    List<StringMatcher> allMatchers = Lists.newArrayList();
    allMatchers.add(buildComposite(quickMatchers, false));
    allMatchers.add(buildComposite(lowerMatchers, true));
    allMatchers.add(buildComposite(regexMatchers, false));
    return buildComposite(allMatchers, false);
}

From source file:Main.java

public static boolean equals(Collection<?> collection1, Collection<?> collection2) {
    if (collection1 == collection2) {
        return true;
    }// w  ww. java  2  s.co  m

    if (collection1.size() != collection2.size()) {
        return false;
    }

    final Iterator<?> iterator1 = collection1.iterator();
    final Iterator<?> iterator2 = collection2.iterator();

    while (iterator1.hasNext()) {
        final Object object1 = iterator1.next();
        final Object object2 = iterator2.next();

        if ((object1 == null && object2 != null) || (object1 != null && object2 == null)) {
            return false;
        }

        if (object1 != null && !object1.equals(object2)) {
            return false;
        }
    }

    return true;
}

From source file:Main.java

public static String singleDump(final Collection<?> l) {
    final StringBuilder sb = new StringBuilder();
    if (l == null) {
        sb.append("[]");
    } else if (l.isEmpty()) {
        sb.append("[]");
    } else {/*from   www. j a v a 2 s .  c  om*/
        final Iterator<?> iterator = l.iterator();
        int cnt = 0;
        sb.append("[");
        while (iterator.hasNext()) {
            if (cnt++ > 0) {
                sb.append(", ");
            }
            sb.append(iterator.next().toString());
        }
        sb.append("]");
    }
    return (sb.toString());
}

From source file:Main.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static Collection substract(Collection source, Collection target) {
    if (source == null || source.size() == 0) {
        return null;
    }//from w  ww  .j av  a 2  s .c o  m
    if (target == null || target.size() == 0) {
        return source;
    }
    Collection result = new ArrayList();
    for (Iterator it = source.iterator(); it.hasNext();) {
        Object candidate = it.next();
        if (!target.contains(candidate)) {
            result.add(candidate);
        }
    }
    return result;
}

From source file:gov.nih.nci.cabig.ctms.web.taglibs.Functions.java

public static String collapseIntoDisjointRangesString(Collection<Integer> list) {
    if (list == null)
        return null;
    if (list.size() == 0)
        return "";
    Iterator<Integer> iterator = list.iterator();
    StringBuffer sb = new StringBuffer();
    Integer rangeStart = null;/*from  ww  w  .j  a v a  2  s.  co m*/
    Integer last = iterator.next();
    while (iterator.hasNext()) {
        Integer i = iterator.next();
        if (i == last + 1) {
            if (rangeStart == null) {
                rangeStart = last;
                sb.append(last);
            }
        } else {
            appendRangeEnd(rangeStart, sb, last);
            sb.append(", ");
            rangeStart = null;
        }
        last = i;
    }
    appendRangeEnd(rangeStart, sb, last);
    return sb.toString();
}

From source file:Main.java

public static <T> T getUnique(Collection<T> list) {
    if (list == null)
        throw new IllegalArgumentException(" collection is null! collection must be not null");
    else if (list.size() != 1)
        throw new IllegalArgumentException(
                list.size() + " collection must be unique : " + list.iterator().next().toString());
    else//from ww  w  .  jav a  2s .  co  m
        return list.iterator().next();
}