我遇到了两个哈希集相交的问题。我不知道为什么一直是0,因此,相似度值为0。
无论如何,我发布了包含交叉点的方法。对应工会的部分工作正常。我很感激你的帮助。
public <T> double Similarity (Set<T> s1, Set<T> s2){
if (s1.isEmpty() || s2.isEmpty()){
return 0.0;
}
double similarity;
int unionValue;
int intersectionValue;
intersectionValue=intersection(s1, s2);
unionValue=union(s1, s2);
similarity=(double)intersectionValue/(double)unionValue;
System.out.println(unionValue+" "+intersectionValue);
System.out.println(similarity);
return similarity;
}
public <T> int intersection (Set<T> s1, Set<T> s2){
Set<T> intersection = new HashSet<T>(s1);
intersection.retainAll(s2);
return intersection.size();
}
public <T> int union (Set<T> s1, Set<T> s2){
Set<T> union = new HashSet<T>(s1);
union.addAll(s1);
for (T apiSwRepo : s2){
if (union.toString().contains(apiSwRepo.toString())){
}
else
union.add(apiSwRepo);
}
return union.size();
}
答案 0 :(得分:3)
你的问题在你的比较中。
if (union.toString().contains(apiSwRepo.toString())) {
} else
union.add(apiSwRepo);
在上面的代码中,您将Set转换为String,然后调用contains()。你应该在union本身上使用.contains(),如下所示:
if (union.contains(apiSwRepo)) {
} else
union.add(apiSwRepo);
我还想建议如下简化:
union.retainAll(s2);
因为你有两个集合,你将从那个简单的陈述中得到联合。
答案 1 :(得分:0)
I don't know the reason yet. But I solved it making this changes to intersection method:
public <T> int intersection (Set<T> s1,Set<T> s2){
Set<T> intersection = new HashSet<T>();
for (T apiSwRepo:s2){
if (s1.toString().contains(apiSwRepo.toString())){
intersection.add(apiSwRepo);
}
}
return intersection.size();
}
As you can see, I did not use the intersection.retainAll
because with my data it did not work, but with Set<String>
test, it did.
Just to be clear, my data-set
was API
information of a HashMap
HashMap<Integer, Set<API>> apisPerSystem = new HashMap<Integer, Set<API>>()