HashSet和TreeSet
package com.wzy.list; import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet; class Student1 implements Comparable{
private Integer id;
private String name; public Student1(String name,Integer id) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "name:"+name+"; id:"+id;
}
@Override
public int compareTo(Object o) {
if(this.id > ((Student1)o).id) {
return ;
}else if(this.id < ((Student1)o).id) {
return -;
}else{
return this.name.compareTo(((Student1)o).name);
} }
} class Student2{
private Integer id;
private String name; public Student2(String name,Integer id) {
this.id = id;
this.name = name;
} @Override
public int hashCode() {
final int prime = ;
int result = ;
result = prime * result + ((id == null) ? : id.hashCode());
result = prime * result + ((name == null) ? : name.hashCode());
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student2 other = (Student2) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
} @Override
public String toString() {
return "Student2 [id=" + id + ", name=" + name + "]";
} }
public class Test02 {
public static void main(String[] args) {
/**
* TreeSet和HashSet存入对象时,不会自动去重操作
*
* TreeSet去掉重复对象,基于Comparable接口实现
* HashSet去掉重复对象,基于equals和hashCode实现
* TreeSet可以对象排序,hashSet不可以
*
* 如果存入的不是new出来的对象,
* 而是String或int类型,可以直接使用,不必实现上述操作
* **/ //TreeSet存入对象实例
Set<Student1> stu1 = new TreeSet<Student1>();
stu1.add(new Student1("aa",));
stu1.add(new Student1("bb",));//id重复
stu1.add(new Student1("dd",));
stu1.add(new Student1("cc",));
stu1.add(new Student1("aa",));//对象重复
Iterator s = stu1.iterator();
while(s.hasNext()) {
System.out.println(s.next());
} //HashSet存入对象实例
Set<Student2> stu2 = new HashSet<Student2>();
stu2.add(new Student2("aa",));
stu2.add(new Student2("bb",));
stu2.add(new Student2("cc",));
stu2.add(new Student2("dd",));
stu2.add(new Student2("ee",));
stu2.add(new Student2("aa",)); Iterator i = stu2.iterator();
while(i.hasNext()) {
System.out.println(i.next());
} //TreeSet存入普通字符串
Set set1 = new TreeSet();
set1.add("a");
set1.add("aa");
set1.add("d");
set1.add("b");
set1.add("c");
//[a, aa, b, c, d] TreeSet已经自动排序好了
System.out.println(set1); //HashSet存入普通字符串
Set set2 = new HashSet();
set2.add("a");
set2.add("aa");
set2.add("d");
set2.add("c");
set2.add("b");
//[aa, a, b, c, d] HashSet不会排序
System.out.println(set2);
}
}
HashSet和TreeSet的更多相关文章
- HashSet 与TreeSet和LinkedHashSet的区别
Set接口 Set不允许包含相同的元素,如果试图把两个相同元素加入同一个集合中,add方法返回false. Set判断两个对象相同不是使用==运算符,而是根据equals方法.也就 ...
- HashSet vs TreeSet vs LinkedHashSet
使用Set集合的主要原因是因为Set集合里面没有重复的元素.Set集合有三个常见的实现类:HashSet,TreeSet,LinkedHashSet.什么时候,选择哪一个使用非常重要.简单的说,如果你 ...
- Set集合——HashSet、TreeSet、LinkedHashSet(2015年07月06日)
一.Set集合不同于List的是: Set不允许重复 Set是无序集合 Set没有下标索引,所以对Set的遍历要通过迭代器Iterator 二.HashSet 1.HashSet由一个哈希表支持,内部 ...
- HashSet和TreeSet 的区别与分析
Set是java中一个不包含重复元素的collection.更正式地说,set 不包含满足 e1.equals(e2) 的元素对 e1 和 e2,并且最多包含一个 null 元素.正如其名称所暗示的, ...
- java集合系列——Set之HashSet和TreeSet介绍(十)
一.Set的简介 Set是一个不包含重复元素的 collection.更确切地讲,set 不包含满足 e1.equals(e2) 的元素.对 e1 和 e2,并且最多包含一个为 null 的元素. S ...
- HashSet,LinkedHashSet,TreeSet的区别
Set接口Set不允许包含相同的元素,如果试图把两个相同元素加入同一个集合中,add方法返回false.Set判断两个对象相同不是使用==运算符,而是根据equals方法.也就是说,只要两个对象用eq ...
- Java集合详解7:HashSet,TreeSet与LinkedHashSet
今天我们来探索一下HashSet,TreeSet与LinkedHashSet的基本原理与源码实现,由于这三个set都是基于之前文章的三个map进行实现的,所以推荐大家先看一下前面有关map的文章,结合 ...
- Java开发笔记(六十五)集合:HashSet和TreeSet
对于相同类型的一组数据,虽然Java已经提供了数组加以表达,但是数组的结构实在太简单了,第一它无法直接添加新元素,第二它只能按照线性排列,故而数组用于基本的操作倒还凑合,若要用于复杂的处理就无法胜任了 ...
- 【java提高】---HashSet 与TreeSet和LinkedHashSet的区别
HashSet 与TreeSet和LinkedHashSet的区别 今天项目开发,需要通过两个条件去查询数据库数据,同时只要满足一个条件就可以取出这个对象.所以通过取出的数据肯定会有重复,所以要去掉重 ...
- Java 容器 & 泛型:三、HashSet,TreeSet 和 LinkedHashSet比较
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket 上一篇总结了下ArrayList .LinkedList和Vector比较,今天泥瓦匠总结下Hash ...
随机推荐
- xp IP安全策略 ipseccmd
///下载 ipseccmd.exe //禁止 xp 连接 public static void BannedXPRunCmd() { string str = Console.ReadLine(); ...
- 【转】acm小技巧
1.一般用c语言节约空间,要用c++库函数或STL时才用c++: cout.cin和printf.scanf最好不要混用. 大数据输入输出最后不用cin.cout,纺织超市. 2.有时候int型不够用 ...
- MySQL练习2
学习MySQL过程中做的练习题,感觉是不错的例子就记录下来. 练习要使用到的表: 表一:员工表–> yg y_id y_name y_sex y_age y_address b_id 1 赵老大 ...
- Mac入门(一)基本用法
我前五年一直外包到微软,每天使用的都是Windows系统和.NET. 2012年加入VMware, 公司的工作机是台Mac 笔记本(MacBook Pro), 所以有机会接触Mac系统 Mac和Wi ...
- 分享15个优秀的 CSS 解决方案和工具
CSS 代码是很难管理,尤其是在大型项目. 样式都写在一个全局作用域里,通过复杂的选择器来指向特定的页面元素.冗余.膨胀和维护可以成为前端开发人员的一场噩梦.幸运的是我们有一些 CSS 工具来帮助开发 ...
- 鼠标悬停显示CSS3动画边框
效果体验:http://keleyi.com/keleyi/phtml/css3/14.htm 以下是代码: <!DOCTYPE html> <html xmlns="ht ...
- IE7浏览器窗口大小改变事件执行多次bug(转)
var resizeTimer = null; $(window).resize(function() { if (resizeTimer) clearTimeout(resizeTimer); re ...
- 翻译:常见问题——ABAP Development Tools for Eclipse
ABAP Development Tools for Eclipse(简称ADT)是一个基于Eclipse的全新ABAP IDE,这篇文档试图回答有关ADT的最重要的常见问题.这只是一个开始,如果你对 ...
- iOS RunLoop简介
一.什么是RunLoop? RunLoop是运行循环,每个Cocoa应用程序都由一个处于阻塞状态的do/while循环驱动,当有事件发生时,就把事件分派给合适的监听器,如此反复直到循环停止.处理分派的 ...
- Android开发学习——Android项目的目录结构
Android项目的目录结构: 资源文件夹: 清单配置文件: Android的四大组件在使用前全部需要在清单文件中配置 <?xml version="1.0" encodin ...