23 Ekim 2019 Çarşamba

Iterables Sınıfı

Giriş
Şu satırı dahil ederiz.
import com.google.common.collect.Iterables
any metodu
Bu metod yerine theCollection.stream().anyMatch(...) kullanılabilir.

Örnek
Şöyle yaparız
Iterable<String> theCollection = ...;
boolean contains = Iterables.any(theCollection, new Predicate<String>() {...});
concat metodu
Metodun imzası şöyle.
/**
 * Combines two iterables into a single iterable. The returned iterable has an
 * iterator that traverses the elements in {@code a}, followed by the elements
 * in {@code b}. The source iterators are not polled until necessary.
 *
 * <p>The returned iterable's iterator supports {@code remove()} when the
 * corresponding input iterator supports it.
 */
public static <T> Iterable<T> concat(Iterable<? extends T> a,
                                     Iterable<? extends T> b)
Açıklaması şöyle.
Combines multiple iterables into a single iterable. The returned iterable has an iterator that traverses the elements of each iterable in inputs. The input iterators are not polled until necessary. The returned iterable's iterator supports remove() when the corresponding input iterator supports it.
Örnek
Elimizde şöyle bir kod olsun.
Iterable<Portfolio> result = ...;
Iterable<Portfolio> result2 = ...;
Şöyle yaparız
Iterable<Portfolio> combined = Iterables.concat(result, result2);
cycle metodu
Iterable üzerinde sonsuza kadar yürüyebilmeyi sağlar.
Örnek
Elimizde şöyle bir kod olsun.
List<Widget> widgets = ...;
for döngüsü yerine Iterator ile yürümek isteyelim.
Iterator<Widget> cyclingIterator = Iterables.cycle(widgets).iterator();
Şöyle yaparız.
public Widget pick() {
  return cyclingIterator.next();
}
filter metodu - Iterable + Predicate
Şöyle yaparız.
Iterable<String> ids = ...;
Collection<String> resourceIds = Lists.newArrayList(
    Iterables.filter(ids, NULL_FILTER));
find metodu
Örnek
Şöyle yaparız
Iterable<String> theCollection = ...;
boolean contains = Iterables.find(theCollection, new Predicate<String>() {...}) != null;
getLast metodu

İmzası şöyle
public static T getLast(Iterable iterable);
Boş Iterable için NoSuchElementException  fırlatır. Eğer exception fırlatmadan başka bir değer dönsün istersek şöyle yaparız.
Integer last = Iterables.getLast(subgraph.nodes(), null);
indexOf metodu
Şöyle yaparız.
List<WebElement> items;

int i = Iterables.indexOf(items, new Predicate<Item>() {...});
limit metodu
Sadece 3 elemanı almak için şöyle yaparız.
ImmutableList.copyOf(Iterables.limit(sortedSet, 3));
toArray metodu
Normal Java kodunda şöyle yaparız
Foo[] foos = x.toArray(new Foo[x.size()]);
Bu metod biraz daha okunabilirlik sağlıyor.
Örnek
Şöyle yaparız.
Foo[] foos = Iterables.toArray(x, Foo.class);
transform metodu
Şöyle yaparız.
Iterable<String> ids = Iterables.transform(matchingComputers,
  new Function<Computer, String>() {
    public String apply(Computer from) {
      return ...;
    }
}));
unmodifiableIterable metodu
Şöyle yaparız.
final List<Integer> first  = Lists.newArrayList(1, 2, 3);
final List<Integer> second = Lists.newArrayList(4, 5, 6);
final List<Integer> third  = Lists.newArrayList(7, 8, 9);
final Iterable<Integer> all =
    Iterables.unmodifiableIterable(
        Iterables.concat(first, second, third));
System.out.println(all);
third.add(9999999);
System.out.println(all);