Idiom for pairwise iteration through a sorted collection

前端 未结 5 764
误落风尘
误落风尘 2021-01-03 02:54

Is there a Java idiom for pairwise iteration through the elements of a sorted Collection? By that I mean that each iteration has access to one element of the co

5条回答
  •  醉酒成梦
    2021-01-03 03:35

    Iterator thingerator = coll.iterator();
    if (thingerator.hasNext()) {
        Thing thing1 = thingerator.next();
        while (thingerator.hasNext()) {
          final Thing thing2 = thingerator.next();
          doStuffToThings(thing1, thing2);
    
          thing1 = thing2;
        }
    }
    

提交回复
热议问题