Как найти дубликаты в Списке <T> быстро и обновить исходный набор

В действительности он ищет значки уведомлений в папке для рисования с именем ic_stat_ic_notification и значения цвета в папке ресурсов Android. Вы должны создать их. Если вы хотите использовать default, просто удалите их из Android-манифеста.

5
задан Community 23 May 2017 в 10:24
поделиться

3 ответа

I think the easiest way is to start by writing an extension method which find's duplicates in a list of objects. Since you're objects use .Equals() they can be compared in most common collections.

public static IEnumerable<T> FindDuplicates<T>(this IEnumerable<T> enumerable) {
  var hashset = new HashSet<T>();
  foreach ( var cur in enumerable ) { 
    if ( !hashset.Add(cur) ) {
      yield return cur;
    }
  }
}

Now it should be pretty easy to update your collection for duplicates. For instance

List<SomeType> list = GetTheList();
list
  .FindDuplicates()
  .ToList()
  .ForEach(x => x.State = "DUPLICATE");

If you already have a ForEach extentsion method defined in your code, you can avoid the .ToList.

8
ответ дан 13 December 2019 в 22:16
поделиться

Your objects have some sort of state property. You're presumably finding duplicates based on another property or set of properties. Why not:

List<obj> keys = new List<object>();

foreach (MyObject obj in myList)
{
    if (keys.Contains(obj.keyProperty))
        obj.state = "something indicating a duplicate here";
    else
        keys.add(obj.keyProperty)
}
1
ответ дан 13 December 2019 в 22:16
поделиться
IEnumerable<T> oldList;
IEnumerable<T> list;

foreach (var n in oldList.Intersect(list))
   n.State = "Duplicate";

Edit: I need to lrn2read. this code is for 2 lists. My bad.

1
ответ дан 13 December 2019 в 22:16
поделиться
Другие вопросы по тегам:

Похожие вопросы: