java.lang.Iterable类的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(5.1k)|赞(0)|评价(0)|浏览(158)

本文整理了Java中java.lang.Iterable类的一些代码示例,展示了Iterable类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Iterable类的具体详情如下:
包路径:java.lang.Iterable
类名称:Iterable

Iterable介绍

[英]Instances of classes that implement this interface can be used with the enhanced for loop.
[中]实现此接口的类的实例可以与增强的for循环一起使用。

代码示例

代码示例来源:origin: iluwatar/java-design-patterns

@Override
public Iterator<E> iterator() {
 return iterable.iterator();
}

代码示例来源:origin: iluwatar/java-design-patterns

@Override
public Spliterator<E> spliterator() {
 return iterable.spliterator();
}

代码示例来源:origin: iluwatar/java-design-patterns

@Override
public void forEach(Consumer<? super E> action) {
 iterable.forEach(action);
}

代码示例来源:origin: google/guava

@Override
 public Iterator<E> iterator() {
  return iterable.iterator();
 }
};

代码示例来源:origin: google/guava

@Override
public void forEach(Consumer<? super T> action) {
 checkNotNull(action);
 fromIterable.forEach((F f) -> action.accept(function.apply(f)));
}

代码示例来源:origin: google/guava

@SuppressWarnings("unchecked") // safe upcast, assuming no one has a crazy Spliterator subclass
@Override
public Spliterator<T> spliterator() {
 return (Spliterator<T>) iterable.spliterator();
}

代码示例来源:origin: google/guava

@Override
 public Iterator<? extends T> apply(Iterable<? extends T> iterable) {
  return iterable.iterator();
 }
};

代码示例来源:origin: google/guava

@Override
public void forEach(Consumer<? super T> action) {
 checkNotNull(action);
 unfiltered.forEach(
   (T a) -> {
    if (retainIfTrue.test(a)) {
     action.accept(a);
    }
   });
}

代码示例来源:origin: google/guava

/**
 * Returns a sequential {@link Stream} of the contents of {@code iterable}, delegating to {@link
 * Collection#stream} if possible.
 */
public static <T> Stream<T> stream(Iterable<T> iterable) {
 return (iterable instanceof Collection)
   ? ((Collection<T>) iterable).stream()
   : StreamSupport.stream(iterable.spliterator(), false);
}

代码示例来源:origin: google/guava

/**
 * Converts an iterable into a collection. If the iterable is already a collection, it is
 * returned. Otherwise, an {@link java.util.ArrayList} is created with the contents of the
 * iterable in the same iteration order.
 */
private static <E> Collection<E> castOrCopyToCollection(Iterable<E> iterable) {
 return (iterable instanceof Collection)
   ? (Collection<E>) iterable
   : Lists.newArrayList(iterable.iterator());
}

代码示例来源:origin: google/guava

static String iterationOrder(Iterable<? extends Node> iterable) {
 StringBuilder builder = new StringBuilder();
 for (Node t : iterable) {
  builder.append(t.value);
 }
 StringBuilder forEachBuilder = new StringBuilder();
 iterable.forEach(t -> forEachBuilder.append(t.value));
 assertTrue(
   "Iterator content was " + builder + " but forEach content was " + forEachBuilder,
   builder.toString().contentEquals(forEachBuilder));
 return builder.toString();
}

代码示例来源:origin: google/guava

@Override
 public Spliterator<T> spliterator() {
  return CollectSpliterators.map(fromIterable.spliterator(), function);
 }
};

代码示例来源:origin: google/guava

private static boolean isEmpty(Iterable<?> iterable) {
 return iterable instanceof Collection
   ? ((Collection<?>) iterable).isEmpty()
   : !iterable.iterator().hasNext();
}

代码示例来源:origin: google/guava

public void testUnmodifiableIterable_forEach() {
 List<String> list = newArrayList("a", "b", "c", "d");
 Iterable<String> iterable = Iterables.unmodifiableIterable(list);
 Iterator<String> expectedIterator = list.iterator();
 iterable.forEach(s -> assertEquals(expectedIterator.next(), s));
 assertFalse(expectedIterator.hasNext());
}

代码示例来源:origin: google/guava

@Override
 public Spliterator<T> spliterator() {
  return CollectSpliterators.filter(unfiltered.spliterator(), retainIfTrue);
 }
};

代码示例来源:origin: google/guava

@Override
public T next() {
 if (!iterator.hasNext()) {
  iterator = iterable.iterator();
  if (!iterator.hasNext()) {
   throw new NoSuchElementException();
  }
 }
 return iterator.next();
}

代码示例来源:origin: google/guava

@GwtIncompatible // Iterables.filter(Iterable, Class)
public void testFilterByType_forEach() throws Exception {
 HasBoth hasBoth1 = new HasBoth();
 HasBoth hasBoth2 = new HasBoth();
 Iterable<TypeA> alist = Lists.newArrayList(hasBoth1, new TypeA(), hasBoth2, new TypeA());
 Iterable<TypeB> blist = Iterables.filter(alist, TypeB.class);
 Iterator<TypeB> expectedIterator = Arrays.<TypeB>asList(hasBoth1, hasBoth2).iterator();
 blist.forEach(b -> assertThat(b).isEqualTo(expectedIterator.next()));
 assertThat(expectedIterator.hasNext()).isFalse();
}

代码示例来源:origin: google/guava

@Override
protected boolean doEquivalent(Iterable<T> iterableA, Iterable<T> iterableB) {
 Iterator<T> iteratorA = iterableA.iterator();
 Iterator<T> iteratorB = iterableB.iterator();
 while (iteratorA.hasNext() && iteratorB.hasNext()) {
  if (!elementEquivalence.equivalent(iteratorA.next(), iteratorB.next())) {
   return false;
  }
 }
 return !iteratorA.hasNext() && !iteratorB.hasNext();
}

代码示例来源:origin: iluwatar/java-design-patterns

/**
 * Collects all remaining objects of this Iterable into a list.
 * 
 * @return a list with all remaining objects of this Iterable
 */
@Override
public List<E> asList() {
 return toList(iterable.iterator());
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Iterator<Flowable<T>> iterator() {
    return new ToFlowableIterator<T>(sources.iterator());
  }
}

相关文章