zoukankan      html  css  js  c++  java
  • Distinct 实现自定义去重

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace wpfDistinct
     8 {
     9     class DataCompare : IEqualityComparer<DateTime>
    10     {
    11 
    12         public bool Equals(DateTime x, DateTime y)
    13         {
    14             return x.Hour == y.Hour;
    15         }
    16 
    17         public int GetHashCode(DateTime obj)
    18         {
    19             return (obj.Year + obj.Month + obj.Day + obj.Hour).ToString().GetHashCode();
    20 
    21         }
    22     }
    23 }
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.Windows;
     7 using System.Windows.Controls;
     8 using System.Windows.Data;
     9 using System.Windows.Documents;
    10 using System.Windows.Input;
    11 using System.Windows.Media;
    12 using System.Windows.Media.Imaging;
    13 using System.Windows.Navigation;
    14 using System.Windows.Shapes;
    15 
    16 namespace wpfDistinct
    17 {
    18     /// <summary>
    19     /// MainWindow.xaml 的交互逻辑
    20     /// </summary>
    21     public partial class MainWindow : Window
    22     {
    23         public MainWindow()
    24         {
    25             InitializeComponent();
    26 
    27             Random rand = new Random();
    28             List<DateTime> list = new List<DateTime>();
    29             for (int i = 0; i < 100; i++)
    30             {
    31                 list.Add(new DateTime(2016, rand.Next(1, 12), rand.Next(1, 31), rand.Next(1, 12),rand.Next(1,59),rand.Next(1,59)));
    32             }
    33             if (null != list)
    34             {
    35                 List<DateTime> list3 = list.Distinct(new DataCompare()).ToList();
    36               
    37                 if (null != list3)
    38                 {
    39                     List<List<DateTime>> collcetion = new List<List<DateTime>>();
    40                     foreach (DateTime time in list3)
    41                     {
    42                         collcetion.Add(list.Where(i => i.Month==time.Month&& i.Day == time.Day&&i.Hour==time.Hour).ToList());
    43                     }
    44                 }
    45                
    46                
    47             }
    48 
    49         }
    50     }
    51 }
    Distinct 要实现IEqualityComparer<DateTime> 这个接口才能自定义
    hashCode的精度到那个地方,就可以以那种精度进行去重

  • 相关阅读:
    Java 继承和重写
    Java 构造函数和函数重载
    java 面向对象
    for循环和数组例题
    java数组和函数
    java程序流程控制
    CF600C Make Palindrome
    CF600A Extract Numbers
    [NOI2007]社交网络
    SPOJ 6779 GSS7
  • 原文地址:https://www.cnblogs.com/ants_double/p/5440573.html
Copyright © 2011-2022 走看看