你看看我们 .net 的效率如何,不要生气,随便写写.
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();
我用.net 中的Dictinary 实现的,方法和楼主的应该是一样的,但5万个分割字符串时用时只有90毫秒,楼上那个大概是600毫秒。不知道是不是我对楼主问题的理解有误?我没试过 Java 但我觉得Java不应该被.net 慢这么多吧。下面是代码,前面的是楼上的方法,后面的是我的方法。
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
{
map.Add(str, 1);
}
}
foreach (string str in map.Keys)
{
Console.WriteLine("{0} {1}", str, map[str]);
}
watch.Stop();
Console.WriteLine(watch.Elapsed);
Console.WriteLine(watch.ElapsedMilliseconds);
Console.Read();
}
引自:http://selives.blogbus.com/logs/27714366.html
线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应的类来实现基本的数据结构。这些类均在java.util包中。本文试图通过简单的描述,向读者阐述各个类的作用以及如何正确使用这些类。
Collection
├List
│├LinkedList
│├ArrayList
│└Vector
│ └Stack
└Set
Map
├Hashtable
├HashMap
└WeakHashMap
------------------------------------------------------------------------------------------------
我对java类库不熟,在处理大批量无序数据的时候,hash效率最高,我本能的感觉大概楼主用错了容器。我查了一下,Map 不等于 HashTable,所以建议楼主更换容器再试。
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
hs.Add(var, 1);
}
.net版本。。和2楼貌似。哈哈真理都是有共性的啊。