Java: split a List into two sub-Lists?

后端 未结 14 1562
执笔经年
执笔经年 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:48

    My solution:

    List listToSplit = new ArrayList();
    
    List list1 = new ArrayList();
    List list2 = new ArrayList();
    
    for (X entry : listToSplit)
    {
        if (list1.size () > list2.size ())
            list2.add (entry);
        else
            list1.add( entry );
    }
    

    Should work :)

提交回复
热议问题