set子接口
特点:无序,无下标,元素不可重复
Set实现类
//测试set接口的使用
public class set {
public static void main(String[] args) {
//创建集合
Set<String> set = new HashSet<>();
//添加数据
set.add("小米");
set.add("苹果");
set.add("华为");
System.out.println("元素个数:" + set.size());
System.out.println(set.toString());
//删除
set.remove("小米");
//遍历
//增强for
for (String string : set) {
System.out.println(string);
}
//迭代器
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
//判断
System.out.println(set.contains("华为"));
System.out.println(set.isEmpty());
}
}
HashSet:
1.基于HashCode实现元素不重复
2.当存入元素的哈希码相同时,会调用equals确认,如结果为true,
则拒绝后者存入
//HashSet集合的使用
//存储结构:哈希表(数组+链表+红黑树)
//存储过程
//1.根据hashcode计算保存的位置,如果此位置为空,则直接保存,如果不为空执行第二步
//2.在执行equals方法,如果equals方法为true,则认为是重复,否则,完成链表
public class Hash_Set {
public static void main(String[] args) {
//新建集合
HashSet<String> hashSet = new HashSet<String>();
//添加元素
hashSet.add("joke");
hashSet.add("rose");
hashSet.add("tom");
hashSet.add("mike");
//删除
hashSet.remove("mike");
//遍历
//增强for
for (String string : hashSet) {
System.out.println(string);
}
System.out.println("=================");
//迭代器
Iterator<String> iterator = hashSet.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
//判断
}
}
public class HashSetTest {
public static void main(String[] args) {
Person s1 = new Person("jack",20);
Person s2 = new Person("rose",18);
Person s3 = new Person("mike",20);
Person s4 = new Person("tom",19);
//创建集合
HashSet<Person> hashSet = new HashSet<Person>();
//增加
hashSet.add(s1);
hashSet.add(s2);
hashSet.add(s3);
hashSet.add(s4);
hashSet.add(new Person("tom",19));
System.out.println(hashSet.toString());
System.out.println("=============================");
//删除
hashSet.remove(new Person("jack",20));
System.out.println(hashSet.toString());
System.out.println("==============================");
//遍历
//增强for
for (Person person : hashSet) {
System.out.println(person.toString());
}
System.out.println("===============================");
//迭代器
Iterator<Person> iterator = hashSet.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
@Override
public boolean equals(Object obj) {
//判断是不是同一个对象
if(this == obj) {
return true;
}
//判断是否为空
if (obj == null) {
return false;
}
//判断是否为Person类
if (obj instanceof Collection.Set.Person) {
Collection.Set.Person p = (Collection.Set.Person)obj;
//比较属性
if (this.name.equals(p.getName()) && this.age == p.getAge()) {
return true;
}
}
//不满足条件
return false;
}
@Override
public int hashCode() {
int n1 = this.name.hashCode();
int n2 = this.age + 31;//1.)31是质数,减少散列冲突 2.)31可以提高执行效率 31*i = (i<<5) - i
return n1 + n2;
}
TreeSet:
1.基于排列顺序实现元素不重复
2.实现了SortedSet接口,对集合元素自动排序
3.元素对象必须实现Comparable接口,指定排序规则
4.通过CompareTo方法确定是否为重复元素
//TreeSet:
//1.基于排列顺序实现元素不重复
//2.实现了SortedSet接口,对集合元素自动排序
//3.元素对象必须实现Comparable接口,指定排序规则
//4.通过CompareTo方法确定是否为重复元素
//存储结构:红黑树
import java.util.Iterator;
import java.util.TreeSet;
public class Tree_Set<Good> {
public static void main(String[] args) {
TreeSet<String> treeSet = new TreeSet<>();
//1.添加元素
treeSet.add("xyz");
treeSet.add("xyz");
treeSet.add("abc");
treeSet.add("hello");
System.out.println("元素个数" + treeSet.size());
System.out.println(treeSet.toString());
//2.遍历
//2.1增强for
for (String string:treeSet) {
System.out.println(string);
}
System.out.println("-------------");
//2.2迭代器
Iterator<String> iterator = treeSet.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
//3.删除
treeSet.remove("xyz");
System.out.println(treeSet.toString());
}
}
//创建集合
TreeSet<Student> students = new TreeSet<>();
Student s1 = new Student("a张三",20);
Student s2 = new Student("王八",99);
Student s3 = new Student("小二",12);
//添加学生
students.add(s1);
students.add(s2);
students.add(s3);
System.out.println("元素个数:" + students.size());
System.out.println(students.toString());
System.out.println("------------------");
//遍历
for (Student string : students) {
System.out.println(string.toString());
}
System.out.println("-----------------");
Iterator<Student> iterator = students.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
System.out.println("------------------");
//删除
students.remove(new Student("小二",12));
System.out.println(students.toString());
}
//在Student中实现Comparable方法
public class Student implements Comparable<Student> {
@Override
public int compareTo(Student o) {
int n1 = this.getName().compareTo(o.getName());
int n2 = this.getAge() - o.getAge();
return n1 == 0?n2:n1;
}
}
Comparator:比较器(自定义比较规则)
Comparble:可比较的
//使用TreeSet集合实现字符串按照长度进行排序
//创建集合并指定比较规则
TreeSet<Student> students = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int n1 = o1.getAge() - o2.getAge();
int n2 = o1.getName().compareTo(o2.getName());
return n1==0?n2:n1;
}
});
//使用TreeSet集合实现字符串按照长度进行排序
public class TreeSet_test {
public static void main(String[] args) {
TreeSet<String> strings = new TreeSet<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int n1 = o1.length() - o2.length();
int n2 = o1.compareTo(o2);
return n1==0?n2:n1;
}
});
//添加数据
strings.add("HelloWorld");
strings.add("BeiJing");
strings.add("NanJing");
strings.add("HangZhou");
strings.add("ShangHai");
strings.add("Top");
System.out.println(strings.toString());
}
}