文字列を入力し、最終的に出力される重複した文字列を削除し、入力順序を保証します.
String s = removeDuplicates("accdalfcld");
s = "acdlf";
LinkedHashSet , 。
public String removeDuplicates(String str){
if(TextUtils.isEmpty(str)){
return str;
}
int length = str.length();
if (length == 1) {
return str;
}
LinkedHashSet set = new LinkedHashSet<>();
for (int i = 0; i < length-1; i++) {
set.add(str.charAt(i));
}
StringBuilder stringBuilder = new StringBuilder();
for (Character c: set){
stringBuilder.append(c);
}
return stringBuilder.toString();
}