首页 技术 正文
技术 2022年11月12日
0 收藏 989 点赞 4,860 浏览 2328 个字
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);
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905