Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.function.Consumer; public class Main { public static void flattenRecursively(Object toFlat, Consumer consumer) { if (isIterable(toFlat)) { ((Iterable<Object>) toFlat).forEach(item -> flattenRecursively(item, consumer)); } else { consumer.accept(toFlat); } } private static boolean isIterable(Object object) { return object instanceof Iterable; } }