Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 删除列表中项目的最快方法_C#_List_Collections_Filtering - Fatal编程技术网

C# 删除列表中项目的最快方法

C# 删除列表中项目的最快方法,c#,list,collections,filtering,C#,List,Collections,Filtering,我有一个用户对象列表,我必须从列表中删除一个具有特定用户ID的项 此方法必须尽可能快,目前我正在循环检查每个项目,检查ID是否与用户ID匹配,如果不匹配,则将该行添加到my filteredList集合中 List allItems = GetItems(); for(int x = 0; x < allItems.Count; x++) { if(specialUserID == allItems[x].ID) continue; else

我有一个用户对象列表,我必须从列表中删除一个具有特定用户ID的项

此方法必须尽可能快,目前我正在循环检查每个项目,检查ID是否与用户ID匹配,如果不匹配,则将该行添加到my filteredList集合中

List allItems = GetItems();

for(int x = 0; x < allItems.Count; x++)
{
    if(specialUserID == allItems[x].ID)
        continue;
    else
        filteredItems.Add( allItems[x] );
}
List allItems=GetItems();
对于(int x=0;x
如果必须尽可能快,请使用不同的数据结构。列表的删除效率不得而知。一个将ID映射到用户的字典怎么样?

如果它真的必须尽可能快,请使用不同的数据结构。列表的删除效率不得而知。一个将ID映射到用户的字典怎么样?

我知道它不是最快的,但是通用列表和删除()呢?(). 有人知道它与问题中的示例相比的性能如何吗?

我知道它不是最快的,但是关于generic list and remove()呢?(). 有人知道它与问题中的示例相比表现如何吗?

使用哈希表。如果使用具有最小冲突可能性的良好散列算法,则所有查找时间都为O(1)。我建议使用哈希表实现IDictionary。如果使用具有最小冲突可能性的良好散列算法,则所有查找时间都为O(1)。我建议您使用一些实现IDictionary的工具。如果您想创建一个新的集合来保持原始集合不变,那么您必须遍历所有项目

从一开始就创建具有适当容量的新列表,以最小化分配

您使用
continue
的程序逻辑似乎有点落后。。。就用这个吧!=运算符而不是==运算符:

List<User> allItems = GetItems();

List<User> filteredItems = new List<User>(allItems.Count - 1);

foreach (User u in allItems) {
   if(u.ID != specialUserID) {
      filteredItems.Add(u);
   }
}
List allItems=GetItems();
List filteredItems=新列表(allItems.Count-1);
foreach(allItems中的用户u){
如果(u.ID!=specialUserID){
添加(u);
}
}

如果要更改原始集合而不是创建新集合,则将项目存储在
字典中将是最快的选择。定位项目和删除项目都接近O(1)操作,因此整个操作将接近O(1)操作,而不是O(n)操作。

如果要创建新集合以保持原始集合不变,则必须循环所有项目

从一开始就创建具有适当容量的新列表,以最小化分配

您使用
continue
的程序逻辑似乎有点落后。。。就用这个吧!=运算符而不是==运算符:

List<User> allItems = GetItems();

List<User> filteredItems = new List<User>(allItems.Count - 1);

foreach (User u in allItems) {
   if(u.ID != specialUserID) {
      filteredItems.Add(u);
   }
}
List allItems=GetItems();
List filteredItems=新列表(allItems.Count-1);
foreach(allItems中的用户u){
如果(u.ID!=specialUserID){
添加(u);
}
}

如果要更改原始集合而不是创建新集合,则将项目存储在
字典中将是最快的选择。定位项目和删除项目都接近于O(1)操作,因此这将使整个操作接近于O(1)操作,而不是O(n)操作。

这里有一个想法,不删除项目本身如何。我的意思是这样的:

public static IEnumerable<T> LoopWithExclusion<T>(this IEnumerable<T> list, Func<T,bool> excludePredicate)
{
   foreach(var item in list)
   {
      if(excludePredicate(item))
      {
         continue;
      }

      yield return item;
   }
}
List<User> users = GetUsers();

//later in the code when you need the filtered list:

foreach(var user in users.LoopWithExclusion(u => u.Id == myIdToExclude))
{
   //do what you gotta do
}
公共静态IEnumerable LoopWithExclution(此IEnumerable列表,Func excludePredicate)
{
foreach(列表中的变量项)
{
if(排除谓词(项))
{
继续;
}
收益回报项目;
}
}
关键是,每当您需要一个“过滤”列表时,只需调用这个扩展方法,它在原始列表中循环,返回所有项目,除了您不需要的项目

大概是这样的:

public static IEnumerable<T> LoopWithExclusion<T>(this IEnumerable<T> list, Func<T,bool> excludePredicate)
{
   foreach(var item in list)
   {
      if(excludePredicate(item))
      {
         continue;
      }

      yield return item;
   }
}
List<User> users = GetUsers();

//later in the code when you need the filtered list:

foreach(var user in users.LoopWithExclusion(u => u.Id == myIdToExclude))
{
   //do what you gotta do
}
List users=GetUsers();
//稍后在代码中,当您需要筛选列表时:
foreach(users.loopWithExclution(u=>u.Id==myIdToExclude)中的var user)
{
//做你该做的
}

这里有一个想法,你不删除它本身怎么样。我的意思是这样的:

public static IEnumerable<T> LoopWithExclusion<T>(this IEnumerable<T> list, Func<T,bool> excludePredicate)
{
   foreach(var item in list)
   {
      if(excludePredicate(item))
      {
         continue;
      }

      yield return item;
   }
}
List<User> users = GetUsers();

//later in the code when you need the filtered list:

foreach(var user in users.LoopWithExclusion(u => u.Id == myIdToExclude))
{
   //do what you gotta do
}
公共静态IEnumerable LoopWithExclution(此IEnumerable列表,Func excludePredicate)
{
foreach(列表中的变量项)
{
if(排除谓词(项))
{
继续;
}
收益回报项目;
}
}
关键是,每当您需要一个“过滤”列表时,只需调用这个扩展方法,它在原始列表中循环,返回所有项目,除了您不需要的项目

大概是这样的:

public static IEnumerable<T> LoopWithExclusion<T>(this IEnumerable<T> list, Func<T,bool> excludePredicate)
{
   foreach(var item in list)
   {
      if(excludePredicate(item))
      {
         continue;
      }

      yield return item;
   }
}
List<User> users = GetUsers();

//later in the code when you need the filtered list:

foreach(var user in users.LoopWithExclusion(u => u.Id == myIdToExclude))
{
   //do what you gotta do
}
List users=GetUsers();
//稍后在代码中,当您需要筛选列表时:
foreach(users.loopWithExclution(u=>u.Id==myIdToExclude)中的var user)
{
//做你该做的
}

假设列表的计数为偶数,我会:

(a) 获取处理器数量的列表

(b) 为每个处理器将列表分成相等的块

(c) 使用这些数据块为每个处理器生成一个线程,终止条件是如果发现谓词返回布尔标志


假设列表的计数为偶数,我会:

(a) 获取处理器数量的列表

(b) 为每个处理器将列表分成相等的块

(c) 使用这些数据块为每个处理器生成一个线程,终止条件是如果发现谓词返回布尔标志


如果您有成百上千个项目,以下是一些有效的代码:

List allItems = GetItems();
//Choose the correct loop here

if((x % 5) == 0 && (X >= 5))
{
     for(int x = 0; x < allItems.Count; x = x + 5)
     {
         if(specialUserID != allItems[x].ID)
             filteredItems.Add( allItems[x] );
         if(specialUserID != allItems[x+1].ID)
             filteredItems.Add( allItems[x+1] );
         if(specialUserID != allItems[x+2].ID)
             filteredItems.Add( allItems[x+2] );
         if(specialUserID != allItems[x+3].ID)
             filteredItems.Add( allItems[x+3] );
         if(specialUserID != allItems[x+4].ID)
             filteredItems.Add( allItems[x+4] );
      }
 }
List allItems=GetItems();
//在这里选择正确的循环
如果((x%5)==0&(x>=5))
{
对于(整数x=0;x