java.lang.Iterable.forEach()方法的使用及代码示例

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

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

Iterable.forEach介绍

[英]Performs the given action for each element of the Iterableuntil all elements have been processed or the action throws an exception. Unless otherwise specified by the implementing class, actions are performed in the order of iteration (if an iteration order is specified). Exceptions thrown by the action are relayed to the caller.
[中]对ITerableun的每个元素执行给定操作,直到所有元素都已处理或该操作引发异常。除非实现类另有规定,否则操作将按迭代顺序执行(如果指定了迭代顺序)。操作引发的异常将转发给调用方。

代码示例

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

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

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

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

代码示例来源:origin: prestodb/presto

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

代码示例来源: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

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

代码示例来源:origin: apache/incubator-druid

public DruidServer addDataSegments(DruidServer server)
{
 server.getSegments().forEach(this::addDataSegment);
 return this;
}

代码示例来源:origin: eclipse-vertx/vert.x

public static void validateHeader(CharSequence name, Iterable<? extends CharSequence> values) {
 validateHeaderName(name);
 values.forEach(HEADER_VALUE_VALIDATOR);
}

代码示例来源:origin: neo4j/neo4j

private Map<AdminCommandSection,List<AdminCommand.Provider>> groupProvidersBySection()
  {
    List<AdminCommand.Provider> providers = new ArrayList<>();
    commands.getAllProviders().forEach( providers::add );
    return providers.stream().collect( Collectors.groupingBy( AdminCommand.Provider::commandSection ) );
  }
}

代码示例来源:origin: prestodb/presto

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

代码示例来源:origin: prestodb/presto

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

代码示例来源: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: spring-projects/spring-framework

private static int count(Iterable<Node> nodes) {
  assertNotNull(nodes);
  AtomicInteger count = new AtomicInteger();
  nodes.forEach(n -> count.incrementAndGet());
  return count.get();
}

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

public void testTransform_forEach() {
 List<Integer> input = asList(1, 2, 3, 4);
 Iterable<String> result =
   Iterables.transform(
     input,
     new Function<Integer, String>() {
      @Override
      public String apply(Integer from) {
       return Integer.toBinaryString(from);
      }
     });
 Iterator<String> expectedIterator = asList("1", "10", "11", "100").iterator();
 result.forEach(s -> assertEquals(expectedIterator.next(), s));
 assertFalse(expectedIterator.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: Netflix/zuul

private static void writeBufferedBodyContent(final HttpRequestMessage zuulRequest, final Channel channel) {
  zuulRequest.getBodyContents().forEach((chunk) -> {
    channel.write(chunk.retain());
  });
}

代码示例来源:origin: spring-projects/spring-framework

public void processGroupImports() {
  for (DeferredImportSelectorGrouping grouping : this.groupings.values()) {
    grouping.getImports().forEach(entry -> {
      ConfigurationClass configurationClass = this.configurationClasses.get(
          entry.getMetadata());
      try {
        processImports(configurationClass, asSourceClass(configurationClass),
            asSourceClasses(entry.getImportClassName()), false);
      }
      catch (BeanDefinitionStoreException ex) {
        throw ex;
      }
      catch (Throwable ex) {
        throw new BeanDefinitionStoreException(
            "Failed to process import candidates for configuration class [" +
                configurationClass.getMetadata().getClassName() + "]", ex);
      }
    });
  }
}

代码示例来源: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

public void testForEach() {
  for (List<Integer> contents : SAMPLE_INPUTS) {
   C unfiltered = createUnfiltered(contents);
   C filtered = filter(unfiltered, EVEN);
   List<Integer> foundElements = new ArrayList<>();
   filtered.forEach(
     (Integer i) -> {
      assertTrue("Unexpected element: " + i, EVEN.apply(i));
      foundElements.add(i);
     });
   assertEquals(ImmutableList.copyOf(filtered), foundElements);
  }
 }
}

代码示例来源:origin: apache/incubator-druid

@Test
 public void testPropertiesWithRestrictedConfigs()
 {
  Injector injector = Guice.createInjector(Collections.singletonList(new PropertiesModule(Collections.singletonList(
    "status.resource.test.runtime.properties"))));
  Map<String, String> returnedProperties = injector.getInstance(StatusResource.class).getProperties();
  Set<String> hiddenProperties = new HashSet<>();
  Splitter.on(",").split(returnedProperties.get("druid.server.hiddenProperties")).forEach(hiddenProperties::add);
  hiddenProperties.forEach((property) -> Assert.assertNull(returnedProperties.get(property)));
 }
}

代码示例来源:origin: Netflix/zuul

private static void writeBufferedBodyContent(final HttpRequestMessage zuulRequest, final Channel channel) {
  zuulRequest.getBodyContents().forEach((chunk) -> {
    channel.write(chunk.retain());
  });
}

相关文章