java8 list 删除元素 简单版

import org.assertj.core.util.Lists;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author :lichuankang
 * @date :2020/9/3 17:03
 * @description :
 */
public class ListMain {

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("测试1");
        list.add("测试2");
        list.add("测试3");
        list.add("测试4");
        list.add("测试5");


        // 方法1
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            if ("测试3".equals(next)) {
                iterator.remove();
            }
        }

        for (String text:list) {
            System.out.println(text);
        }

        System.out.println("------------------------------------------------------------------");
        // 方法2
        list.removeIf("测试2"::equals);
        for (String text:list) {
            System.out.println(text);
        }

    }
}

posted @ 2020-09-04 11:37  lick  阅读(6101)  评论(0编辑  收藏  举报