Set接口
Set接口
-
特点:无序、无下标、元素不可重复
-
方法:全部继承自Collection中的方法
Set接口使用
示例代码:
/** * 测试Set接口的使用 * 特点:无序、无下标、不可重复 */ public class Demo { public static void main(String[] args) { //创建集合 Set<String > set = new HashSet<>(); //添加数据 set.add("c"); set.add("b"); set.add("a"); set.add("a"); System.out.println(set.size()); System.out.println(set.toString()); //2.删除 set.remove("a"); System.out.println(set.size()); System.out.println(set.toString()); System.out.println("------------------------------"); //3.遍历【重点】 //3.1 增强for for (String s:set){ System.out.println(s); } //3.2使用迭代器 Iterator<String> iterator = set.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } //4.判断 System.out.println(set.contains("a")); System.out.println(set.isEmpty()); } }
Set接口实现类
- HashSet【重点】:
- 基于HashCode实现元素不可重复
- 当存入元素的哈希码相同时,会调用equals进行确认,如结果为true,则拒绝后者存入
- TreeSet:
- 基于排列顺序实现元素不重复
HashSet使用(1)
示例代码:
/**
* HashSet的使用
* 存储结构:哈希表(数组+链表+红黑树)
*/
public class Demo {
public static void main(String[] args) {
//创建集合
HashSet<String> hashSet = new HashSet<>();
//1.添加元素
hashSet.add("a");
hashSet.add("b");
hashSet.add("c");
hashSet.add("d");
System.out.println(hashSet.size());
System.out.println(hashSet.toString());
//2.删除数据
hashSet.remove("a");
System.out.println(hashSet.size());
System.out.println(hashSet.toString());
System.out.println("------------------------");
//3.遍历
//3.1增强for
for (String s:hashSet){
System.out.println(s);
}
//3.2 迭代器
Iterator<String> iterator = hashSet.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//4.判断
System.out.println(hashSet.contains("b"));
System.out.println(hashSet.isEmpty());
}
}
HashSet的使用(2)
示例代码:
/**
* HashSet的使用
* 存储结构:哈希表(数组+链表+红黑树)
* 存储过程
* 1.根据hashcode,计算保存的位置,如果此位置为空,直接保存,不为空执行第二步
* 2.再执行equals方法,如果equals方法返回true,则认为是重复,反之,形成链表
*/
public class Demo {
public static void main(String[] args) {
//创建集合
HashSet<Person> hashSet = new HashSet<>();
//1.添加元素
Person p1 = new Person("a",10);
Person p2 = new Person("b",20);
Person p3 = new Person("c",30);
hashSet.add(p1);
hashSet.add(p2);
hashSet.add(p3);
hashSet.add(new Person("c",30));
//hashSet.add(p3); //重复
System.out.println(hashSet.size());
System.out.println(hashSet.toString());
//2.删除数据
//hashSet.remove(p1);
//hashSet.remove(new Person("a",10)); //根据hashcode找
//System.out.println(hashSet.size());
//System.out.println(hashSet.toString());
System.out.println("------------------------");
//3.遍历
//3.1增强for
for (Person s:hashSet){
System.out.println(s);
}
//3.2 迭代器
Iterator<Person> iterator = hashSet.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//4.判断
//System.out.println(hashSet.contains(p1));
System.out.println(hashSet.contains(new Person("a",10)));
System.out.println(hashSet.isEmpty());
}
}
//重写hashcode()方法和equals()方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age &&
Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
TreeSet使用(1)
示例代码:
/**
* TreeSet的使用
* 存储结构:红黑树
*/
public class Demo {
public static void main(String[] args) {
//创建集合
TreeSet<String> treeSet = new TreeSet<>();
//1.添加元素
treeSet.add("a");
treeSet.add("b");
treeSet.add("c");
//treeSet.add("a");
System.out.println(treeSet.size());
System.out.println(treeSet.toString());
//2.删除
//treeSet.remove("a");
System.out.println(treeSet.size());
System.out.println(treeSet.toString());
System.out.println("-----------------");
//3.遍历
for (String s:treeSet){
System.out.println(s);
}
Iterator<String> iterator = treeSet.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//判断
System.out.println(treeSet.contains("a"));
System.out.println(treeSet.isEmpty());
}
}
TreeSet使用(2)
示例代码:
/**
* 使用TreeSet保存数据
* 存储结构:红黑树
* 要求:元素必须要实现Comparable接口,compareTo()方法返回值为0,认为是重复元素
*/
public class Demo {
public static void main(String[] args) {
//创建一个集合
TreeSet<Person> person = new TreeSet<>();
//添加
Person p1 = new Person("xyz",10);
Person p2 = new Person("hello",20);
Person p3 = new Person("zhangsan",30);
person.add(p1);
person.add(p2);
person.add(p3);
System.out.println(person.size());
System.out.println(person.toString());
//2.删除
// person.remove(p1);
// System.out.println(person.size());
// System.out.println(person.toString());
// person.remove(new Person("hello",20));
// System.out.println(person.size());
// System.out.println(person.toString());
//3.遍历
//增强for
for(Person p:person){
System.out.println(p);
}
//迭代器
Iterator<Person> iterator = person.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
System.out.println("-------------------");
//判断
System.out.println(person.contains(new Person("xyz",10)));
System.out.println(person.isEmpty());
}
}
//person类
public class Person implements Comparable<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 || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age &&
Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
//先按姓名来比,再按年龄比
@Override
public int compareTo(Person o) {
int n1 = this.getName().compareTo(o.getName());
int n2 = this.age - o.getAge();
return n1 == 0?n2:n1;
}
}
Comparator接口使用
/**
* TreeSet集合的使用
* Comparator:实现定制比较(比较器)
*/
public class Demo {
public static void main(String[] args) {
//创建一个集合并定制比较规则
TreeSet<Person> person = 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("xyz",30);
Person p2 = new Person("hello",20);
Person p3 = new Person("zhangsan",10);
Person p4 = new Person("lisi",10);
person.add(p1);
person.add(p2);
person.add(p3);
person.add(p4);
System.out.println(person.toString());
}
}
TreeSet案例
示例代码:
/**
* 使用TreeSet集合实现字符串按照长度进行排序
* helloworld zhang lisi wangwu chongqing beijing
* Comparator接口实现定制比较
*/
public class Demo {
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("zhang");
strings.add("lisi");
strings.add("wangwu");
strings.add("chongqing");
strings.add("beijing");
strings.add("xian");
System.out.println(strings.toString());
}
}