码迷,mamicode.com
首页 > 其他好文 > 详细

set接口

时间:2019-03-17 18:31:25      阅读:160      评论:0      收藏:0      [点我收藏+]

标签: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>
这段代码利用了set不能重复的特性,当你键入一段字符串,可以筛选出重复的,输出的字符串就不含有重复字符。

set接口

标签:print   scan   哈希表   set   number   有序   str   ++   col   

原文地址:https://www.cnblogs.com/zhc8016/p/10547784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!