You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java7下如何删除List<TestDTO>中不在另一个List<String>内的元素

Java 7: Remove TestDTO Elements Where getOne() Isn't in Duplicate List

Hey there! Since you're stuck with Java 7 (no fancy Stream API to lean on yet), here are two reliable, safe ways to hit your goal: keeping only the TestDTO elements whose getOne() value exists in the duplicate list, and removing the rest.

First, let's define the sample TestDTO class to match your context:

class TestDTO {
    private String one;

    public TestDTO(String one) {
        this.one = one;
    }

    public String getOne() {
        return one;
    }
}

Method 1: Use Iterator (Safe for In-Place Removal)

This is the standard Java 7 approach to modify a list while iterating—it avoids the dreaded ConcurrentModificationException that pops up when you call list.remove() directly in a loop.

public class Main {
    public static void main(String[] args) {
        List<TestDTO> test = new ArrayList<>();
        test.add(new TestDTO("a")); // dto (keep)
        test.add(new TestDTO("b")); // dto1 (keep)
        test.add(new TestDTO("c")); // dto2 (remove)

        List<String> duplicate = Arrays.asList("a", "b");

        // Convert to HashSet for way faster lookups (optional but recommended for large lists)
        Set<String> duplicateSet = new HashSet<>(duplicate);

        Iterator<TestDTO> iterator = test.iterator();
        while (iterator.hasNext()) {
            TestDTO currentDto = iterator.next();
            if (!duplicateSet.contains(currentDto.getOne())) {
                iterator.remove(); // Safe removal via iterator's built-in method
            }
        }

        // Check the result
        for (TestDTO dto : test) {
            System.out.println(dto.getOne()); // Outputs: a, b
        }
    }
}

Quick tip: Turning duplicate into a HashSet changes contains() from an O(n) operation to O(1)—huge performance boost if your lists are large.

Method 2: Collect Keepers First, Then Replace the List

If iterator syntax feels clunky, you can first gather all the elements you want to keep, then overwrite the original list with this filtered collection:

public class Main {
    public static void main(String[] args) {
        List<TestDTO> test = new ArrayList<>();
        test.add(new TestDTO("a"));
        test.add(new TestDTO("b"));
        test.add(new TestDTO("c"));

        List<String> duplicate = Arrays.asList("a", "b");
        Set<String> duplicateSet = new HashSet<>(duplicate);

        List<TestDTO> keepers = new ArrayList<>();
        for (TestDTO dto : test) {
            if (duplicateSet.contains(dto.getOne())) {
                keepers.add(dto);
            }
        }

        // Replace original list with only the keepers
        test.clear();
        test.addAll(keepers);

        // Verify the outcome
        for (TestDTO dto : test) {
            System.out.println(dto.getOne()); // Outputs: a, b
        }
    }
}

Both methods will give you exactly what you want: dto and dto1 stay, dto2 gets removed.

内容的提问来源于stack exchange,提问作者ArrchanaMohan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:30:56