C#:使用Hashtable实现输出那些用户发表主题最多的信息
构思:先计算各自的数量,那些数量最多,输出详细信息
具体算法如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Count { #region 计算各实体数量 public static Hashtable EntityCount(DataTable dt, string clmnUid) { //List<string> lst = new List<string>(); 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<string> MaxCountUid(Hashtable uidsCount,bool listResult) { int maxCount = 0; IList<string> uids = new List<string>(); 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<string> 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 }
构造测试数据:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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; } }
测试代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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(); } }
输出结果:
uid0:name0(13)
uid1:name1(13)
uid2:name2(13)