# 求一处理字符串的算法

0

split进行分割，然后遍历插入Map (Java)中，以字符串为Key，以出现的数量为Value， 后来随着字符串越来越长，性能也急剧下降，

TaranChen | 初学一级 | 园豆：100

0

`Stopwatch watch = new Stopwatch();                       watch.Start();            string str = "1232%%%112%%%12322%%%112%%%23121%%%1231%%%7232%%%192%%%12622%%%119%%%23521%%%1631%%%";            StringBuilder sBuilder = new StringBuilder();            for (int i = 0; i < 100000; i++)                sBuilder.Append(str);            string[] strs = sBuilder.ToString().Split(new string[]{"%%%"},StringSplitOptions.RemoveEmptyEntries);            var booleanGroupQuery = from str1 in strs                                    group str1 by str1;            foreach (var strGroup in booleanGroupQuery)            {                Console.WriteLine("{0} {1}", strGroup.Key, strGroup.Count<string>());            }            watch.Stop();            Console.WriteLine(watch.Elapsed);            Console.WriteLine(watch.ElapsedMilliseconds);            Console.Read();`

Launcher | 园豆：45050 (高人七级) | 2009-06-18 20:59
0

public static void GroupBy()
{
Stopwatch watch = new Stopwatch();

Random rand = new Random();
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < 50000; i++)
{
int j = 0;
string str = "";

while (j++ < rand.Next(3,5))
{
str += rand.Next(1, 5).ToString();
}
sBuilder.AppendFormat("{0}%%%", str);
}

watch.Start();

string[] strs = sBuilder.ToString().Split(new string[] { "%%%" }, StringSplitOptions.RemoveEmptyEntries);

var booleanGroupQuery = from str1 in strs
group str1 by str1;

foreach (var strGroup in booleanGroupQuery)
{
Console.WriteLine("{0} {1}", strGroup.Key, strGroup.Count<string>());
}

watch.Stop();

Console.WriteLine(watch.Elapsed);
Console.WriteLine(watch.ElapsedMilliseconds);

watch.Reset();
watch.Start();

Dictionary<string, int> map = new Dictionary<string, int>();

strs = sBuilder.ToString().Split(new string[] { "%%%" }, StringSplitOptions.RemoveEmptyEntries);

foreach (string str in strs)
{
if (map.ContainsKey(str))
{
map[str]++;
}
else
{
}
}

foreach (string str in map.Keys)
{
Console.WriteLine("{0} {1}", str, map[str]);
}
watch.Stop();

Console.WriteLine(watch.Elapsed);
Console.WriteLine(watch.ElapsedMilliseconds);
}

eaglet | 园豆：17139 (专家六级) | 2009-06-19 08:07
0

Collection
List
│├ArrayList
│└Vector
│　└Stack
└Set
Map
├Hashtable
├HashMap
└WeakHashMap

------------------------------------------------------------------------------------------------

0

System.Collections.Hashtable hs=new System.Collections.Hashtable ();
string[] list = sBuilder.ToString().Split(new char[]{'%','%','%'});
list.g
foreach (string var in list)
{
if (hs.Contains(var))
hs[var] = Convert.ToInt32(hs[var]) + 1;
else