Java中拆除一个列表为两个

上页

  Java中将一个列表拆除为两个List,第一个列表的长度为指定的长度:

import java.util.AbstractMap.SimpleEntry;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;
 
import static java.util.stream.Collectors.*;
 
/**
 * <b>(*) Split a list into two parts; the length of the first part is given</b>
 */
public class P17 {
 
    public static <T> Map<Boolean, List<T>> split(List<T> list, int n) {
        return IntStream
                .range(0, list.size())
                .mapToObj(i -> new SimpleEntry<>(i, list.get(i)))
                .collect(partitioningBy(entry -> entry.getKey() < n, mapping(SimpleEntry::getValue, toList())));
    }
}

 

下一篇:Java列表List的列表List排序