标签:print scan 哈希表 set number 有序 str ++ col
今天看一下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>标签:print scan 哈希表 set number 有序 str ++ col
原文地址:https://www.cnblogs.com/zhc8016/p/10547784.html