Is it possible to merge iterators in Java?

后端 未结 14 1816
自闭症患者
自闭症患者 2020-11-30 08:14

Is it possible to merge iterators in Java? I have two iterators and I want to combine/merge them so that I could iterate though their elements in one go (in same loop) rathe

14条回答
  •  一生所求
    2020-11-30 09:00

    You can try ConcatIterator from Cactoos:

    Iterator names = new ConcatIterator<>(
      Arrays.asList("Sarah", "Mary").iterator(),
      Arrays.asList("Jeff", "Johnny").iterator(),
    );
    

    Also check ConcatIterable, which concatenates Iterables.

提交回复
热议问题