67,512
社区成员
发帖
与我相关
我的任务
分享
/**
* 运行结果 大概
一次调用 a b b2 c d 顺序
[d, a, c, b]前面是输出 a 方法 耗时188
bacd前面是输出 b 方法 耗时78
bacd前面是输出 b2 方法 耗时219
dacb前面是输出 c 方法 耗时1468
abcd前面是输出 d 方法 耗时47
一次调用方法 d c b2 b a 顺序
abcd前面是输出 d 方法 耗时47
dacb前面是输出 c 方法 耗时1875
bacd前面是输出 b2 方法 耗时219
bacd前面是输出 b 方法 耗时62
[d, a, c, b]前面是输出 a 方法 耗时156
不知道这么测试对不对.新手疑问
*/
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Test {
public static void a(String str){
HashMap map = new HashMap();
for(int i=0;i<str.length();i++)
{
String strchar = str.substring(i, i+1);
map.put(strchar, strchar);
}
System.out.print(map.keySet());
}
public static void b(String str){
char[] cs = str.toCharArray();
String _s = String.valueOf(cs[0]);
for (int i = 0; i < cs.length; i++) {
if(_s.indexOf((int)cs[i]) == -1){
_s += String.valueOf(cs[i]);
}
}
System.out.print(_s);
}
public static void b2(String strs) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < strs.length(); i++) {
if (sb.indexOf(String.valueOf(strs.charAt(i))) == -1) {
sb.append(strs.charAt(i));
}
}
System.out.print(sb.toString());
}
public static void c(String funs){
String[] fun=funs.split("");
Set set = new HashSet();
for(String f:fun){
set.add(f);
}
String temp="";
Iterator i=set.iterator();
while(i.hasNext()){
temp += i.next();
}
System.out.print(temp);
}
public static void d(String strs){
char[] cs = strs.toCharArray();
char[] _cs = new char[127];
for (int i = 0,j = _cs.length; i < j; i++) {
_cs[cs[i]] = cs[i];
}
String str = "";
for (int i = 0; i < _cs.length; i++) {
if((int)_cs[i] != 0){
str += _cs[i];
}
}
System.out.print(str);
}
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 45000; i++) {
sb.append("bbbbbbbbbbbbbbbbaaaaaaaaccddaaaaaaa");
}
long a = new Date().getTime();
Test.a(sb.toString());
long b = new Date().getTime();
System.out.println("前面是输出 a 方法 耗时"+(b-a)+"\n");
a = new Date().getTime();
Test.b(sb.toString());
b = new Date().getTime();
System.out.println("前面是输出 b 方法 耗时"+(b-a)+"\n");
a = new Date().getTime();
Test.b2(sb.toString());
b = new Date().getTime();
System.out.println("前面是输出 b2 方法 耗时"+(b-a)+"\n");
a = new Date().getTime();
Test.c(sb.toString());
b = new Date().getTime();
System.out.println("前面是输出 c 方法 耗时"+(b-a)+"\n");
a = new Date().getTime();
Test.d(sb.toString());
b = new Date().getTime();
System.out.println("前面是输出 d 方法 耗时"+(b-a)+"\n");
}
}
public static void a(String str){
HashMap map = new HashMap();
for(int i=0;i<str.length();i++)
{
String strchar = str.substring(i, i+1);
map.put(strchar, strchar);
}
System.out.print(map.keySet());
}
public static void c(String funs){
String[] fun=funs.split("");
Set set = new HashSet();
for(String f:fun){
set.add(f);
}
String temp="";
Iterator i=set.iterator();
while(i.hasNext()){
temp += i.next();
}
System.out.print(temp);
}