一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第1天,点击查看活动详情。
HashSet
介绍
完全基于HashMap(数组+链表+(红黑树))实现的。
存储无序, 无下标, 元素不重复数据。
代码示例
public class HashSetTest {
public static void main(String[] args) {
Set<Student> set = new HashSet<>();
set.add(new Student("张三", 12,1));
set.add(new Student("李四", 12,2));
set.add(new Student("王五", 12,3));
set.add(new Student("张三", 12,4));
System.out.println(set);
}
}
//[Student{name='王五', age=12, id=3}, Student{name='张三', age=12, id=1}, Student{name='李四', age=12, id=2}]
注意
HashSet 保证唯一方式 必须重写hashcode和equals
TreeSet
介绍
底层是基于TreeMap红黑树。红黑树底层进行会进行值比较 使用 Comparable中CompareTo进行 所以存储泛型类必须实现 Comparable接口或外部定义类实现Comparator接口
代码示例
package com.lee.collection;
import java.util.Objects;
//实现Comparable接口并 重写compareTo方法 否则报Student cannot be cast to java.lang.Comparable错误
public class Student implements Comparable<Student> {
private String name;
private int age;
private int id;
public Student(String name, int age, int id) {
this.name = name;
this.age = age;
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
//好像触及到掘金md解析的关键字变红了
return "Student{" +
"name='" + name + ''' +
", age=" + age +
", id=" + id +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
id == student.id &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age, id);
}
//自定义比较内容
@Override
public int compareTo(Student o) {
return this.id - o.id;
}
}
//或自定义类实现Comparator接口
public class Compares implements Comparator<Teacher> {
@Override
public int compare(Teacher o1, Teacher o2) {
return o1.getAge() - o2.getAge();
}
}
public class TreeSetTest {
public static void main(String[] args) {
Set<Student> set = new TreeSet<>();
set.add(new Student("张三", 12,1));
set.add(new Student("李四", 12,2));
set.add(new Student("王五", 12,3));
set.add(new Student("张三", 12,1));
System.out.println(set);
//也可 通过外部类实现Comparator TreeSet构造设置
// Set<Teacher> set = new TreeSet<>(new Compares());
// set.add(new Teacher( 12,1));
// set.add(new Teacher( 13,1));
// set.add(new Teacher( 12,1));
// System.out.println(set);
}
}
HashMap与TreeMap
介绍
HashMap与HashTree实现 Map接口与Collection没有关系 Map中每个元素都是Entry类型,每个元素都包含Key(键)和Value(值)二者十分类似
HashMap是对散列表的具体实现。
TreeMap是红黑树的具体实现。
里面都包含Key-Value值。
代码示例
public class HashMapTest {
public static void main(String[] args) {
Map<Integer,String> map = new HashMap<>();
Map<Integer,String> map2 = new TreeMap<>();
map.put(1, "bj");
map.put(2, "bj2");
map.put(3, "bj3");
map.put(1, "bj4");
map.put(null, "bj5");
map.put(null, "bj6"); //hashmap允许null
System.out.println(map);
// map2.put(1, "bj");
// map2.put(2, "bj2");
// map2.put(3, "bj2");
// map2.put(1, "bj4");
//// map2.put(null, "bj5"); //treemap 不允许null
// System.out.println(map2);
System.out.println("===========map遍历1============");
map.forEach((k,v)->{
System.out.println(k+"---"+v);
});
System.out.println("===========map遍历2============");
Set<Integer> integers = map.keySet();
for (Integer key : integers) {
System.out.println(key+"---"+map.get(key));
}
System.out.println("===========map遍历3============");
Collection<String> values = map.values();
for (String value : values) {
System.out.println(value);
}
System.out.println("===========map遍历4============");
Set<Map.Entry<Integer, String>> entries = map.entrySet();
for (Map.Entry<Integer, String> entry : entries) {
System.out.println(entry.getKey()+"---"+entry.getValue());
}
}
}