1、字符串去重并排序(非自然排序)
字符串:jbcahacribac
输出:jbcahri
private void stringSet() {
StringBuffer sb = new StringBuffer();
String str="jbcahacribac";
for (int i = 0; i < str.length(); i ++) {
char charWord = str.charAt(i);
int firstPosition = str.indexOf(charWord);
int lastPosition = str.lastIndexOf(charWord);
if (firstPosition == lastPosition || firstPosition == i) {
sb.append(charWord);
}
}
Log.e("TAG","------------"+sb.toString());
}
2、字符串去重并排序(自然排序)
字符串:jbcahacribac
输出:abchijr
private void stringSet() {
String str="jbcahacribac";
String[] split = str.split("");
TreeSet<String> treeSet=new TreeSet<>();
for (String s:split) {
treeSet.add(s) ;
}
str="";
for (String s:treeSet) {
str+=s;
}
Log.e("TAG","--------"+str.toString());
}