0

我有以下代码:

       List<HashSet<String>> authorLists = new List<HashSet<String>>
       // fill it
        /** Remove duplicate authors  */
        private void removeDublicateAuthors(HashSet<String> newAuthors, int curLevel)
        {

            for (int i = curLevel - 1; i > 0; --i)
            {
                HashSet<String> authors = authorLists[i];
                foreach (String item in newAuthors)
                {
                    if (authors.Contains(item))
                    {
                        newCoauthors.Remove(item);
                    }
                }
            }
        }

如何正确删除物品?我需要遍历 newAuthors 和 authorLists。由于这个原因,此处不能使用 RemoveWhere。

创建新列表、向其中添加项目然后删除重复项目非常低效。就我而言,authorLists 列表具有以下大小:

authorLists [0].size = 0;
authorLists [1].size = 322;
authorLists [2].size = 75000; // (even more than this value)

我需要调用 removeDublicateAuthors 1*(1) 322 (n) 75000 (m) 次,其中 n 和 m 分别是第一级和第二级重复作者的大小。我必须经常删除这些项目,并且数组的大小非常大。所以,这个算法效率很低。实际上,我在 Java 中有以下代码,并出于某些原因对其进行了重写:

/** 删除作者树中的重复作者*/

private void removeDublicateAuthors(HashSet<String> newCoauthors, int curLevel ) {

for(int i = curLevel - 1; i > 0; --i) {
    HashSet<String> authors = coauthorLevels.get(i);
    for (Iterator<String> iter = newCoauthors.iterator(); iter.hasNext();) {
        iter.next();
        if(authors.contains(iter)) {
            iter.remove();
        }
    }
}
}

它的工作速度比目前建议的选项快得多

4

4 回答 4

3

您可以在另一个哈希集中添加要删除的项目,然后将它们全部删除。

于 2014-03-18T09:16:36.300 回答
1

您在这里所做的事情是错误的,原因有两个:1.您无法更改正在解析的集合-sintax问题2.即使您使代码正常工作,您也只会更改值,而不是引用-逻辑问题

   List<HashSet<String>> authorLists = new List<HashSet<String>>
   // fill it
   /** Remove duplicate authors  */
   // handle reference instead of value
   private void removeDublicateAuthors(ref HashSet<String> newAuthors, int curLevel)
   {
       List<string> removeAuthors = new List<string>();

       for (int i = curLevel - 1; i > 0; --i)
       {
           HashSet<String> authors = authorLists[i];
           foreach (String item in newAuthors)
           {
               if (authors.Contains(item))
               {
                   removeAuthors .Add(item);
               }
           }
       }

       foreach(string author in removeAuthors)
       {
           newAuthors.Remove(author);
       }
   }
于 2014-03-18T09:46:26.377 回答
0

您正在寻找的是ExceptWith. 您正在尝试找到从另一个集合中减去的集合,这正是该方法所做的。

于 2014-03-18T18:13:18.283 回答
-1

如果我不明白您要做什么,请原谅我。

哈希集不允许重复,因为项目的索引是项目的哈希。两个相等的字符串将具有相同的散列,因此具有相同的索引。因此,如果您简单地组合任意两个哈希集,则结果不会重复。

考虑以下:

        var set1 = new HashSet<string>();
        set1.Add("foo");
        set1.Add("foo");

        var set2 = new HashSet<string>();
        set2.Add("foo");

        var set3 = set1.Union(set2);

        foreach (var val in set3)
        {
          Console.WriteLine(val);   
        }

此代码的输出将是:

foo

现在,如果您试图确保哈希集 A 不包含哈希集 B 中的任何项目,您可以执行以下操作:

        var set1 = new HashSet<string>();
        set1.Add("foo");
        set1.Add("bar");

        var set2 = new HashSet<string>();
        set2.Add("foo");
        set2.Add("baz");

        foreach (var val in set2)
        {
            set1.Remove(val);
        }

        foreach (var val in set1)
        {
            Console.WriteLine(val);    
        }

其输出将是:

bar

考虑一下,您可以使用 .Except 方法从另一组中减去一组。

var set3 = set1.Except(set2);

这将产生 set1 中不在 set2 中的所有项目

于 2014-03-18T18:04:37.610 回答