自定义类型在Set容器里
来源:互联网 发布:个人房源采集软件 编辑:程序博客网 时间:2024/06/05 21:59
一、子定义Person类在Set容器里的唯一值条件
1.自定义Person类
2.重写hashCode() 与 equals()函数
public class Person {private String id;private String name;private int age; public Person(){}public Person(String id, String name, int age) {super();this.id = id;this.name = name;this.age = age;}@Override//确定自定义类型的hashCode的值public int hashCode() {System.out.println("hashcode");final int prime = 31;int result = 1;result = prime * result + age;result = prime * result + ((id == null) ? 0 : id.hashCode());result = prime * result + ((name == null) ? 0 : name.hashCode());return result;}@Override//确定自定类型的比较方式: 这里是如果年龄、id、姓名都相等的话就是相等//没有重写该方法的情况,系统默认使用Object的equals()方法 ,比较的是地址public boolean equals(Object obj) {System.out.println("equals");if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Person other = (Person) obj;if (age != other.age)return false;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;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}在容器中添加自定义的类
import java.util.HashSet;import java.util.Set;public class Test {public static void main(String[] args) {Set<Person> set = new HashSet<Person>();set.add(new Person("1001","xiaolizi",20)); // 1set.add(new Person("1002","xiaoming",22));set.add(new Person("1002","xiaoming",22)); // 3System.out.println(set.size());}}ps: 1和3处使用了Person类的hashCode();方法后,他们的hashCode值相同
使用equals()方法进行比较; 确定 1和3相同;
结果
二、自定义类型在TreeSet的排序方法
1.前面步骤相同
2.内部比较,实现Comparable 接口,重写方法comparaTo
public class Student implements Comparable<Student>{private String name;private int age;public Student() {}public Student(String name, int age) {super();this.name = name;this.age = age;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + age;result = prime * result + ((name == null) ? 0 : name.hashCode());return result;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + "]";}/* * 实现了Comparable接口,重写了compareTo方法 ;来实现内部比较 * 定义该类在Set里面的排序方式 */@Overridepublic int compareTo(Student o) {//按照age来排序return this.age - o.age; // 小于、等于或大于时,则分别返回负整数、零或正整数。 }@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Student other = (Student) obj;if (age != other.age)return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;return true;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
测试类
import java.util.Set;import java.util.TreeSet;public class TestStu {public static void main(String[] args) {Set<Student> set = new TreeSet<Student>();set.add(new Student("zhangsan",12));set.add(new Student("lisi",10));set.add(new Student("zhangsan",12));System.out.println("容器内元素"+set.size());System.out.println(set);}}
执行结果:
阅读全文
1 0
- 自定义类型在Set容器里
- set容器存储自定义类型数据
- (c++)关于stl里set容器的自定义排序
- (c++)关于stl里set容器的自定义排序
- 关于stl里set容器的自定义排序
- (c++)关于stl里set容器的自定义排序
- set,multiset容器类型
- set,multiset容器类型
- c++ 自定义排序容器set
- 关联性容器存放自定义类型
- Java容器类型初探-List 与 Set
- C++ 10.4 关联容器-----set 类型
- C++容器(五):set类型
- 类型RadioButton的控件必须放在runat=server的容器里
- set集合容器自定义比较函数
- map和set容器自定义比较函数
- 如何在Joomla!新建菜单类型里快速增加自定义组件类型?
- Python 里的集合(set)类型
- 6.2—排序—Merge Two Sorted Lists
- synchronized
- 总师组的工作模式和方法
- 6.3—排序—Merge k Sorted Lists
- 宏函数与普通函数的比较
- 自定义类型在Set容器里
- [leetcode]138. Copy List with Random Pointer@Java解题报告
- 6.4—排序—Insertion Sort List
- 安卓开发由一个APP拉起另一个APP的方法总结
- webrtc 服务器搭建
- 6.5—排序—Sort List
- 微信开发2
- Kali linux 2016.2(Rolling)里安装OpenVAS
- 6.6—排序—First Missing Positive