【发布时间】:2014-09-18 19:43:20
【问题描述】:
我有一个算法要运行,在其中一个步骤中,我必须在名为 (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)。对于 (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);
}
【问题讨论】:
-
覆盖
Pair类的hashCode和equals方法,并使用Collections.frequency()。
标签: java arraylist count duplicates