利用Set List Map集合的特点处理重复字符串

empty
public class ForString {

	
	/*
	 * 清除重复字符,返回字符串
	 */
	
	public String getString(String str){
		Set s = new HashSet();
		char[] ch = str.toCharArray();
		for (char c : ch) {
			s.add(c);
		}
		StringBuffer sb = new StringBuffer();
		
		for (char c : s) {
			sb.append(c);
		}
		
		return sb.toString();
	}
	
	/*
	 * 返回重复的字符串
	 */
	public String getCFString(String str){
		Set s = new HashSet();//清除重复的字符串
		Set s2 = new HashSet();//存放重复的字符串
		char[] ch = str.toCharArray();
		for (char c : ch) {
			boolean b = s.add(c);
			if(!b){
				s2.add(c);
			}
		}

		StringBuffer sb = new StringBuffer();
		for (char c : s2) {
			sb.append(c);
		}
		
		return sb.toString();
	}
	
	/*
	 * 返回不重复的字符串
	 */
	public String getNoCF(String str){
		Set s = new HashSet();//清除重复的字符串
		Set s2 = new HashSet();//存放重复的字符串
		Set s3 = new HashSet();//存放不重复的字符串
		char[] ch = str.toCharArray();
		for (char c : ch) {
			boolean b = s.add(c);
			if(!b){
				s2.add(c);
			}
		}
		
		s3.addAll(s);
		s3.removeAll(s2);

		StringBuffer sb = new StringBuffer();
		for (char c : s3) {
			sb.append(c);
		}
		
		return sb.toString();
	}
	
	/*
	 * 返回Map集合
	 */
	public Map getStringNum(String str){
		char[] ch = str.toCharArray();
		Map map = new HashMap();
		System.out.println(str);
		for (char c : ch) {
			if(map.containsKey(c)){
				Integer i = map.get(c);
				i++;
				map.put(c, i);
			}else{
				map.put(c, 1);
			}
		}
		return map;
	}
	
	
}

你可能感兴趣的:(利用Set List Map集合的特点处理重复字符串)