Set子接口
- 特点:无序,无下标,元素不可重复。
- 方法:全部继承自callection中的方法。
Set实现类
- HashSet【重点】:
- 基于HashCode实现元素不重复。
- 当存入元素的哈希码相同时,会调用equals进行确认,如果结果为true,则拒绝后者存入。
- TreeSet:
- 基于排列顺序实现元素不重复。
- 实现了SortedSet接口,对集合元素自动排序。
- 元素对象的类型必须实现Comparable接口,指定排序规则。
- 通过CompareTo方法确定是否为重复元素。
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
* Set接口的使用
* 特点:无序,无下标,元素不能重复
*/
public class Demo01 {
public static void main(String[] args) {
//创建对象
Set<String> hashSet = new HashSet<>();
//添加数据
hashSet.add("张三");
hashSet.add("李四");
hashSet.add("王五");
hashSet.add("赵六");
System.out.println("元素个数:"+hashSet.size());
System.out.println(hashSet.toString());
//删除数据
// hashSet.remove("张三");
// hashSet.clear();
// System.out.println(hashSet.size());
// System.out.println(hashSet.toString());
//遍历数据【重点】
System.out.println("———————————————增强For—————————————————————");
for (String s : hashSet) {
System.out.print(s+" ");
}
System.out.println("\n———————————————迭代器—————————————————————————————");
Iterator<String> iterator = hashSet.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//判断
System.out.println(hashSet.contains("张三"));
System.out.println(hashSet.isEmpty());
}
}
hashset
import java.util.HashSet;
import java.util.Iterator;
/**
* HashSet集合的使用
* 存储结构:哈希表(数组+链表+红黑树)数组加链表就好比超市一排收银台加多排顾客的感觉,收银台横排是数组,顾客竖排是链表(单向链表)哈希值相同时进入链表。
*/
public class Demo02 {
public static void main(String[] args) {
//创建集合
HashSet<String> hashSet = new HashSet<>();
//添加元素
hashSet.add("张学友");
hashSet.add("刘德华");
hashSet.add("郭富城");
hashSet.add("黎明");
System.out.println("元素个数:"+hashSet.size());
System.out.println(hashSet.toString());
hashSet.remove("黎明");
System.out.println("元素个数:"+hashSet.size());
//遍历
System.out.println("———————————————————————增强for—————————————————————————————");
for (String s : hashSet) {
System.out.print(s+" ");
}
System.out.println("\n———————————————————————迭代器—————————————————————————————");
Iterator<String> iterator = hashSet.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next()+" ");
}
System.out.println();
//判断
System.out.println(hashSet.contains("郭富城"));
System.out.println(hashSet.isEmpty());
}
}
import java.util.HashSet;
import java.util.Iterator;
/**
* HashSet集合的使用
* 存储结构:哈希表(数组+链表+红黑树(JDK1.8))
* 存储过程:
* (1)根据hashCode计算保存的位置,如果此位置为空,则直接保存,如果此位置不为空,则执行第二步。
* (2)再执行equals方法,如果equals方法为true,则认为是重复,否则形成链表。
*/
public class Demo03 {
public static void main(String[] args) {
//创建对象
HashSet<Person> hashSet = new HashSet<>();
Person P1 = new Person("张学友", 20);
Person P2 = new Person("刘德华", 19);
Person P3 = new Person("郭富城", 18);
Person P4 = new Person("黎明", 17);
//添加元素
hashSet.add(P1);
hashSet.add(P2);
hashSet.add(P3);
hashSet.add(P4);
hashSet.add(new Person("黎明",17));
System.out.println("元素个数:"+hashSet.size());
System.out.println(hashSet.toString());
//删除元素
// hashSet.remove(P1);
// hashSet.clear();
// System.out.println("元素个数:"+hashSet.size());
// 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());
}
//判断
System.out.println(hashSet.contains(new Person("黎明",17)));
System.out.println(hashSet.isEmpty());
}
}
import java.util.Objects;
//Person类
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
if (o instanceof Person) {
Person person = (Person) o;
return age == person.age && Objects.equals(name, person.name);
}
return false;
}
@Override
public int hashCode() {
return this.name.hashCode()+this.age;
}
}
TreeSet
普通:
import java.util.Iterator;
import java.util.TreeSet;
/**
* TreeSet的使用
* 存储结构:红黑树。(无序,无下标,元素不可重复)
*/
public class Demo04 {
public static void main(String[] args) {
//创建对象
TreeSet<String> treeSet = new TreeSet<>();
//添加元素
treeSet.add("张学友");
treeSet.add("刘德华");
treeSet.add("郭富城");
treeSet.add("黎明");
System.out.println("元素个数:"+treeSet.size());
System.out.println(treeSet.toString());
//删除元素
// treeSet.remove("张学友");
// treeSet.clear();
// System.out.println("元素个数:"+treeSet.size());
// System.out.println(treeSet.toString());
//遍历
System.out.println("——————————————————增强for————————————————————————————————");
for (String s : treeSet) {
System.out.println(s);
}
System.out.println("——————————————————迭代器————————————————————————————————");
Iterator<String> iterator = treeSet.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//判断
System.out.println(treeSet.contains("黎明"));
System.out.println(treeSet.isEmpty());
}
}
进阶:
import java.util.Iterator;
import java.util.TreeSet;
/**
* 使用TreeSet保存复杂数据
* 存储结构:红黑树
* 要求:元素必须实现CompareTo接口中的compareTo()方法,如果返回值为0,则认为时重复元素。
*/
public class Demo05 {
public static void main(String[] args) {
//创建对象
TreeSet<Person> treeSet = new TreeSet<>();
Person p1 = new Person("张学友", 20);
Person p2 = new Person("刘德华", 19);
Person p3 = new Person("郭富城", 18);
Person p4 = new Person("黎明", 17);
//添加元素
treeSet.add(p1);//类需要先实现CompareTo接口才能添加到TreeSet中
treeSet.add(p2);
treeSet.add(p3);
treeSet.add(p4);
System.out.println(treeSet.size());
System.out.println(treeSet.toString());
//删除元素
// treeSet.remove(p1);
// treeSet.clear();
// System.out.println(treeSet.size());
//遍历元素
System.out.println("——————————————————增强for——————————————————————————");
for (Person person : treeSet) {
System.out.print(person+"\t");
}
System.out.println("\n——————————————————迭代器——————————————————————————————————");
Iterator iterator = treeSet.iterator();
while(iterator.hasNext()){
System.out.print(iterator.next()+"\t");
}
System.out.println();
//判断
System.out.println(treeSet.contains(p1));
System.out.println(treeSet.contains(new Person("张学友",20)));
System.out.println(treeSet.isEmpty());
}
}
高级:
import java.util.Comparator;
import java.util.TreeSet;
/**
* TreeSet集合的使用
* Comparator:实现定制比较(比较器)
* Comparable:可比较的
*/
public class Demo06 {
public static void main(String[] args){
//创建集合并指定比较规则。
TreeSet<Person> treeSet = new TreeSet<>(new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
int n1 = o1.getAge()- o2.getAge();
int n2 = o1.getName().compareTo(o2.getName());
return n1==0?n2:n1;
}
});
Person p1 = new Person("张学友", 20);
Person p2 = new Person("刘德华", 19);
Person p3 = new Person("郭富城", 18);
Person p4 = new Person("黎明", 18);
//添加元素
treeSet.add(p1);
treeSet.add(p2);
treeSet.add(p3);
treeSet.add(p4);
System.out.println(treeSet.size());
System.out.println(treeSet.toString());
}
}
案例:
import java.util.Comparator;
import java.util.TreeSet;
/**
* 要求:使用TreeSet集合实现字符串按照长度进行排序。
* helloword zhang lisi wangwu beijing xian nanjing 1 22 333 4444
* Comparator接口实现定制比较
*/
public class Demo07 {
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("helloword");
strings.add("zhang");
strings.add("lisi");
strings.add("wangwu");
strings.add("beijing");
strings.add("xian");
strings.add("nanjing");
strings.add("1");
strings.add("22");
strings.add("333");
strings.add("4444");
System.out.println(strings.size());
System.out.println(strings.toString());
}
}