1. private static String ss(String name)
{
String[] str = name.split(",");
if (str.length == 0 )
{
return null;
}
List<String> list = new ArrayList();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length; i++)
{
if (!list.contains(str[i]))
{
list.add(str[i]);
sb.append(str[i] + ",");
}
}
return sb.toString().substring(0, sb.toString().length() - 1);
}
2. public static String duplicatRemoval(String source){
String[] ps = source.split(",");
ArrayList<String> as = new ArrayList<>();
f:for (int i = 0; i < ps.length; i++) {
for (int j = 0; j < as.size(); j++) {
if (ps[i].equals(as.get(j))) {
continue f;
}
}
as.add(ps[i]);
}
StringBuffer buffer = new StringBuffer();
for (String string : as) {
buffer.append(string+",");
}
return buffer.toString().substring(0, buffer.toString().lastIndexOf(","));
}
3. public static void main(String[] args)
{
long start = new Date().getTime();
long end = 0;
String name = "T1,T1,T1,T2,T2,T2,T2,T2,T2,T3,T3,T3,T3,T3,T3,T3,T3,T3,T3";
for (int i = 0; i < 10000; i++)
{
ss(name);
}
end = new Date().getTime();
System.out.println(end-start);
}
方法一效率更高一点