2大型列表只需要过滤和获取不同的项

js5cn81o  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(310)

我有两个巨大的清单,其中超过数百万的项目。我不能共享生产代码。但我可以通过以下方式模拟实际代码。其中我有几个相同的id和其他不同的id。我想有一个列表,将只有不同的id元素。我已经用经典的java解决了这个问题,没有使用流api。在性能方面,它是足够好的,当我有数以百万计的项目。我该如何改进code:-

public class TestClass {
    private String id;
    private LocalDate creationTimestamp;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public LocalDate getCreationTimestamp() {
        return creationTimestamp;
    }

    public void setCreationTimestamp(LocalDate creationTimestamp) {
        this.creationTimestamp = creationTimestamp;
    }
}

    TestClass testClass = new TestClass();
    testClass.setId("1");
    testClass.setCreationTimestamp(LocalDate.now());
    TestClass testClass2 = new TestClass();
    testClass2.setId("2");
    testClass.setCreationTimestamp(LocalDate.now());
    TestClass testClass3 = new TestClass();
    testClass3.setId("3");
    testClass.setCreationTimestamp(LocalDate.now());
    TestClass testClass4 = new TestClass();
    testClass4.setId("4");
    testClass.setCreationTimestamp(LocalDate.now());

    List<TestClass> testClassesList1 = new ArrayList<>();
    testClassesList1.add(testClass);
    testClassesList1.add(testClass2);
    testClassesList1.add(testClass3);
    testClassesList1.add(testClass4);

    TestClass testClass5 = new TestClass();
    testClass5.setId("1");
    testClass5.setCreationTimestamp(LocalDate.now());
    TestClass testClass6 = new TestClass();
    testClass6.setId("2");
    testClass6.setCreationTimestamp(LocalDate.now());
    TestClass testClass7 = new TestClass();
    testClass7.setId("5");
    testClass7.setCreationTimestamp(LocalDate.now());
    TestClass testClass8 = new TestClass();
    testClass8.setId("6");
    testClass8.setCreationTimestamp(LocalDate.now());

    List<TestClass> testClassesList2 = new ArrayList<>();
    testClassesList2.add(testClass5);
    testClassesList2.add(testClass6);
    testClassesList2.add(testClass7);
    testClassesList2.add(testClass8);

    List<TestClass> uniqueTestClasses = new ArrayList<>();

    if(testClassesList1.size() == testClassesList2.size()) {
        for (int i = 0; i < testClassesList1.size(); i++) {
            if(testClassesList1.get(i).getId().equalsIgnoreCase(
                    testClassesList2.get(i).getId())){
                uniqueTestClasses.add(testClassesList1.get(i));
            }else{
                uniqueTestClasses.add(testClassesList1.get(i));
                uniqueTestClasses.add(testClassesList2.get(i));
            }
        }
    }

这个很好用。如果尺寸不同,那么解决方案是什么?但就表现而言并不好。我如何改进它以实现与streamapi相同的目标。

xzlaal3s

xzlaal3s1#

当您需要使用hashset方法的代码片段时,下面是一个。

public List<TestClass> checker(List<TestClass> ls1, List<TestClass> ls2){

    //Creating HashSet to store ids
    HashSet<Integer> set = new HashSet<>();

    //Finding the smaller list
    List<TestClass> smallerList = ls1.size() <= ls2.size() ? ls1 : ls2; 
    List<TestClass> biggerList = ls1.size() > ls2.size() ? ls1 : ls2;

    //Adding smaller list values to the HashSet
    for(TestClass tc : smallerList)
        set.add(tc.getId());

    //Looping through bigger list and searching and removing data
    Iterator<TestClass> iter = biggerList.iterator();
    while(iter.hasNext()){
        TestClass t = iter.next();

        //Checking if the object is in smallerList
        if(set.contains(t.getId()))
            iter.remove();
    }
    return biggerList;
}

我还没有测试过这个代码。它可能有一些语法错误。

相关问题