每添加一个对象,都会调用hashcode方法
1.TreeSet
排好序的, 并且 不允许重复
set 无序并且不允许重复
tree底层利用的是红黑树
排序:
自定义的类
Integer
String
return 0 代表即将要添加的元素和现有的元素重复 则不添加
升序:this>o return 1
this<o return -1
降序 this>o return -1;
this<o return 1;
TreeSet 可以排序的, 泛型是自定义类的话 必须实现排序接口
Comparable 自然排序
2.自然排序
- 在泛型中的自定义类中实现Comparable<自定义类名>
- 重写compareTo方法
- 创建TreeSet集合,向集合中添加元素
一个元素比较排序
public class Student implements Comparable<Student>{
int age;
public Student(int age) {
super();
this.age = age;
}
@Override
public String toString() {
return "Student [age=" + age + "]";
}
@Override
public int compareTo(Student o) {
System.out.println("===compareTo===");
if(this.age>o.age) {
return -1;
}else if (this.age<o.age) {
return 1;
}
return 0;
}
}
public class Test {
public static void main(String[] args) {
TreeSet<Student> set = new TreeSet<>();
set.add(new Student(5));
set.add(new Student(33));
set.add(new Student(1));
set.add(new Student(6));
set.add(new Student(6));
System.out.println(se);
}
}
两个元素比较排序
public class Animal implements Comparable<Animal>{
String name;
int age;
public Animal(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public int compareTo(Animal o) {
//第一条件 age 降序
if(this.age>o.age) {
return -1;
}else if (this.age<o.age) {
return 1;
}else {
//第二条件 姓名升序
int num = this.name.compareTo(o.name);
return num;
}
}
@Override
public String toString() {
return "Animal [name=" + name + ", age=" + age + "]";
}
}
public class Test {
public static void main(String[] args) {
TreeSet<Animal> set = new TreeSet<>();
set.add(new Animal("zhangsan", 4));
set.add(new Animal("zhangsan", 4));
set.add(new Animal("zhangsan", 14));
set.add(new Animal("lisi", 14));
set.add(new Animal("wangwu", 14));
for (Animal animal : set) {
System.out.println(animal);
}
}
3.定制排序
1.编写自定义类
2.单独定义比较器类,实现Comparator<自定义类>
3. 重写compare方法
4.创建TreeSet集合,new TreeSet<>(new 比较器对象)
定制排序比自然排序耦合性低
public class Student {
String name;
int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
import java.util.Comparator;
public class StudentComparator implements Comparator<Student>{
//compare和CompareTo方法一样
@Override
public int compare(Student o1, Student o2) {
//年龄降序排序
if(o1.age>o2.age) {
return -1;
}else if (o1.age<o2.age) {
return 1;
}
return 0;
}
}
import java.util.TreeSet;
public class Test {
public static void main(String[] args) {
TreeSet<Student> set = new TreeSet<>(new StudentComparator());
set.add(new Student("a",1));
set.add(new Student("a",5));
set.add(new Student("a",4));
set.add(new Student("a",2));
set.add(new Student("a",11));
for (Student student : set) {
System.out.println(student);
}
}
}
4.Map的三种遍历方法
不能通过增强for循环直接遍历集合
1.Set<Map.Entry<K,V>> entrySet()
2.Set keySet()
3.Collection values()
通过一些方式进行转换, 转换成 set,Collection这种集合进行遍历
Set keySet()
import java.util.Collection;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
public class Test {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
// 添加元素
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
// 如果键有相同,键只能存储一份,但是新的值覆盖旧的值
map.put("a", 5);
System.out.println(map);
// 判断是否包含指定的键
boolean b = map.containsValue("a");
System.out.println(b);
// 判断是否包含指定的值
boolean containValue = map.containsValue(13);
System.out.println(containValue);
// 判断是否为空(集合中没有元素) 没有元素结果为true 有元素结果为false
boolean empty = map.isEmpty();
System.out.println(empty);
// 根据键 得到值
Integer value = map.get("a");
System.out.println(value);
// 1. 将集合中所有的键全部拿到
Set<String> keys = map.keySet();
for (String key : keys) {
Integer value2 = map.get(key);
System.out.println(key + "=" + value2);
}
// 2. Collection<V> values() 只能拿到所有的值
// 只能通过键找值,不能通过值找键
Collection<Integer> values = map.values();
for (Integer integer : values) {
System.out.println(integer);
}
// 3. Set<Map.Entry<K,V>> entrySet()
Set<Entry<String, Integer>> set = map.entrySet();
for (Entry<String, Integer> entry : set) {
String key = entry.getKey();
Integer value2 = entry.getValue();
System.out.println(key + "=" + value2);
}
}
}