1 TreeSet
其实就和Hashset使用是一样的
1.1 TreeSet的使用
public class Test01 {
/**
* ֪知识点:Tree的使用
*
*/
public static void main(String[] args) {
//创建TreeSet集合
TreeSet<String> set = new TreeSet<>();
//添加元素
set.add("深海泰坦");
set.add("远古恐惧");
set.add("正义石像");
set.add("众星之子");
set.add("探险家");
set.add("虚空之女");
set.add("邪恶法师");
set.add("xxx");
set.add("yyy");
set.add("zzz");
//获取集合长度
int size = set.size();
System.out.println("集合长度为" + size);//10
TreeSet<String> newSet1 = new TreeSet<>();
Collections.addAll(newSet1, "aaa","bbb","ccc","ccc");//批量添加元素
set.addAll(newSet1);//
//set.clear();
System.out.println("判断集合中是否包含指定元素" + set.contains("远古恐惧"));//true
System.out.println("判断集合中是否包含子集合中所有元素" + set.containsAll(newSet1));//true
System.out.println("判断集合中是否没有元素" + set.isEmpty());//false
set.remove("深海泰坦");//移除指定元素
set.removeAll(newSet1);
TreeSet<String> newSet2 = new TreeSet<>();
Collections.addAll(newSet2, "xxx","yyy","zzz","zzz");
set.retainAll(newSet2);
Object[] objs = set.toArray();
System.out.println(Arrays.toString(objs));
String[] ss = new String[set.size()];
set.toArray(ss);
System.out.println(Arrays.toString(ss));
System.out.println("--------------------------------");
for (String element : set) {
System.out.println(element);
}
System.out.println("--------------------------------");
Iterator<String> it = set.iterator();
while(it.hasNext()){//
String element = it.next();//
System.out.println(element);
}
}
}
1.2 TreeSet的特点
public class Test02 {
/**
* TreeSet的特点:自然排序
* 理解:TreeSet会根据元素类型不同选择自动选择排序规则
*
* 排序规则写在对应的类当中,实现Comparable接口
*
* TreeSet在添加元素的时候,e.compareTo() 排序添加
*
* 内置比较器返回为什么是int类型??
*
* 整数判断出来的结果:整数,负数,0
* 负数就表示比比较对象小
* 正数表示比比较对象大
* 0表示与
*/
public static void main(String[] args) {
//TreeSet存储Interger的排序规则,数字升序
TreeSet<Integer> set1 = new TreeSet<>();
set1.add(30);
set1.add(10);
set1.add(50);
set1.add(40);
set1.add(20);
for (Integer element : set1) {
System.out.println(element);
}
System.out.println("---------------------------");
//TreeSet存储String的排序规则:字典排序
TreeSet<String> set2 = new TreeSet<>();
set2.add("b");
set2.add("d");
set2.add("a");
set2.add("c");
set2.add("ad");
set2.add("ab");
set2.add("ac");
for (String element : set2) {
System.out.println(element);
}
}
}
1.3 研究内置比较器的使用
public class Test03 {
/**
* 研究内置比较器的使用-- Comparable
*
* 需求,创建学生类,将学生类的对象添加到TreeSet中
*/
public static void main(String[] args) {
TreeSet<Student> set = new TreeSet<>();
//当年龄相同的时候就不会存储到集合当中
set.add(new Student("张三", '男', 27, "2401", "001"));
set.add(new Student("李四", '女', 23, "2401", "002"));
set.add(new Student("王五", '女', 23, "2401", "002"));
for (Student stu : set) {
System.out.println(stu);
}
}
}
Student类
//需要添加泛型
//就是和谁比较
public class Student implements Comparable<Student>{
private String name;
private char sex;
private int age;
private String classId;
private String id;
public Student() {
}
public Student(String classId, String id) {
this.classId = classId;
this.id = id;
}
public Student(String name, char sex, int age, String classId, String id) {
this.name = name;
this.sex = sex;
this.age = age;
this.classId = classId;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getClassId() {
return classId;
}
public void setClassId(String classId) {
this.classId = classId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public boolean equals(Object obj) {
if(this == obj){
return true;
}
if(obj instanceof Student){
Student stu = (Student) obj;
if(this.classId.equals(stu.classId) && this.id.equals(stu.id)){
return true;
}
}
return false;
}
@Override
public String toString() {
return name + "\t" + sex + "\t" + age + "\t" + classId + "\t" + id;
}
@Override
public int compareTo(Student o) {
//this表示当前对象
//o表示要比较的对象
return this.age - o.age;
}
}
1.4 外置比较器的使用
public class Test04 {
/**
* ֪研究外置比较器的使用 -- Comparator
*
* 需求:将学生类的对象添加到TreeSet中
* 场景:-联合开发
* 1.原来的类中比较规则满足不了当前需求
*
* 比较器的优先级别:外置比较器 > 内置比较器
*/
public static void main(String[] args) {
//匿名内部类的使用场景!!!
TreeSet<Student> set = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if(o1.equals(o2)){
return 0;
}
int nameLen1 = o1.getName().length();
int nameLen2 = o2.getName().length();
if(nameLen1 != nameLen2){
//return nameLen1 - nameLen2;
return Integer.compare(nameLen1, nameLen2);
}
int age1 = o1.getAge();
int age2 = o2.getAge();
if(age1 != age2){
return Integer.compare(age1, age2);
}
return 1;
}
});
set.add(new Student("张三", '男', 27, "2401", "001"));
set.add(new Student("李四", '女', 23, "2401", "002"));
for (Student stu : set) {
System.out.println(stu);
}
}
}
2 HashMap
2.1 HashMap集合的使用
public class Test01 {
/**
* 知识点:HashMap的使用
*/
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
//添加元素
map.put("费的提克", 27);
map.put("厄加特", 23);
map.put("索尔", 28);
map.put("费的提克", 25);
//将newmap中所有元素添加到新集合中
HashMap<String, Integer> newMap = new HashMap<>();
newMap.put("aaa", 10);
newMap.put("bbb", 20);
newMap.put("ccc", 30);
newMap.put("ddd", 40);
map.putAll(newMap);
//如果键存在就获取value值
//如果不存在就添加
Integer putIfAbsent = map.putIfAbsent("费的提克", 28);
System.out.println("putIfAbsent" + putIfAbsent);
//通过键获取到对应的值
Integer integer1 = map.get("费的提克");
System.out.println("ͨ费的提克的值为" + integer1);//28
//ͨ通过key获取对应的value,如果key不存在则返回默认值
Integer integer2 = map.getOrDefault("艾瑞利亚", 888);
System.out.println("判断集合中艾瑞利亚的值为" + integer2);//888
//清空集合中的所有元素
//map.clear();
System.out.println("集合中是否包含厄加特" + map.containsKey("厄加特"));//true
System.out.println("集合中是否有23" + map.containsValue(23));//true
System.out.println("集合是否为空" + map.isEmpty());//false
//ͨ通过键删除指定映射关系:key + value
map.remove("aaa");
//通过key删除指定映射关系:key + value
map.remove("bbb", 20);
//ͨ通过key替换value
map.replace("费的提克", 30);
//ͨ通过key + value替换
map.replace("厄加特", 23, 25);
//获取映射关系的个数,包含了key + value
int size = map.size();
System.out.println("获取映射关系的个树" + size);//10
//获取map中所有的接口
Collection<Integer> values = map.values();
System.out.println(Arrays.toString(values.toArray()));//将集合转换成数据,再将数组转换成字符串
System.out.println("-----------------------");
//遍历 -- keySet()
//思路:获取map集合中所有的key放在一个set集合中,遍历Set集合获取key
Set<String> keySet = map.keySet();
for (String key : keySet) {
Integer value = map.get(key);
System.out.println(key + " -- " + value);
}
System.out.println("-----------------------");
//键值对对象 -- entrySet()
Set<Entry<String,Integer>> entrySet = map.entrySet();
for (Entry<String, Integer> entry : entrySet) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + " -- " + value);
}
}
}
2.2 HashMap的注意事项
public class Test02 {
/**
* hash的注意事项
*
*
*/
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
//put第一次添加数据,返回null
Integer put1 = map.put("费的提克", 27);
Integer put2 = map.put("厄加特", 23);
Integer put3 = map.put("斯维因", 28);
System.out.println("put1:" + put1);//null
System.out.println("put2:" + put2);//null
System.out.println("put3:" + put3);//null
//如果使用put添加数据,如果map中有key
Integer put4 = map.put("费的提克", 29);
System.out.println("put4:" + put4);//28
//键值对对象 -- entrySet()
//(Key+Value)
Set<Entry<String,Integer>> entrySet = map.entrySet();
for (Entry<String, Integer> entry : entrySet) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + " -- " + value);
}
}
}
2.3 针对HashMap的value排序
public class Test03 {
/**
* 针对与HashMap的Value排序
*/
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("费的提克", 27);
map.put("厄加特", 23);
map.put("斯维因", 28);
//将map的映射关系取出来,返回set集合中
Set<Entry<String,Integer>> entrySet = map.entrySet();
//利用ArrayList的sort方法进行排序
ArrayList<Entry<String,Integer>> list = new ArrayList<>(entrySet);
//利用ArrayList进行排序
list.sort((o1, o2) -> {
Integer v1 = o1.getValue();
Integer v2 = o2.getValue();
return Integer.compare(v1, v2);
});
//遍历
for (Entry<String, Integer> entry : list) {
System.out.println(entry);
}
}
}
2.4 Hash的特点
public class Test04 {
/**
* Hash的特点
*
*注意:
* 1.HashMap的Key不允许重复,Key的唯一的
* 2.HashMap的value允许重复
*
* Hash的特点:无序 + Key去重
*/
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("费的提克", 27);
map.put("斯维因", 23);
map.put("厄加特", 23);
map.put("厄加特", 28);
map.put("厄加特", 29);
Set<Entry<String,Integer>> entrySet = map.entrySet();
for (Entry<String, Integer> entry : entrySet) {
System.out.println(entry);
}
}
}