Is it possible to merge iterators in Java?

后端 未结 14 1630
自闭症患者
自闭症患者 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:01

    every Iterator object holds own memory location (adress), so you can't simply "merge" them. except if you extend iterator class and write your own implementation there.

    If you are dealing with the same number of objects in both iterators an alternative solution would be to process two iterators in one loop like this :

       while (iterator1.hasNext() && iterator2.hasNext()) {
          // code
        }
    

提交回复
热议问题