List去重的实现
List<T> 当T为值类型的时候 去重比较简单,当T为引用类型时,一般根据业务需要,根据T的中几个属性来确定是否重复,从而去重。
查看System.Linq下的Enumerable存在一个去重方法
/// <summary>Returns distinct elements from a sequence by using a specified <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> to compare values.</summary>
/// <param name="source">The sequence to remove duplicate elements from.</param>
/// <param name="comparer">An <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> to compare values.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>An <see cref="T:System.Collections.Generic.IEnumerable`1" /> that contains distinct elements from the source sequence.</returns>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="source" /> is <see langword="null" />.</exception>
[__DynamicallyInvokable]
public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
return DistinctIterator(source, comparer);
}
通过实现IEqualityComparer<T>比较器来实现对象的比较。
IEqualityComparer<T>的简单实现,通过委托来比较对象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace Extensions
{
public delegate bool ComparerDelegate<T>(T x, T y); /// <summary>
/// list比较
/// </summary>
/// <typeparam name="T"></typeparam>
public class ListCompare<T> : IEqualityComparer<T>
{
private ComparerDelegate<T> _comparer; public ListCompare(ComparerDelegate<T> @delegate)
{
this._comparer = @delegate;
} public bool Equals(T x, T y)
{
if (ReferenceEquals(x, y))
{
return true;
}
if (_comparer != null)
{
return this._comparer(x, y);
}
else
{
return false;
}
} public int GetHashCode(T obj)
{
return obj.ToString().GetHashCode();
}
}
}
使用方法:
list= List.Distinct(new ListCompare<Path>
((x, y) => x.Latitude == y.Latitude && x.Longitude == y.Longitude)).ToList();
List去重的实现的更多相关文章
- JavaScript常见的五种数组去重的方式
▓▓▓▓▓▓ 大致介绍 JavaScript的数组去重问题在许多面试中都会遇到,现在做个总结 先来建立一个数组 var arr = [1,2,3,3,2,'我','我',34,'我的',NaN,NaN ...
- 数组去重 JS
我说的数组去重是这样的: var arr = ['f', 'a', 'b', 'd', 'e', 'g'] ; var str='f'; 去除arr中的str 最简单的是遍历arr与str做比较, ...
- [Algorithm] 使用SimHash进行海量文本去重
在之前的两篇博文分别介绍了常用的hash方法([Data Structure & Algorithm] Hash那点事儿)以及局部敏感hash算法([Algorithm] 局部敏感哈希算法(L ...
- JS去重及字符串奇数位小写转大写
面试中经常会考到数组的去重.作为一名合格的前端开发者,不知道几种去重方法是在不应该.废话不多说直接开撸-- 一.indexOf()方法 实现思路:使用indexOf()方法来判断新数组中是否有这个值, ...
- js数组去重
这就是数组去重了...var str=['hello','node','element','node','hello','blue','red'];var str1=[]; function firs ...
- [Hadoop]-从数据去重认识MapReduce
这学期刚好开了一门大数据的课,就是完完全全简简单单的介绍的那种,然后就接触到这里面最被人熟知的Hadoop了.看了官网的教程[吐槽一下,果然英语还是很重要!],嗯啊,一知半解地搭建了本地和伪分布式的, ...
- 1.uniq去重命令讲解
uniq命令: 常见参数: -c,--count ***** 在每行旁边显示改行重复出现的次数 -d,--repeated 仅显示重复出现的行,2次或2次以上的行,默认的去重包 ...
- python list dict 去重的两种方式
def dedupe(items, key=None): seen = set() for item in items: val = item if key is None else key(item ...
- js给数组去重写法
数组为 var list =['A','B','A']; 法一:常规做法,新建list,给list添加元素,添加前判断是否包含 var removeRepeatItem = function(list ...
- 分享一种容易理解的js去重排序方法
<script> var arr=[1,8,6,4,88,22,99,4,6,86,5,58,89,5]; //先使用sort()函数去重 var a=arr.sort(function ...
随机推荐
- SpringBoot集成Security,JWT,Swagger全分析
GitHub地址: https://github.com/li-jun0201/springsecuritydemo本项目采用SpringBoot1.5.9, SpringSecurity,JWT, ...
- Java开发者必备的10大学习网站,送给入门学习java的你,请收下!
作为开发者来说,必备的除了对编码的热情还要有自己的一套技巧,另外不可缺少的就是平时学习的网站.以下本人收集的 Java 开发者必备的网站,这些网站可以提供信息.以及一些很棒的讲座 , 还能解答一般问题 ...
- Asp.net Core 2.2关于AutoMapper更初级的入门教程
今天刚看到老张的哲学博客关于AutoMapper的教程,从壹开始前后端分离[ .NET Core2.0 +Vue2.0 ]框架之十三 || DTOs 对象映射使用,项目部署Windows+Linux完 ...
- 利用策略模式优化过多 if else 代码
前言 不出意外,这应该是年前最后一次分享,本次来一点实际开发中会用到的小技巧. 比如平时大家是否都会写类似这样的代码: if(a){ //dosomething }else if(b){ //dosh ...
- JavaScript面试的完美指南(开发者视角)
为了说明 JS 面试的复杂性,首先,请尝试给出以下结果: onsole.log(2.0 == "2" == new Boolean(true) == "1") ...
- css控制元素高度自适应
可以采用元素定位 + padding 的方式使特定元素高度自适应. css 样式: html,body{ height:100%; margin:; padding:; } .wrap { heigh ...
- Android-----Intent中通过startActivity(Intent intent )隐式启动新的Activity
显式Intent我已经简单使用过了,也介绍过概念,现在来说一说隐式Intent: 隐式Intent:就是只在Intent中设置要进行的动作,可以用setAction()和setData()来填入要执行 ...
- Linux 操作系统基础
list : ls 目录: 文件,路径映射. ls : -l : lang 长格式, 显示完整信息. 文件类型: -: 普通文件(f) d: 目录文件 b: 块设备文件(block) c: 字块设备文 ...
- Iterm2/Mac自带终端工具快速进入你想进入的虚拟机教程
一.首先我们在终端本地要写一个登录的脚本,eg: 当然首先要touch login.sh 啦,下面就是脚本文件,比较low,大神勿喷,会更炫酷写法的小伙伴可以自己参考这个思路写,不会的直接复制就好啦 ...
- java8 日期时间之间的关系
Class or Enum Year Month Day Hours Minutes Seconds* Zone Offset Zone ID toString Output Where Discu ...