我有两个列表,资源和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]
答案 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)
。