Iterable<Integer> it = new Yielder<Integer>() { @Override protected void yieldNextCore() { for (int i = 0; i < 10; i++) { yieldReturn(i); if (i == 5) yieldBreak(); } } };
Iterable<Integer> it = new Generator<Integer>() { @Override protected void run() { for (int i = 0; i < 10; i++) { yield(i); if (i == 5) return; } } };
String comparableClassName = ...; // the class name of the objects you wish to compare String comparatorClassName = ...; // something random to avoid class name conflicts String source = "public class " + comparatorClassName + " implements Comparable<" + comparableClassName + "> {" + " public int compare(" + comparableClassName + " a, " + comparableClassName + " b) {" + " return " + expression + ";" + " }" + "}"; JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); /* * Please refer to the JavaCompiler JavaDoc page for examples of the following objects (most of which can remain null) */ Writer out = null; JavaFileManager fileManager = null; DiagnosticListener<? super JavaFileObject> diagnosticListener = null; Iterable<String> options = null; Iterable<String> classes = null; Iterable<? extends JavaFileObject> compilationUnits = new ArrayList<? extends JavaFileObject>(); compilationUnits.add( new SimpleJavaFileObject() { // See the JavaDoc page for more details on loading the source String } ); compiler.getTask(out, fileManager, diagnosticListener, options, classes, compilationUnits).call(); Comparator comparator = (Comparator) Class.forName(comparableClassName).newInstance();
private Iterable<Car> getCarsByBrands(List<Brand> brands) { Iterable<Iterable<Car>> carsGroupedByBrand = new Arraylist<>(); for(Brand brand : brands) { carsGroupedByBrand.add(retrieveCarsByBrand(brand)); } return Iterables.concat(carsGroupedByBrand); }
Iterable<File> filesIterable = new Iterable<>(){ public Iterator<File> iterator() { return FileUtils.iterateFiles(...); } }; ... for (File file : filesIterable) { ... } // ... and you may reuse it over and over. // For each cycle declaration there will be a new `Iterator` created. for (File file : filesIterable) { ... }
Iterable<Integer> numbers = new Range(1, 100); Iterable<Integer> primes = numbers.inject(numbers, new Functions.Injecter<Iterable<Integer>, Integer>() { public Iterable<Integer> call(Iterable<Integer> numbers, final Integer number) throws Exception { // We don't test for 1 which is implicit if ( number <= 1 ) { return numbers; } // Only keep in numbers those that do not divide by number return numbers.reject(new Functions.Predicate1<Integer>() { public Boolean call(Integer n) throws Exception { return n > number && n % number == 0; } }); } });
Iterable<String> reverse = new IteratorIterable(new ReverseListIterator(stringList)); for(String string: reverse ){ //...do something }
Iterable<String> iterable = new SomeIterable<String>(); // ... List<String> sorted = StreamSupport.stream(iterable.spliterator(), false) .sorted() .collect(Collectors.toList());
// implement a logic the yield-style Iterable<Integer> y=new Yield<Integer>() { protected void produce(Flow<Integer> f) { for (int i = min; i < max; i++) { for (int j = min; j < max; j++) { f.yield(i * j); } } } }; // use the Iterable, e.g. in a for-each loop int maxPalindrome=0; for(int i:y) if(isPalindrome(i) && i>maxPalindrome) maxPalindrome=i; System.out.println(maxPalindrome);
Vector<Integer> numbers = new Vector<Integer>(); numbers.add(42); numbers.add(3); numbers.add(16); numbers.add(92); numbers.add(9); Iterable<Integer> filtered = new Where<Integer>(numbers) { protected boolean predicate(Integer i) { return i > 10; } }; Iterable<String> converted = new Select<Integer, String>(filtered) { protected String select(Integer i) { return i.toString(); } }; for (final String str : converted) System.out.println(str);