几种字符串去重的方法

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值