1.使用for循环遍历
public static void main(String[] args) {
List oldList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7);
List newList = Lists.newArrayList();
if (oldList.size() <= limit) {
for (int i = 0; i < limit; i++) {
newList.add(oldList.get(i))
2.使用Stream API
public static void main(String[] args) {
List oldList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7);
List newList = new ArrayList<>(limit);
if (oldList.size() <= limit) {
newList = oldList.stream().limit(limit).collect(Collectors.toList());
3.使用subList方法
public static void main(String[] args) {
List oldList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7);
List newList = new ArrayList<>(limit);
if (oldList.size() <= limit) {
newList = oldList.subList(0,limit);
4.使用Apache Commons Collections
public static void main(String[] args) {
List oldList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7);
List newList = new ArrayList<>(limit);
if (oldList.size() <= limit) {
CollectionUtils.addAll(newList, oldList.iterator());