Example usage for java.util Iterator next

List of usage examples for java.util Iterator next

Introduction

In this page you can find the example usage for java.util Iterator next.

Prototype

E next();

Source Link

Document

Returns the next element in the iteration.

Usage

From source file:Main.java

public static List<String> getValuesNames(List source) {
    Iterator it = source.iterator();
    List clone = new ArrayList();
    while (it.hasNext()) {
        String name = it.next().toString();
        clone.add(name);/*from w ww  . j a v  a2 s. c  om*/
    }
    return clone;
}

From source file:Main.java

public static <T1, T2> T2 getHashMapElementByHash(ConcurrentHashMap<T1, T2> target, int hashcode) {
    Iterator<T1> iter = target.keySet().iterator();
    Object key = null;/*from w  ww .ja va 2s.c  o  m*/
    while (iter.hasNext()) {
        key = iter.next();
        if (key.hashCode() == hashcode) {
            return target.get(key);
        }
    }
    return null;
}

From source file:Main.java

public static int[] integerListToIntArray(List<Integer> integers) {
    int[] ret = new int[integers.size()];
    Iterator<Integer> iterator = integers.iterator();

    for (int i = 0; i < ret.length; i++) {
        ret[i] = iterator.next().intValue();
    }//from  w  w  w .  j  ava 2 s  .c  o  m

    return ret;
}

From source file:Main.java

/**
 * Adds all elements that a given {@link Iterator} provides to a given
 * {@link Collection} of appropriate type
 * /*ww  w  .j  av  a2 s. c  om*/
 * @param targetCollection
 *           the {@link Collection} where to add the elements
 * @param sourceIterator
 *           the {@link Iterator} where to get the elements from
 * @param <T>
 *           the type of elements to transfer
 */
public static <T> void addAll(final Collection<? super T> targetCollection,
        final Iterator<? extends T> sourceIterator) {
    while (sourceIterator.hasNext()) {
        targetCollection.add(sourceIterator.next());
    }
}

From source file:Main.java

/**
 * Gets one element from the provided Collection. If no element is present,
 * an Exception is thrown. No guarantee is made as to which element is
 * returned from time to time./*from  ww  w  . j a v a 2  s  . c om*/
 *
 * @param <T> Collection type
 * @param collection to use
 * @return one element from the provided Collection
 * @throws NoSuchElementException if no elements are present in the
 * Collection
 */
public static <T> T getAnyFrom(Collection<T> collection) {
    final Iterator<T> iterator = collection.iterator();
    if (iterator.hasNext()) {
        return iterator.next();
    }
    throw new NoSuchElementException("No value present");
}

From source file:Main.java

/**
 * Check whether map contains value//from   w  ww .  j  av a 2 s.  c o  m
 *
 * @param map specified map
 * @return boolean value
 */
public static boolean isEmpty(Map map) {
    boolean isEmpty = true;
    if (map != null) {
        Set keySet = map.keySet();
        Iterator it = keySet.iterator();
        while (it.hasNext()) {
            if (map.get(it.next()) != null) {
                isEmpty = false;
                break;
            }
        }
    }
    return isEmpty;
}

From source file:Main.java

public static <T> Optional<T> findAnyIn(Collection<T> collection) {
    final Iterator<T> iterator = collection.iterator();
    if (iterator.hasNext()) {
        return Optional.of(iterator.next());
    }//w  w  w  .j  ava2s .c o  m
    return Optional.empty();
}

From source file:Main.java

public static <T> Collection<T> reverse(final Collection<T> collection) {
    final LinkedList<T> newCollection = new LinkedList<>();
    final Iterator<T> i = collection.iterator();
    while (i.hasNext()) {
        newCollection.addFirst(i.next());
    }/*from ww w  .j  a  v  a2  s  .  c o  m*/
    return newCollection;
}

From source file:Main.java

/**
 * Retrieves the nth element by linear search.
 * //from w w w  .j  a va2s. c  om
 * The first element has index 0.
 * 
 * @param iterable the iterable
 * @param n the 0-based index
 * @return the item
 */
public static <T> T nthOf(final Iterable<T> iterable, final int n) {
    final Iterator<T> iter = iterable.iterator();
    for (int i = 0; i < n; ++i) {
        iter.next();
    }
    return iter.next();
}

From source file:Main.java

public static String join(Iterator<String> strings, String sep) {
    if (!strings.hasNext())
        return "";

    String start = strings.next();
    if (!strings.hasNext()) // only one, avoid builder
        return start;

    StringBuilder sb = new StringBuilder(64).append(start);
    while (strings.hasNext()) {
        sb.append(sep);/*  ww w  .  j  av a  2 s  .  c o  m*/
        sb.append(strings.next());
    }
    return sb.toString();
}