java--将字符串转化成字符数组

package com.test;
import com.bean.Student;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.TreeSet;

import com.bean.Student;
public class test_5 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	//键盘录入
	Scanner sc =new Scanner(System.in);
	System.out.println("please String:");
	String line =sc.nextLine();
	//将字符串转化成字符数组
	char[] arr=line.toCharArray();
	//定义集合,传入比较器对字符排序并保留重复,TreeSet
	TreeSet<Character> ts =new TreeSet<>(new Comparator<Character>() {
		public int compare(Character c1,Character c2) {
			int num =c1-c2;
			return num==0? 1 :num;
		}
	});
	for(char c: arr) {
		ts.add(c);
	}
	for(Character c:ts) {
		System.out.print(c);
	}
}

}

上一篇:ArrayList和LinkedList区别以及list,set,map三者的区别


下一篇:treeset集合