示例代码:
public static void main(String[] args) {
TreeSet<String> set = new TreeSet<>();
Scanner sc = new Scanner(System.in);
String[] arr = sc.next().split("");
Collections.addAll(set, arr);
System.out.println(set.size());
}
代码详解:
1. 大家所熟悉的Set的特性就是无序、不可重复,而Set中的TreeSet,就是有序不可重复的,所以我们直接选用TreeSet处理此题,答案也就迎刃而解;
2. Collections.addAll(Collection<? super T> c, T... elements); 此方法是Collections类中的方法,参数c是要将元素储存到的集合,elements参数是所要存入集合的参数。