Java: repeating a list of lists?

Question, but in C #. So, is there a C # command in Java? I need this for a Matches-SearchTerm-Files relationship.

foreach(var i in BunchOfItems.SelectMany(k => k.Items)) {} 

[Why not for loops?] I made such structures in nested loops, but soon they swell. Therefore, I prefer something more than higher.

 public static Stack<Integer[]> getPrintPoss(String s,File f,Integer maxViewPerF) { Stack<File> possPrint = new Stack<File>(); Integer[] poss = new Integer[4](); int u,size; for(File f:files) { size = f2S(f).length(); u = Math.min(maxViewsPerF,size); for(int i=0; i<u;i++) { // Do something --- bloated, and soon out of control // wants more succintly } } return possPrint; } 
+6
java list iteration
source share
6 answers

Get about half a year of patience until the JDK7 is final, which will include Closures . This provides a simple syntax and the same features as LINQ that were demonstrated in the answer you are talking about.

+1
source share
 for (List<Object> lo : list) { for (Object o : lo) { // etc etc } } 

I do not think there is a simpler solution.

+6
source share

If you can get the data in Iterable<Iterable<T>> , you can get from that to squashed Iterable<T> using the Guava Iterables.concat method. If you really have an Iterable<S> , with somehow getting from S to Iterable<T> , well then you should first use Iterables.transform to view this as Iterable<Iterable<T>> needed for concat .

All this will look much nicer if and when Java something reminds closures, but at least today it is possible.

http://guava-libraries.googlecode.com

+6
source share

With Java 8 you can say

 Collection bunchOfItems = ...; bunchOfItems.stream().flatMap(k::getItems).forEach(i -> /* operate on i */); 

or

 Item[] bunchOfItems = ...; Stream.of(bunchOfItems).flatMap(k::getItems).forEach(i -> /* operate on i */); 

depending on whether you have Collection or Array .

+3
source share

I have my own version. The wait is desperate for Closures in Java:

 public static <T, E> Iterable<T> transformMany(Iterable<E> iterable, Func<E, Iterable<T>> f) { if (null == iterable) throw new IllegalArgumentException("null iterable"); if (null == f) throw new IllegalArgumentException("null f"); return new TransformManyIterable<E, T>(iterable, f); } public interface Func<E, T> { T execute(E e); } public class TransformManyIterable<TOriginal, TResult> implements Iterable<TResult> { private Iterable<TOriginal> iterable; private Func<TOriginal, Iterable<TResult>> func; public TransformManyIterable(Iterable<TOriginal> iterable, Func<TOriginal, Iterable<TResult>> func) { super(); this.iterable = iterable; this.func = func; } class TransformIterator implements Iterator<TResult> { private Iterator<TOriginal> iterator; private Iterator<TResult> currentIterator; public TransformIterator() { iterator = iterable.iterator(); } @Override public boolean hasNext() { if (currentIterator != null && currentIterator.hasNext()) return true; else { while (iterator.hasNext()) { Iterable<TResult> iterable = func.execute(iterator.next()); if (iterable == null) continue; currentIterator = iterable.iterator(); if (currentIterator.hasNext()) return true; } } return false; } @Override public TResult next() { if (currentIterator != null && currentIterator.hasNext()) return currentIterator.next(); else { while (iterator.hasNext()) { Iterable<TResult> iterable = func.execute(iterator.next()); if (iterable == null) continue; currentIterator = iterable.iterator(); if (currentIterator.hasNext()) return currentIterator.next(); } } throw new NoSuchElementException(); } @Override public void remove() { throw new UnsupportedOperationException(); } } @Override public Iterator<TResult> iterator() { return new TransformIterator(); } } 

Using:

  Iterable<SomeType> result = transformMany(input, new Func<InputType, Iterable<SomeType>>() { @Override public Iterable<SomeType> execute(InputType e) { return new ArrayList<SomeType>(); } }); 
+1
source share

The SelectMany method is part of LINQ, which is .Net-specific. This question asks about the LINQ equivalent for java. Unfortunately, this does not look like a direct equivalent.

+1
source share

All Articles