在O(n)java8流中查找两个列表的交集

时间:2020-04-22 13:21:53

标签: java java-8 java-stream time-complexity intersection

我有两个列表,资源和TableResources。我想根据条件找到两个列表的交集,即两个列表相同的subscriberId +“ _” + tableName。我能够在O(N ^ 2)时间内实现这一目标。我想在O(N)时间使用Java8流执行相同的操作。

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class intersectionStream {
    private static class Resource {
        String subscriberId;
        String tableName;
        List<String> buyers;

        public Resource(String subscriberId, String tableName) {
            this.subscriberId = subscriberId;
            this.tableName = tableName;
        }

        @Override
        public String toString() {
            return "TableResource{" +
                    "subscriberId='" + subscriberId + '\'' +
                    ", tableName='" + tableName + '\'' +
                    ", buyers=" + buyers +
                    '}';
        }

        public String getResourceString() {
            return this.subscriberId + "_" + this.tableName;
        }

    }

    private static class TableResource {
        String subscriberId;
        String tableName;

        public TableResource(String subscriberId, String tableName) {
            this.subscriberId = subscriberId;
            this.tableName = tableName;
        }

        public String getTableResource() {
            return this.subscriberId + "_" + this.tableName;
        }

        @Override
        public String toString() {
            return "GlobalTableResource{" +
                    "subscriberId='" + subscriberId + '\'' +
                    ", tableName='" + tableName + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) {
        List<Resource> resources = new ArrayList<>();
        List<TableResource> tableResources = new ArrayList<>();
        HashSet<String> commonResources = new HashSet<>();

        resources.add(new Resource("1", "table1"));
        resources.add(new Resource("2", "table2"));
        resources.add(new Resource("3", "table3"));
        resources.add(new Resource("3", "table4"));
        resources.add(new Resource("3", "table5"));


        tableResources.add(new TableResource("2", "table2"));
        tableResources.add(new TableResource("3", "table3"));
        tableResources.add(new TableResource("5", "table5"));
        tableResources.add(new TableResource("6", "table6"));

        for(Resource resource : resources) {
            for(TableResource tableResource : tableResources) {
                if(tableResource.getTableResource().equals(resource.getResourceString())) {
                    commonResources.add(tableResource.getTableResource());
                }
            }

        }

        System.out.println("Hashset is : " + commonResources);
    }
}

所需的输出为:哈希集为:[2_table2,3_table3]

2 个答案:

答案 0 :(得分:2)

您可以尝试以下操作,

Set<String> tableResourcesSet = tableResources.stream()
                .map(TableResource::getTableResource)
                .collect(Collectors.toSet());

resources.stream()
        .map(Resource::getResourceString)
        .filter(tableResourcesSet::contains)
        .collect(Collectors.toSet());

答案 1 :(得分:0)

尝试一下:

Set<String> resourceStrings = resources.stream()
                                       .map(Resource::getResourceString)
                                       .collect(toCollection(HashSet::new));

Set<String> commonResources = tableResources.stream()
                                            .map(TableResource::getTableResource)
                                            .filter(resourceStrings::contains)
                                            .collect(toSet());

说明:

收集到HashSet中需要花费线性时间O(n),而HashSet::contains需要花费恒定的O(1)时间。 因此,一旦有了HashSet,您就可以在O(n)的时间内遍历第二个集合。 总体复杂度为O(n + n)O(2n)。大的O表示法抑制了。因此,由此产生的复杂性被认为是O(n)