com.googlecode.concurrenttrees.common
Class Iterables

java.lang.Object
  extended by com.googlecode.concurrenttrees.common.Iterables

public class Iterables
extends Object

Provides methods to convert Iterables to Lists and Sets.

This class is mostly provided for backwards compatibility in applications which were programmed against concurrent-trees 1.0.0, in which the tree APIs returned lists and sets instead of lazily-evaluated iterables.

Note that in applications which would have simply iterated through the lists and sets returned by the old APIs, the new approach of returning lazy iterables is more efficient. Applications can iterate the iterables returned in exactly the same manner, and results will be the same.

These methods are provided for convenience in applications which actually relied on List and Set-specific features in the objects which were returned.

Most methods in this class are somewhat similar to utilities in Google Guava; but are provided here to avoid a dependency on Guava. Applications could use either these methods or Guava.

Author:
Niall Gallagher

Method Summary
static int count(Iterable<?> iterable)
          Counts the number of elements returned by the given Iterable.
static
<T> List<T>
toList(Iterable<T> iterable)
          Copies elements from the given Iterable into a new List.
static
<T> Set<T>
toSet(Iterable<T> iterable)
          Copies elements from the given Iterable into a new Set.
static String toString(Iterable<?> iterable)
          Returns a string representation of elements returned by the given Iterable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

toList

public static <T> List<T> toList(Iterable<T> iterable)
Copies elements from the given Iterable into a new List.

The iteration order of the list returned, will be the same as that of the iterable.

Be aware of the memory implications of copying objects from a lazy iterable into a collection; usually it's better to just work with the iterable directly (i.e. by iterating it).

Type Parameters:
T - The type of elements returned by the iterable
Parameters:
iterable - Provides elements to be copied into a new list
Returns:
A new List which contains the elements which were returned by the iterable

toSet

public static <T> Set<T> toSet(Iterable<T> iterable)
Copies elements from the given Iterable into a new Set.

The iteration order of the set returned, will be the same as that of the iterable.

Be aware of the memory implications of copying objects from a lazy iterable into a collection; usually it's better to just work with the iterable directly (i.e. by iterating it).

Type Parameters:
T - The type of elements returned by the iterable
Parameters:
iterable - Provides elements to be copied into a new set
Returns:
A new Set which contains the elements which were returned by the iterable

toString

public static String toString(Iterable<?> iterable)
Returns a string representation of elements returned by the given Iterable.

Parameters:
iterable - Provides elements whose toString representations should be included in the string
Returns:
A string representation of elements returned by the given Iterable

count

public static int count(Iterable<?> iterable)
Counts the number of elements returned by the given Iterable.

Parameters:
iterable - Provides elements to be counted
Returns:
The number of elements returned by the iterable


Copyright © 2013. All Rights Reserved.