[Java] 纯文本查看 复制代码
package practice;
import java.util.Comparator;
import java.util.TreeSet;
public class Test6 {
/**
*需求:给一个字符串去重,并且不改变元字符串的顺序。
(用TreeSet集合做)
例如:String s ="abcacd"; [a, b, c, a, d]
*/
public static void main(String[] args) {
String s ="bcadbcad";
TreeSet<Character> ts = new TreeSet<>(new Comparator<Character>() {
@Override
public int compare(Character c1, Character c2) {
//int num = c1.compareTo(c2);
return c1 == c2 ? 0 : 1;
}
});
char[] arr = s.toCharArray();
for (char c : arr) {
ts.add(c);
}
System.out.println(ts);
}
}