我有一个算法要运行,在其中一个步骤中我必须在名为(PairList)的列表中找到重复对,计算它们并消除小于特定参数(minSupp)的对。这是我的代码用于将对添加到PairList。
for (int i = sizes.get(sequence.getId())-maxError ; i <= sizes.get(sequence.getId())-1; i++){
for(int j = i+1; j<sizes.get(sequence.getId()); j++){
//Get the first Item
int first = sequence.getItemsets().get(i);
//gets the second Item
int second =sequence.getItemsets().get(j);
//Generate pattern as pair
Pair pattern = new Pair(first, second);
sequenceID = sequence.getId();
//Generate triples with sequence ID and pair
Triples triples = new Triples(sequenceID, i, j);
if (!pairList.contains(pattern)){
//adds if it doesn't exist
pairList.add(pattern);
}
现在pairList包含一些像这样的对: (3,28)(3,58)(3,61)(3,28)(5,21)(3,28)(5,21) 我想知道例如在这个列表中出现了多少次(3,28)。并且对于a(minSupp = 2)我想删除少于2次的对,所以输出应该是这样的:
(3, 28) : 3
(3, 58) : 1 (this must be removed)
(3, 61) : 1 remove
(5, 21) : 2
我一直在努力,直到现在这是我的代码,但它给了我一个远远超出我想要的输出所以请求帮助!
for(Pair pair : pairList){
int a = Collections.frequency(pairList, pair);
for (int i=0 ; i<pairList.size() ; i++){
for (int j =i+1 ; j<pairList.size()-1;j++){
if (pairList.get(i).getX()==pairList.get(j).getX() && pairList.get(i).getY()==pairList.get(j).getY() ){
a++;
System.out.println(pair + ": " + a);
}
答案 0 :(得分:0)
Collections.frequency()
, equals
已经足以在列表中找到对的频率。所以你的嵌套for循环是多余的。
可以通过.remove()
简单地删除对。如果您希望每个对只打印一次,您可以将列表添加到 Set 然后迭代它(您还需要实现hashCode
):
for (Pair pair : new HashSet<Pair>(pairList)) { // a set has no double entries
int frequency = Collections.frequency(pairList, pair);
System.out.print(pair + " : " + frequency); // print frequency of pair
if (frequency < minSupp) { // if frequency is too low remove this pair
while (pairList.remove(pair)); // remove all occurrences of this pair
System.out.print(" remove");
}
System.out.println();
}
有关如何正确实施hashCode
和equals
的详细信息,请查看此问题:What issues should be considered when overriding equals and hashCode in Java?