set集合
set也实现了Collection接口,具有Collection的所有属性和方法。
set集合里的元素不重复,并且最多只有一个null元素。
HashSet
HashSet实现了Set接口,底层使用哈希表结构,具备Set集合的特点,并且存取元素无序,存储不重复元素和查询元素的效率比数组高,而且HashSet只能使用Iterator迭代器
public static void main(String[] args) {
HashSet<Student> set = new HashSet<Student>();
Student s1 = new Student(12, "aaa");
Student s2 = new Student(14, "bbb");
Student s3 = new Student(16, "sss");
Student s4 = new Student(14, "zzz");
//添加方法
set.add(s1);
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s2);
set.add(s4);
//添加一个age和name都一样的对象
set.add(new Student(14, "bbb"));
//删除方法
boolean f = set.remove(new Student(16, "sss"));
System.out.println(f);
Iterator it = set.iterator();
while(it.hasNext()) {
System.out.print(it.next()+"\t");
}
System.out.println();
for(Student str:set) {
System.out.print(str+"\t");
}
System.out.println("\n"+set.size());
}
程序运行后的结果是:
true
Student [age=14, name=bbb] Student [age=12, name=aaa] Student [age=14, name=zzz]
Student [age=14, name=bbb] Student [age=12, name=aaa] Student [age=14, name=zzz]
3
遍历set集合可以使用Iterator迭代器,也可以使用foreach循环
集合执行了两次添加s1对象的方法,但是最终只有一个被添加到了集合中,同时也添加了两次age为16,name为sss对象,这是两个不一样的对象,但是最终也只有一个被添加到了集合中,这是在实体类中重写了hashCode和equals方法,对添加的对象进行判断,只要对象的属性完全一样,那么集合就会认为这个是同一个对象。
直接在IDE中构造这两个方法
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
//重写equals方法,如果两个对象的值或者地址或者getClass一致,则认为这两个是同一个对象
@Override
public 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;
}
//自己手写一个equals方法
// public boolean equals(Object obj) {
// if(!(obj instanceof Student)) {
// throw new RuntimeException();
// }
// Student other = (Student)obj;
// if(this.name.equals(other.name) && this.age == other.age) {
// return true;
// }
//
// return false;
// }
TreeSet
特点:TreeSet集合底层使用二叉树结构,不允许存储重复元素、最多只能存储一个null元素,存储的元素会按照指定的规则自动排序,排序方式有两种:自然排序、比较器排序
public static void main(String[] args) {
TreeSet<Student> set = new TreeSet<Student>();
Student s1 = new Student(12, "aaa");
Student s2 = new Student(14, "ggg");
Student s3 = new Student(16, "ccc");
Student s4 = new Student(14, "zzz");
boolean f = set.remove("zcc");
System.out.println(f);
set.add(s1);
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s2);
set.add(s4);
Iterator it = set.iterator();
while(it.hasNext()) {
System.out.print(it.next()+"\t");
}
}
执行结果:
false
Student [age=12, name=aaa] Student [age=16, name=ccc] Student [age=14, name=ggg] Student [age=14, name=zzz]
创建一个TreeSet集合,并在其中添加可以进行排序的元素,底层在存储自定义元素时,使用自然排序的情况下,会强制把要存储的自定义对象转为Comparable对象,Student对象和Comparable对象没有任何关系,无法实现强制转换,就会引发类型转换异常,这时我们需要让Student对象实现Comparable接口,并重写接口中的compareTo方法
public class Student implements Comparable {
private int age;
private String name;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int age, String name) {
super();
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [age=" + age + ", name=" + name + "]";
}
//在实体类中自然排序
@Override
public int compareTo(Object o) {
if(!(o instanceof Student)) {
throw new RuntimeException();
}
Student other = (Student)o;
int flag = this.name.compareTo(other.name);
return flag == 0?this.age-other.age:flag;
}
}
这里的compareTo方法是按name的自然顺序在排序。
也可以自定义一个比较器,实现Comparator<>接口
public class MyComparetor implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
int flag = o1.getAge() - o2.getAge();
return flag == 0?o1.getName().compareTo(o2.getName()):flag;
}
}
自定义的比较器是按age的自然排序在做比较,当比较器和实体类中的比较方法同时存在时,优先实现比较器
public static void main(String[] args) {
//当实体类中的比较方法和实现的比较器同时存在时,优先实现比较器
TreeSet<Student> set = new TreeSet<Student>(new MyComparetor());
Student s1 = new Student(12, "aaa");
Student s2 = new Student(14, "ggg");
Student s3 = new Student(16, "ccc");
Student s4 = new Student(14, "zzz");
boolean f = set.remove("zcc");
System.out.println(f);
set.add(s1);
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s2);
set.add(s4);
Iterator it = set.iterator();
while(it.hasNext()) {
System.out.print(it.next()+"\t");
}
}
这里同时调用了实体类的自然比较和比较器
false
Student [age=12, name=aaa] Student [age=14, name=ggg] Student [age=14, name=zzz] Student [age=16, name=ccc]
最终的比较结果是比较器的比较规则。
Comparable和Comparator的区别:
- Comparable属于自然排序,必须保证元素自身实现Comparable接口,并重写该接口中的compareTo方法
- Comparator属于强制排序,元素不需要实现Comparator接口,只需要在创建具有二叉树结构的对象时,在构造方法中体现出Comparator对象
- Comparable针对元素, Comparator针对集合
- 当TreeSet集合中同时具有自然排序、比较器排序时,优先使用比较器排序
Map集合的所有方法可以在API中查找。