Alon
Alon

Reputation: 2929

Make ArrayList<LinkedList<String>> into an Iterable

I have an ArrayList<LinkedList> and I want to make one Iterator for all the LinkedLists.

What is the best way to do this?

I found this:

  'final Iterable<Integer> all =
  Iterables.unmodifiableIterable(
  Iterables.concat(first, second, third));'

But I have many LinkedLists. What can I do to combine all of them?

Thanks!

Upvotes: 0

Views: 57

Answers (2)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136062

One simple solution

    ArrayList<LinkedList<String>> l1 = ...
    List<String> l2 = new ArrayList<>();
    for(List<String> e : l1) {
        l2.addAll(e);
    }

Upvotes: 0

VGR
VGR

Reputation: 44404

It's not clear whether you want to do this using only standard Java classes, but if so, you can do it this way as of Java 8:

ArrayList<LinkedList<String>> strings = /* ... */;
Iterator<String> i = strings.stream().flatMap(l -> l.stream()).iterator();

Upvotes: 2

Related Questions