Collection之Set学习
<!--[if gte vml 1]><v:shapetype
id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t"
path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f">
<v:stroke joinstyle="miter" />
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0" />
<v:f eqn="sum @0 1 0" />
<v:f eqn="sum 0 0 @1" />
<v:f eqn="prod @2 1 2" />
<v:f eqn="prod @3 21600 pixelWidth" />
<v:f eqn="prod @3 21600 pixelHeight" />
<v:f eqn="sum @0 0 1" />
<v:f eqn="prod @6 1 2" />
<v:f eqn="prod @7 21600 pixelWidth" />
<v:f eqn="sum @8 21600 0" />
<v:f eqn="prod @7 21600 pixelHeight" />
<v:f eqn="sum @10 21600 0" />
</v:formulas>
<v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect" />
<o:lock v:ext="edit" aspectratio="t" />
</v:shapetype><v:shape id="图片_x0020_0" o:spid="_x0000_i1025" type="#_x0000_t75"
alt="Collection_Set.jpeg" style='width:415.5pt;height:275.25pt;visibility:visible;
mso-wrap-style:square'>
<v:imagedata src="file:///C:\Users\yingkh\AppData\Local\Temp\msohtmlclip1\01\clip_image001.jpg"
o:title="Collection_Set" />
</v:shape><![endif]--><!--[if !vml]--><!--[endif]-->
TreeSet和HahSet的区别:
HashSet 是哈希表实现的,无序的结合,表现为检索(contains)的时间复杂度是 o(0)
TreeSet 是红黑树实现的,排序的集合
TreeSet:
public class TreeSet<E>
extends AbstractSet<E>
implements
SortedSet<E>, Cloneable, java.io.Serializable
{
private transient
SortedMap<E,Object> m; // The backing Map
|
Add方法:
public boolean add(E o)
{
return m.put(o, PRESENT)==null;
}
|
M是一个TreeMap,treeMap的put方法如下:
public V put(K key, V value) {
Entry<K,V> t = root;
if (t == null) {
incrementSize();
root = new Entry<K,V>(key, value, null);
return null;
}
while (true) {
int cmp = compare(key, t.key);
if (cmp == 0) {
return t.setValue(value);
} else if (cmp < 0) {
if (t.left != null) {
t = t.left;
} else {
incrementSize();
t.left = new
Entry<K,V>(key, value, t);
fixAfterInsertion(t.left);
return null;
}
} else { // cmp > 0
if (t.right != null) {
t = t.right;
} else {
incrementSize();
t.right = new
Entry<K,V>(key, value, t);
fixAfterInsertion(t.right);
return null;
}
}
}
}
|
显然,TreeSet判断是否重复,是通过TreeMap的compare方法。
再看HashSet:
HashSet:
实现的接口和集成的类如下;
public class HashSet<E>
extends AbstractSet<E>
implements Set<E>,
Cloneable, java.io.Serializable
{
static final long serialVersionUID =
-5024744406713321676L;
private transient
HashMap<E,Object> map;
|
可见,HashSet持有一个HashMap。
Add方法:
public boolean add(E
o) {
return map.put(o, PRESENT)==null;
}
|
HashMap的put方法:
public V put(K key, V value) {
K k = maskNull(key);
int hash = hash(k);
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
if (e.hash == hash && eq(k,
e.key)) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, k, value, i);
return null;
}
|
可见,HashSet主要是通过hashcode和equals方法来判断是否重复的。
分享到:
相关推荐
这是我的博客Python机器学习:朴素贝叶斯那一章要用的数据集合,有需要的可以下载,里面的spam.csv我从kaggle上面下载的,
Java集合Collection、List、Set、Map使用详解
这个一个简单的Java集合类学习代码,让你能够更好的理解Collection集合类。
SMS Spam Collection 是一组为 SMS Spam 研究收集的带有 SMS 标记的消息。它包含 5,574 条消息的一组英文 SMS 消息,根据合法或垃圾邮件进行标记。 这些文件每行包含一条消息。每行由两列组成:v1 包含标签(非垃圾...
Java集合的学习,以思维导图形式进行总结梳理,希望对我自己和大家的学习有所帮助,若有不对之处希望指出。
list & Set list 下面是他们的关系表 –Collection–(单列集合) –|—-> List–(有序、可重复数据) —||—-> ArrayLis–(主要实现类) —||—-> LinkedList (看情况) —||—-> Vector–(古老实现类) Collection接口–...
深入讲述了JAVA中的集合,介绍了JAVA集合中所用到的数据结构的基础知识。详细解说List 、Set、Map以及他们的实现类,是学习JAVA集合不可多得的资料。欢迎下载!
Collection学习笔记Collection集合概述Collection集合常用方法Collection集合的遍历(迭代器)集合的使用样例 Collection集合概述 是单例集合的顶层接口 它提供了更具体的子接口的实现,如set和list. Collection集合...
对集合操作的接口3.1 Collection接口3.2 List接口3.3 Set接口4. 集合类4.1 集合的实现类4.1.1 ArrayList 4.1.2 LinkedList 4.1.3 Vecotor4.1.4 HashSet 4.1.5 LinkedHashSet 4.2 Conllections工具类5. 集合的遍历...
mongodb学习笔记资料,从安装到操作库collection 对document的crud 索引 replicaSet sharding 备份与恢复
个人学习总结,仅供参考。详细介绍集合中常用的方法并附带了相关代码,对Collection的子类:List(ArrayList、LinkedList)及Set(HashSet、TreeSet)进行了详细的介绍。
实现类:红色Collection|_____Set(HashSet)| |_____SortedSet(TreeSet)|_____List(LinkedList,ArrayList) Collection:集合层次中的根接口,JDK没有提供这个接口的实现类。Set:不能包含重复的元素,子接口SortedSet是...
学习集合:理解Java中的Collection、泛型、List、Set、Collections、Map和HashMap等概念和用法。 掌握异常处理:了解Java的异常体系、异常分类,学会声明、抛出和捕获异常,并自定义异常。 初步掌握多线程:理解线程...
第8天 集合今日学习内容List集合Set集合今日学习目标能够说出List集合特点使用List存储的数据结构能够说出List常见的三个的特点能够说出Set集合的
包装类 String 类 字符串的特性 String 类的常见方法 StringBuffer 类 Math 类 Arrays 类 System 类 日期类包括1代2代3代 集合 集合的框架体系 Collection 接口和常用方法 Collection 接口实现类的特点 List 接口和...
《Java JDK 7学习笔记》将IDE操作纳为教学内容之一,使读者能与实践结合,提供的视频教学能更清楚地帮助读者掌握操作步骤。 内容简介 书籍 计算机书籍 《java jdk 7学习笔记》是作者多年来教学实践经验的总结...
new wall id=1 face ( 0, 0, 0) (10, 0, 0) (10, 0 ...plot set title text 'Basic collection of particles in a box' plot add ball yellow plot add wall white plot add axes brown plot set back green plot show
They are not a simple collection of many algorithms, but a carefully designed system, so that the codes can be maximally reused and cooperate well. (6) it is easy to use. Detailed help information ...
这篇集合总结一共包括十二节,介绍了一些接口和实现类的底层源码以及基本的增加、删除元素等的操作(包括List、Map、Set接口、ArrayList、Vector、LinkedList、HashSet、TreeSet、HashMap、TreeMap等实现类)。...
有关java的collection的学习与应用,这课件挺不错的,欢迎下载。。