Collection iterator - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Collection iterator method.

This code example shows how to use the following methods:iterator, size
    Assert.assertEquals(jackFollowings.size(), 1);
 
    ODocument loadedKim = jackFollowings.iterator().next();
    Assert.assertEquals(loadedKim.field("name"), "Kim");
    Collection<ODocument> kimFollowings = loadedKim.field("following");
    Assert.assertNotNull(kimFollowings);
    Assert.assertEquals(kimFollowings.size(), 1);
 
    ODocument loadedTeri = kimFollowings.iterator().next();
    Assert.assertEquals(loadedTeri.field("name"), "Teri");
    Collection<ODocument> teriFollowings = loadedTeri.field("following");
    Assert.assertNotNull(teriFollowings);
    Assert.assertEquals(teriFollowings.size(), 1);
 
    Assert.assertEquals(teriFollowings.iterator().next().field("name"), "Jack");
 
    db.close();
  } 
} 
8
CodeRank
This code example shows how to use the following methods:iterator, size
	 * Convert the collection of caches in a single expected element. 
	 * <p>Throw an {@link IllegalStateException} if the collection holds more than one element 
	 * @return the singe element or {@code null} if the collection is empty 
	 */ 
	static Cache extractFrom(Collection<? extends Cache> caches) {
		if (CollectionUtils.isEmpty(caches)) {
			return null; 
		} 
		else if (caches.size() == 1) {
			return caches.iterator().next();
		} 
		else { 
			throw new IllegalStateException("Unsupported cache resolution result " + caches +
					": JSR-107 only supports a single cache."); 
		} 
	} 
 
} 
7
CodeRank
This code example shows how to use the following methods:isEmpty, iterator
     * @return first element from the provided collection or <code>null</code> if the 
     *         collection is <code>null</code> or empty. 
     */ 
    public static <T> T getFirst(Collection<T> collection)
    { 
        if (collection == null || collection.isEmpty())
        { 
            return null; 
        } 
 
        return collection.iterator().next();
    } 
} 
7
CodeRank
This code example shows how to use the following methods:iterator
	protected void checkForExecution() { 
		if (!executed) {
			executed = true;
			final Object result = command.execute(null);
			if (result instanceof Collection)
				resultSet = ((Collection<Object>) result).iterator();
			else if (result instanceof Object)
				resultOne = (Object) result;
		} 
	} 
} 
7
CodeRank
This code example shows how to use the following methods:iterator, size
    @Test 
    public void getFunctions() throws Exception { 
        final Collection<ErlangFunction> functions = import1.getFunctions(); 
        final ErlangFunction function = functions.iterator().next(); 
        final Collection<ErlangFunction> functions2 = export.getFunctions(); 
        final ErlangFunction function2 = functions2.iterator().next(); 
        assertEquals(3, functions.size()); 
        assertEquals(new ErlangFunction("foldl", 3), function); 
        assertEquals(2, functions2.size()); 
        assertEquals(new ErlangFunction("f", 2), function2); 
    } 
 
} 
7
CodeRank
This code example shows how to use the following methods:iterator, size
	 * @throws IncorrectResultSizeDataAccessException if more than one 
	 * result object has been found in the given Collection 
	 * @see org.springframework.util.CollectionUtils#hasUniqueObject 
	 */ 
	public static <T> T uniqueResult(Collection<T> results) throws IncorrectResultSizeDataAccessException {
		int size = (results != null ? results.size() : 0);
		if (size == 0) {
			return null; 
		} 
		if (!CollectionUtils.hasUniqueObject(results)) {
			throw new IncorrectResultSizeDataAccessException(1, size);
		} 
		return results.iterator().next();
	} 
 
	/** 
	 * Return a unique result object from the given Collection. 
	 * <p>Throws an exception if 0 or more than 1 instance found. 
	 * @param results the result Collection (can be {@code null}) 
	 * @return the unique result object 
8
CodeRank
This code example shows how to use the following methods:iterator
     * @param collection the collection 
     * @param <T> the member type 
     * @return the enumeration 
     */ 
    public static <T> NamingEnumeration<T> namingEnumeration(final Collection<T> collection) {
        final Iterator<T> iterator = collection.iterator();
        return new NamingEnumeration<T>() { 
            public T next() { 
                return nextElement(); 
            } 
 
            public boolean hasMore() { 
                return hasMoreElements(); 
            } 
 
            public void close() { 
            } 
 
            public boolean hasMoreElements() { 
                return iterator.hasNext();
7
CodeRank
This code example shows how to use the following methods:iterator
        assertThat(filters.getName(), equalTo("tags"));
 
        assertThat(filters.getBuckets().size(), equalTo(2));
 
        Collection<? extends Filters.Bucket> buckets = filters.getBuckets();
        Iterator<? extends Filters.Bucket> itr = buckets.iterator();
 
        Filters.Bucket bucket = itr.next();
        assertThat(bucket, Matchers.notNullValue());
        assertThat(bucket.getDocCount(), equalTo((long) numTag1Docs));
 
        bucket = itr.next();
        assertThat(bucket, Matchers.notNullValue());
        assertThat(bucket.getDocCount(), equalTo((long) numTag2Docs));
    } 
 
} 
6
CodeRank
This code example shows how to use the following methods:iterator, size
				services = bundleContext.getServiceReferences(IMetaStore.class, filter);
			} catch (InvalidSyntaxException e) {
				//can only happen if our filter is malformed, which it should never be 
				throw new RuntimeException(e);
			} 
			if (services.size() == 1) {
				metastoreServiceReference = services.iterator().next();
				metastore = bundleContext.getService(metastoreServiceReference);
			} 
			if (metastore == null) {
				//if we still don't have a store then something is wrong with server configuration 
				final String msg = "Invalid server configuration. Failed to initialize a metadata store"; //$NON-NLS-1$
				throw new IllegalStateException(msg);
			} 
		} 
		return metastore;
	} 
 
	private void registerHostingService() { 
		SiteHostingConfig config = SiteHostingConfig.getSiteHostingConfig(PreferenceHelper.getString(ServerConstants.CONFIG_SITE_VIRTUAL_HOSTS));
6
CodeRank
This code example shows how to use the following methods:iterator
    } 
 
    if (next instanceof Iterator<?>)
      return (Iterator<OIdentifiable>) next;
 
    return ((Collection<OIdentifiable>) next).iterator();
  } 
} 
7
CodeRank

Related Collection iterator Questions & Answers:

iterator Method Overview
Returns an iterator over the elements in this collection.
See Code Examples for other Java 8 Collection Methods: