Java: split a List into two sub-Lists?

后端 未结 14 1556
执笔经年
执笔经年 2020-12-08 02:29

What\'s the simplest, most standard, and/or most efficient way to split a List into two sub-Lists in Java? It\'s OK to mutate the original List, so no copying should be nece

14条回答
  •  无人及你
    2020-12-08 02:53

    Quick semi-pseudo code:

    List sub=one.subList(...);
    List two=new XxxList(sub);
    sub.clear(); // since sub is backed by one, this removes all sub-list items from one
    

    That uses standard List implementation methods and avoids all the running around in loops. The clear() method is also going to use the internal removeRange() for most lists and be much more efficient.

提交回复
热议问题