guava iterable example
public class IteratorTest { public static void main(String[] args) { test(); } public static void test() { final Function<String, Iterable<String>> function = new Function<String, Iterable<String>>() { @Override public Iterable<String> apply(final String input) { return new Iterable<String>() { @Override public Iterator<String> iterator() { return new AbstractIterator<String>() { int start = 0; int end = input.length(); @Override protected String computeNext() { if (start > end - 1) { return endOfData(); } return String.valueOf(input.charAt(start++)); } }; } }; } }; List<String> names = Lists.newArrayList("123", "456", "789", "abc"); Function<List<String>, Iterable<Iterable<String>>> fun = new Function<List<String>, Iterable<Iterable<String>>>() { @Override public Iterable<Iterable<String>> apply(final List<String> input) { return new Iterable<Iterable<String>>() { @Override public Iterator<Iterable<String>> iterator() { return new AbstractIterator<Iterable<String>>() { int size = 0; @Override protected Iterable<String> computeNext() { if (size > input.size() - 1) { return endOfData(); } Iterable<String> apply = function.apply(input.get(size++)); Preconditions.checkNotNull(apply); return apply; } }; } }; } }; Function<Iterable<Iterable<String>>, Iterable<String>> convert = new Function<Iterable<Iterable<String>>, Iterable<String>>() { @Override public Iterable<String> apply(final Iterable<Iterable<String>> iterators) { return new Iterable<String>() { @Override public Iterator<String> iterator() { return new AbstractIterator<String>() { Iterator<Iterable<String>> outerIterator = iterators.iterator(); Iterator<String> innerIterator = null; @Override protected String computeNext() { if (innerIterator == null) { if (!outerIterator.hasNext()) { return endOfData(); } innerIterator = outerIterator.next().iterator(); } while (innerIterator.hasNext()) { return innerIterator.next(); } innerIterator = null; return computeNext(); } }; } }; } }; //每次外部的iterator便利一次就回不去了,所以为了每次获取到都是从0开始的,需要包一层 Function<Iterator<String>, Iterable<String>> con = new Function<Iterator<String>, Iterable<String>>() { @Override public Iterable<String> apply(final Iterator<String> input) { return new Iterable<String>() { @Override public Iterator<String> iterator() { return new AbstractIterator<String>() { Iterator<String> inner = input; @Override protected String computeNext() { while (inner.hasNext()) { return inner.next(); } return endOfData(); } }; } }; } }; Iterable<Iterable<String>> res = fun.apply(names); Iterable<String> iterators = convert.apply(res); for(String name:iterators){ System.out.println(name); } System.out.println(Iterables.size(iterators)); //直接转换 Iterables.concat(res); } public void test2() { Function<String, Iterable<String>> function = new Function<String, Iterable<String>>() { @Override public Iterable<String> apply(final String input) { return new Iterable<String>() { @Override public Iterator<String> iterator() { return new AbstractIterator<String>() { int start = 0; int end = input.length(); @Override protected String computeNext() { if (start > end - 1) { return endOfData(); } return String.valueOf(input.charAt(start++)); } }; } }; } }; Iterable<String> aa = function.apply("12345"); Preconditions.checkNotNull(aa); for (String a : aa) { System.out.println(a); } Iterable<String> bb = Iterables.filter(aa, new Predicate<String>() { @Override public boolean apply(String input) { return !StringUtils.equals(input, "5"); } }); Iterable<String> cc = Iterables.concat(aa, bb); Preconditions.checkNotNull(cc); } }