示例:Wpfhashtable.rar

注意此示例是WPF程序

xaml代码


    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    Title
="hashtable" Height="300" Width="500" Loaded="Window_Loaded" >
    
<Grid>
        
<ListBox Width="220" x:Name="list1" HorizontalAlignment="Left" Margin="12,12,0,45" />
        
<ListBox Width="220" x:Name="list2" HorizontalAlignment="Right" Margin="0,12,12,45" />
        
<Button Height="30" x:Name="button1" Margin="199,0,199,9" VerticalAlignment="Bottom" Content="去掉重复项" Click="button1_Click" />
    
</Grid>
</Window>

 

using System;
using System.Windows;
using System.Collections;

namespace Wpfhashtable
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// 
</summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i 
< 10000; i++) //随机生成一万项
            {
                Random rd 
= new Random();
                list1.Items.Add(rd.Next(0, 100).ToString() + rd.Next(0, 10000 - i).ToString());
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Hashtable ht 
= new Hashtable();
            int count 
= list1.Items.Count;
            
for (int i = 0; i < count; i++)
            {
                if (!ht.ContainsKey(list1.Items[i].ToString()))  //判断hashtable中是否包含此Key
                {
                    ht.Add(list1.Items[i].ToString(), null);
                    list2.Items.Add(list1.Items[i].ToString());
                }
            }
            MessageBox.Show("去重复后有" + list2.Items.Count.ToString() + "项");
        }
    }
}

 

这个方法去重复数据的速度还是挺快的,如果各位大侠知道其他更好的方法,希望不吝赐教!

相关文章:

  • 2022-12-23
  • 2022-02-15
  • 2021-12-28
  • 2022-12-23
  • 2022-12-23
  • 2021-12-28
  • 2021-12-28
  • 2022-01-19
猜你喜欢
  • 2022-12-23
  • 2021-10-17
  • 2021-08-02
  • 2021-09-30
  • 2021-12-03
  • 2021-05-21
  • 2022-12-23
相关资源
相似解决方案