Java tutorial
//package com.java2s; import java.util.Collection; import java.util.Map; import java.util.function.BiConsumer; import java.util.function.Consumer; public class Main { public static <E> void forEach(Collection<E> c, Consumer<? super E> action) { if (isNullOrEmpty(c)) { return; } for (E item : c) { action.accept(item); } } public static <K, V> void forEach(Map<K, V> m, BiConsumer<K, V> consumer) { if (isNullOrEmpty(m)) { return; } for (Map.Entry<K, V> entry : m.entrySet()) { consumer.accept(entry.getKey(), entry.getValue()); } } public static boolean isNullOrEmpty(Collection<?> c) { return c == null || c.isEmpty(); } public static <K, V> boolean isNullOrEmpty(Map<K, V> map) { return map == null || map.isEmpty(); } }