博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#:使用Hashtable实现输出那些用户发表主题最多的信息
阅读量:7138 次
发布时间:2019-06-28

本文共 4438 字,大约阅读时间需要 14 分钟。

构思:先计算各自的数量,那些数量最多,输出详细信息

 

具体算法如下:

public class Count    {        #region 计算各实体数量        public static Hashtable EntityCount(DataTable dt, string clmnUid)        {            //List
lst = new List
(); Hashtable uidsCount = new Hashtable(); foreach( DataRow row in dt.Rows) { string uid = row[clmnUid].ToString(); if (uidsCount.Contains(uid)) { uidsCount[uid] = (int)uidsCount[uid] + 1; } else { uidsCount.Add(uid,1); } } return uidsCount; } #endregion #region 获取数量最多的实体 public static IList
MaxCountUid(Hashtable uidsCount,bool listResult) { int maxCount = 0; IList
uids = new List
(); int value = 0; foreach (DictionaryEntry kv in uidsCount) { value = (int)kv.Value; if (value > maxCount) { maxCount = value; uids.Clear(); uids.Add(kv.Key.ToString()); } else if(value == maxCount) { uids.Add(kv.Key.ToString()); } } return uids; } public static Hashtable MaxCountUid(Hashtable uidsCount) { int maxCount = 0; Hashtable uids = new Hashtable(); int value = 0; foreach (DictionaryEntry kv in uidsCount) { value = (int)kv.Value; if (value > maxCount) { maxCount = value; uids.Clear(); uids.Add(kv.Key,kv.Value); } else if (value == maxCount) { uids.Add(kv.Key, kv.Value); } } return uids; } #endregion #region 输出用户的ID、姓名、数量 public static void PrintMaxCountName(IList
uids,DataTable users, string clmnUid, string clmnName) { foreach(DataRow dr in users.Rows) { string strUid = dr[clmnUid].ToString(); foreach(string uid in uids) { if (uid == strUid) { string strName = dr[clmnName].ToString(); Console.WriteLine(strUid + ":" + strName); } } } } public static void PrintMaxCountName(Hashtable uids, DataTable users, string clmnUid, string clmnName) { foreach (DataRow dr in users.Rows) { string strUid = dr[clmnUid].ToString(); foreach (DictionaryEntry kv in uids) { if (kv.Key.ToString() == strUid) { string strName = dr[clmnName].ToString(); Console.WriteLine(strUid + ":" + strName +"(" + kv.Value.ToString() + ")"); } } } } #endregion }
View Code

构造测试数据:

public class Data    {        private string[] usersColumns = { "uid", "name" };        private string[] subjectsColumns = { "title", "uid", "time" };        public DataTable initUsers()        {            DataTable users = new DataTable();            foreach (string columnName in usersColumns)            {                users.Columns.Add(columnName);            }            for (int i = 0; i < 10;i++ )            {                users.Rows.Add("uid"+i,"name"+i);            }            return users;        }        public DataTable initSujects()        {            DataTable subjects = new DataTable();            foreach (string columnName in subjectsColumns)            {                subjects.Columns.Add(columnName);            }            for (int i = 0; i < 123; i++)            {                subjects.Rows.Add("title" + i, "uid" + i % 10,"time" + 1);            }            return subjects;        }    }
View Code

测试代码:

class Program    {                static void Main(string[] args)        {            const string clmnUid = "uid";            const string clmnName = "name";            Data data = new Data();            DataTable users = data.initUsers();            DataTable subjects = data.initSujects();            Hashtable uidsCount = Count.EntityCount(subjects, clmnUid);            Hashtable maxCountUids = Count.MaxCountUid(uidsCount);            Count.PrintMaxCountName(maxCountUids,users,clmnUid,clmnName);            Console.Read();        }    }
View Code

输出结果:

uid0:name0(13)

uid1:name1(13)

uid2:name2(13)

 

转载地址:http://dwvrl.baihongyu.com/

你可能感兴趣的文章
编译hadoop的libhdfs.a
查看>>
PHP简易计算器方法2
查看>>
意见整理
查看>>
Linux安装Tomcat,运行Eclipse,web项目
查看>>
计算机网络笔记
查看>>
mysql 查重复数据
查看>>
【c学习-10】
查看>>
GNU make 总结 (四)
查看>>
poj1611(并查集简单应用)
查看>>
python 文件读写模式r,r+,w,w+,a,a+的区别(附代码示例)
查看>>
asp.net web 通过IHttpAsyncHandler接口进行消息推送
查看>>
wcf 使用sqlMembership证书认证
查看>>
MogoDB安装与使用(Windows篇)
查看>>
Objective-C Runtime 运行时之一:类与对象
查看>>
[Python]打开文件的模式
查看>>
UVALive5429 UVA382 POJ1528 HDU1323 ZOJ1284 Perfection
查看>>
HDU1195 ZOJ2416 Open the Lock【BFS】
查看>>
使用GDAL获取网络数据
查看>>
数据库事务的四个隔离级别
查看>>
Hibernate初学
查看>>