
org.ocpsoft.common.util.Iterators Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2012 Lincoln Baxter, III
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ocpsoft.common.util;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
/**
* Utility methods for working with {@link Iterator} and {@link Iterable} instances.
*
* @author Lincoln Baxter, III
*/
public final class Iterators
{
public Iterators()
{}
/**
* Return the elements of the given {@link Iterable} as a {@link List}.
*/
public static List asList(final Iterable iterable)
{
List result = new ArrayList<>();
for (T t : iterable)
{
result.add(t);
}
return result;
}
/**
* Return the elements of the given {@link Iterator} as a {@link List}.
*/
public static List asList(final Iterator iterator)
{
List result = new ArrayList<>();
while (iterator.hasNext())
{
T t = iterator.next();
result.add(t);
}
return result;
}
/**
* Return the elements of the given {@link Iterable} as a {@link List}.
*/
public static Set asSet(final Iterable iterable)
{
Set result = new HashSet<>();
for (T t : iterable)
{
result.add(t);
}
return result;
}
/**
* Return the elements of the given {@link Iterator} as a {@link List}.
*/
public static Set asSet(final Iterator iterator)
{
Set result = new HashSet<>();
while (iterator.hasNext())
{
T t = iterator.next();
result.add(t);
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy