java.util.Set 接口继承自 Collection 接口(java.util.Set接口 extends Collection接口),它与 Collection 接口中的方法基本一致,只是比 Collection 接口更加严格。与 List 接口不同的是, Set 接口中元素无序,并且都会以某种规则保证存入的元素不出现重复。
Set接口的特点:
1.不允许存储重复的元素
2.没有索引,没有带索引的方法,也不能使用普通的for循环遍历
java.util.HashSet 是 Set 接口的一个实现类,它所存储的元素是不可重复的,并且元素都是无序的(即存取顺序不一致)。HashSet 是根据对象的哈希值来确定元素在集合中的存储位置,因此具有良好的存取和查找性能。保证元素唯一性的方式依赖于: hashCode 与 equals 方法。
java.util.HashSet集合 implements Set接口
HashSet特点:
1.不允许存储重复的元素
2.没有索引,没有带索引的方法,也不能使用普通的for循环遍历
3.是一个无序的集合,存储元素和取出元素的顺序有可能不一致
4.底层是一个哈希表结构(查询的速度非常的快)
@Test
public void testSet01(){
Set set = new HashSet<>();
set.add("a");
set.add("b");
set.add("c");
set.add("c");
set.add("d");
Iterator it = set.iterator();
while (it.hasNext()) System.out.print(it.next());// abcd
}
比较器
方法一:实现Comparable比较系统的类型
// 比较int类型
@Test
public void intTest(){
int a = 1;
int b = 2;
System.out.println(a - b);// >0:a大;<0:b大;=0:一样大
}
// 比较double类型
@Test
public void doubleTest(){
Double a = (Double)12.1;
Double b = (Double)12.2;
System.out.println(a.compareTo(b)); // -1
}
// 比较String类型
@Test
public void StringTest(){
String a = "a";
String b = "b";
System.out.println(a.compareTo(b)); // -1
}
比较自定义类型:
student类:
public class Student02 implements Comparable{ private int age; private String 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; } public Student02(int age, String name) { this.age = age; this.name = name; } @Override public String toString() { return "Student{" + "age=" + age + ", name='" + name + ''' + '}'; } @Override public int compareTo(Student02 o) { // 1.如果学生姓名一样,就默认是一个人 // return this.getName().compareTo(o.getName()); // 2.如果学生年龄一样,就默认是一个人 return this.getAge() - o.getAge() ; } }
@Test
public void student02Test() {
Student02 s1 = new Student02(10,"aa");
Student02 s2 = new Student02(10,"aa");
System.out.println(s1.compareTo(s2)); // 0
}
方法二:实现public interface Comparatorstudent类:
public class Student{
private int age;
private String 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;
}
public Student(int age, String name) {
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + ''' +
'}';
}
}
// 外部自定义一个比较器,实现Compartor接口
// 1.如果学生姓名一样,就默认是一个人
class Bijiao01 implements Comparator {
@Override
public int compare(Student o1, Student o2) {
return o1.getName().compareTo(o2.getName());
}
}
// 2.如果学生年龄一样,就默认是一个人
class Bijiao02 implements Comparator {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge() - o2.getAge();
}
}
@Test
public void studentTest() {
Student s1 = new Student(10,"aa");
Student s2 = new Student(10,"bb");
Comparator a = new Bijiao01();
System.out.println(a.compare(s1, s2));// -1
Comparator b = new Bijiao02();
System.out.println(b.compare(s1, s2));// 0
}
TreeSet
TreeSet 通过比较器储存自定义类型元素
方法一:实现public interface Comparatorstudent类:
public class Student{
private int age;
private String 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;
}
public Student(int age, String name) {
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + ''' +
'}';
}
}
public static void main(String[] args) {
Set set = new TreeSet<>(new Comparator() {
@Override
public int compare(Student o1, Student o2) {
return o1.getName().compareTo(o2.getName());
}
});
set.add(new Student(10,"alili"));
set.add(new Student(11,"blili"));
set.add(new Student(11,"flili"));
set.add(new Student(12,"blili"));
set.add(new Student(15,"dlili"));
set.add(new Student(13,"elili"));
System.out.println(set.size());
Iterator it = set.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
}
其中:
方法二:实现Comparablestudent类:
public class Student02 implements Comparable{ private int age; private String 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; } public Student02(int age, String name) { this.age = age; this.name = name; } @Override public String toString() { return "Student{" + "age=" + age + ", name='" + name + ''' + '}'; } @Override public int compareTo(Student02 o) { // 1.如果学生姓名一样,就默认是一个人 // return this.getName().compareTo(o.getName()); // 2.如果学生年龄一样,就默认是一个人 return this.getAge() - o.getAge() ; } }
@Test
public void testComparable() {
Set set = new TreeSet<>();
set.add(new Student02(10,"alili"));
set.add(new Student02(11,"blili"));
set.add(new Student02(11,"flili"));
set.add(new Student02(12,"blili"));
set.add(new Student02(15,"dlili"));
set.add(new Student02(13,"elili"));
System.out.println(set.size());
Iterator it = set.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
}



