今天看一下set接口:

 

set接口是一个不包含重复元素的 collection,是collection的子接口。set也有一个子接口SortedSet,提供了元素顺序遍历的方法。

HashSet:用哈希表实现的;向HashSet集合中传入元素时,HashSet会调用该对象的HashCode方法获取Hash值,然后决定存储位置(无序)

LinkedHashSet:HashSet的子类,有HashSet的计算速度,不允许重复的值,使用HashCode确定在集合中的位置,使用链表的方式确定位置(有序,按照输入的顺序输出)

TreeSet:使用树形结构实现的。

例:

System.out.println("请输入一个字符串:");
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
char[] array = string.toCharArray();
HashSet<character> hashSet = new HashSet<>();
for(int i = 0 ; i < array.length ; i++) {
    hashSet.add(array[i]);
}
System.out.println(hashSet);</character>
这段代码利用了set不能重复的特性,当你键入一段字符串,可以筛选出重复的,输出的字符串就不含有重复字符。