0
点赞
收藏
分享

微信扫一扫

63 java集合和泛型_13 _Map实现类 _TreeMap


文章目录

  • ​​Map集合的实现类​​
  • ​​TreeMap(存储结构:红黑树)​​
  • ​​TreeSet 与 TreeMap 的关系​​

Map集合的实现类

  1. HashMap 【重点】 :
  • JDK1.2版本,线程不安全,运行效率快;允许用null作为key或是value。
  1. Hashtable:
  • JDK1.0版本,线程安全,运行效率慢;不允许nul 1作为key或是value。
  1. Properties :(IO流再学)
  • Hashtable的子类,要求key和value都是String。通常用于配置文件的读取。
  1. TreeMap:
  • 实现了SortedMap接口(是Map的子接口),可以对key自动排序

TreeMap(存储结构:红黑树)

  1. 有序(自己实现排序),无下标,键值对存储,键不可重复,值可以重复
  2. 实现排序:
  • 第一种:在key对应的元素类型中,必须实现Comparable接口,重写CompareTo方法,指定排序规则
  • 第二种:在创建TreeMap集合时,用匿名内部类实现Comparator 接口,重写compare方法

代码:

package com.wlw.collection.map;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

/**
* TreeMap 的使用
* 可以对key自动排序
* 存储结构:红黑树
* 也支持 Comparator:实现定制比较(比较器)
*/
public class TreeMap_Demo {
public static void main(String[] args) {
//新建集合,(定制比较器)
TreeMap<Student, String> treeMap = new TreeMap<>(
/*
new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int n1 = o1.getStuNo() - o2.getStuNo();
int n2 = o1.getName().compareTo(o2.getName());
return n1 == 0 ? n2 : n1;
}
}
*/
);

Student s1 = new Student("唐三藏",110);
Student s2 = new Student("孙悟空",111);
Student s3 = new Student("猪八戒",112);
Student s4 = new Student("沙和尚",113);

///1.添加
treeMap.put(s1,"东土大唐");
treeMap.put(s2,"花果山");
treeMap.put(s3,"高老庄");
treeMap.put(s4,"流沙河");
treeMap.put(new Student("沙和尚",113),"天庭"); //不会添加,但会更改value,因为我们重写了比较CompareTo()方法,经判断key已经重复了
System.out.println("元素个数:"+treeMap.size());
System.out.println(treeMap.toString());
/*
*和TreeSet一样,因为存储的元素类型没有实现Comparable接口,去指定排序规则,集合无法判断如何去排序
*第一次运行报类型转换异常:无法比较,这是因为我们存储的元素类型 没有实现Comparable接口,指定排序规则。
* Exception in thread "main" java.lang.ClassCastException: com.wlw.collection.map.Student cannot be cast to java.lang.Comparable
* 重写之后(定义比较规制),就可以正常执行
*/

//2.删除
/*
treeMap.remove(s3);
System.out.println("删除之后,元素个数"+treeMap.size());
System.out.println(treeMap.toString());
*/

//3.遍历
//3.1使用 keySet() 返回一个只包含键的set集合
System.out.println("-------------3.1使用 keySet()----------------");
for (Student key : treeMap.keySet()) {
System.out.println(key.toString()+"--->"+treeMap.get(key));
}

//3.2使用 entrySet() 返回一个包含entry的set集合(这个方法遍历效率更高)
System.out.println("-------------3.2使用 entrySet()----------------");
for (Map.Entry<Student,String> entry : treeMap.entrySet()) {
System.out.println(entry.getKey()+"--->"+entry.getValue());
}

//4.判断
System.out.println(treeMap.containsKey(s3)); //true
System.out.println(treeMap.containsKey(new Student("唐三藏",110))); //true ,因为我们重写了CompareTo()方法,经判断是一样的,已经存在了这个key
System.out.println(treeMap.containsValue("龙宫"));//false

}
}
/*
元素个数:4
{Student{name='唐三藏', stuNo=110}=东土大唐, Student{name='孙悟空', stuNo=111}=花果山, Student{name='猪八戒', stuNo=112}=高老庄, Student{name='沙和尚', stuNo=113}=天庭}
-------------3.1使用 keySet()----------------
Student{name='唐三藏', stuNo=110}--->东土大唐
Student{name='孙悟空', stuNo=111}--->花果山
Student{name='猪八戒', stuNo=112}--->高老庄
Student{name='沙和尚', stuNo=113}--->天庭
-------------3.2使用 entrySet()----------------
Student{name='唐三藏', stuNo=110}--->东土大唐
Student{name='孙悟空', stuNo=111}--->花果山
Student{name='猪八戒', stuNo=112}--->高老庄
Student{name='沙和尚', stuNo=113}--->天庭
true
true
false
*/

package com.wlw.collection.map;
/*
实现Comparable接口,重写CompareTo方法,指定排序规则
如果compareTo()方法返回值为0,则认为是重复元素
*/
import java.util.Objects;

public class Student implements Comparable<Student>{
private String name;
private int stuNo;

public Student() {
}

public Student(String name, int stuNo) {
this.name = name;
this.stuNo = stuNo;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getStuNo() {
return stuNo;
}

public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}

@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", stuNo=" + stuNo +
'}';
}

//重写equals
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return stuNo == student.stuNo &&
Objects.equals(name, student.name);
}

//重写hashcode
@Override
public int hashCode() {
return Objects.hash(name, stuNo);
}

//重写compareTo(比较规则)
@Override
public int compareTo(Student o) {
int n1 = this.stuNo - o.getStuNo();
int n2 = this.name.compareTo(o.getName());
return n1 == 0 ? n2 : n1;
}
}

TreeSet 与 TreeMap 的关系

TreeSet的部分源码:(TreeSet 里面用的就是TreeMap,他们两的存储结构都是红黑树)

private transient NavigableMap<E,Object> m;

TreeSet(NavigableMap<E,Object> m) {
this.m = m;
}

//无参构造
public TreeSet() {
this(new TreeMap<E,Object>());
}

//add()方法
public boolean add(E e) {
return m.put(e, PRESENT)==null;
}


举报

相关推荐

0 条评论