1:字符串方法
public class DemoSet {
public static void main(String[] args) {
String str = DemoSet.getString1("aaabbbccc");
System.out.println(str);
}
public static String getString1(String s) {
String result = "";
for(int i = 0; i < s.length(); i++) {
String str = s.charAt(i) + "";
if(result.indexOf(str) == -1) {
result = result + str;
}
}
return result;
}
}
//输出结果: abc
2:字符串数组
public class Demo3 {
public static void main(String[] args) {
String str = getString4("aaasssddd");
System.out.println(str);
}
public static String toString3(String[] arr) {
String result = "";
for (Object o : arr) {
String s = (String) o;
result +=(String) o;
}
return result;
}
public static String getString4(String s) {
String result = "";
String[] arr = new String[0];
for(int i = 0; i < s.length(); i++) {
String str = s.charAt(i) + "";
if(s.indexOf(str) == i) {
String[] arr1 = Arrays.copyOf(arr,arr.length + 1);
for(int j = 0; j < arr1.length; j++) {
arr1[j] = str;
System.out.print(arr1[j]);
}
}
}
return result;
}
}
//输出结果: asd
3: List集合
public class Demo1 {
public static void main(String[] args) {
String str = getString2("dddcccvvv");
System.out.println(str);
}
public static String toString1(List list) {
String result = "";
for (Object o : list) {
String s = (String) o;
result = result + s;
}
return result;
}
public static String getString2(String s) {
String result = "";
List list = new ArrayList();
for(int i = 0; i < s.length(); i++) {
String str = s.charAt(i) + "";
if(!list.contains(str)) {
list.add(str);
}
}
System.out.println(list.toString());
result = toString1(list);
return result;
}
}
//输出结果: dcv
4: set集合
public class Demo2 {
public static void main(String[] args) {
String str = getString3("qqqwwweee");
System.out.println(str);
}
public static String toString2(Set list) {
String result = "";
for (Object o : list) {
String s = (String) o;
result = result + s;
}
return result;
}
public static String getString3(String s) {
String result = "";
Set set = new HashSet();
for(int i = 0; i < s.length(); i++) {
String str = s.charAt(i) + "";
set.add(str);
}
System.out.println(set.toString());
result = toString2(set);
return result;
}
}
//输出结果: qew
5: HashMap
public class Demo4 {
public static void main(String[] args) {
String str = getString5("zzzxxxccc");
System.out.println(str);
}
public static String getString5(String s) {
String result = "";
Map map = new HashMap();
for(int i = 0; i < s.length(); i++) {
map.put(s.charAt(i), i);
}
Set set = map.keySet();
for (Object o : set) {
result = result + o;
}
return result;
}
}
//输出结果: zxc