金华网站制作费用,佛山网络推广电话,做seo的网站,百度推广首页大家都知道。在Map和Set不可存在反复元素#xff1f; 可是对于内部的细节我们并不了解。今天我们就一块来 探讨一下#xff01; 1 对于 HashMap HashSet 他们的底层数据结构的实现是#xff1a;维护了一张 HashTable 。容器中的元素所有存储在Hashtable 中。他们再加入… 大家都知道。在Map和Set不可存在反复元素 可是对于内部的细节我们并不了解。今天我们就一块来 探讨一下 1 对于 HashMap HashSet 他们的底层数据结构的实现是维护了一张 HashTable 。容器中的元素所有存储在Hashtable 中。他们再加入元素的时候,是怎样推断是否存在有反复元素的呢 每个被加入的元素都有一个 hashCode(哈希值)他们先比較哈希值是否同样 不同样的元素加入进入 HashTable. 假设hashCode同样的话 再去比較 equals()方法假设也同样的话JVM就觉得数据已经存在了。就不会加入数据 如图1 2 对于 TreeMap TreeSet 他们底层是数据结构的实现是维护了一棵二叉树。 容器中加入元素的时候他们有是怎么推断是否有同样元素的我们都直到 TreeMap TreeSet 她们 都是 有序的存储数据。 为了维护 数据的唯一性。 再存入数据的时候他们会调用元素中 实现的 Comparable 的 compareTo() 方法代码1。 或者 集合本身创建的时候 传入了 迭代器代码2. 详细的实现是调用比較方法返回-1 的时候加入到左子树返回1 的时候 加入到 右子树。返回0 有同样数据 不加入该元素 如图2 代码1原理一 package stu.love.v;import java.util.*;
//什么时候用Map
/*
当存在映射关系时。
每一个学员都相应一个地址
姓名年龄同样的视为同一个人*/
// 容器中的对象 本身 具备比較性。class StudentD implements ComparableStudentD
{private String name;private int age;public StudentD(String name,int age){this.name name;this.age age;}public int compareTo(StudentD stu){int t this.age-stu.age;return t0?this.name.compareTo(stu.name):t; } // 重写了 hashCode 方法 public int hashCode() { return name.hashCode()age*36; } // 重写了 equals 方法 public boolean equals(Object obj) { if(!(obj instanceof StudentD)) throw new ClassCastException(类型异常); StudentD stu (StudentD)obj; return this.name.equals(stu.name) this.age stu.age; } public void setName(String name) { this.name name; } public void setAge(int age) { this.age age; } public String getName() { return this.name; } public int getAge() { return this.age; } public String toString() { return this.name ,age; } } class Demo16 { public static void main(String[] args) { //保证键唯一的原理先推断哈希值是否同样同样再推断equals() HashMapStudentD,String hm new HashMapStudentD,String(); hm.put(new StudentD(xiaobai,23),shanghai); hm.put(new StudentD(wanghei,20),beijing); hm.put(new StudentD(lisi,28),shenzhen); hm.put(new StudentD(lisi,28),shenzhen); // Map 第一种 迭代方式 依据 key 找 value SetStudentD sethm.keySet(); for(IteratorStudentD ite set.iterator();ite.hasNext();) { StudentD stu ite.next(); String value hm.get(stu); sop(stu的地址是:value); } // map 的 另外一种 迭代方式 获取 键值对。entry 获取当中的 key 和 value SetMap.EntryStudentD,String entry hm.entrySet(); for(IteratorMap.EntryStudentD,String ite entry.iterator();ite.hasNext();) { Map.EntryStudentD,String kv ite.next(); StudentD key kv.getKey(); String value kv.getValue(); sop(key的地址是:value); } } public static void sop(Object obj) { System.out.println(obj); } } 代码2 package stu.love.v;/*
TreeMap:
HashMap保证键唯一的原理和HashSet同样
TreeMap保证键唯一的原理和TreeSet同样*/
import java.util.*;class Student1
{private String name;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;}private int age;public Student1(String name,int age){this.name name;this.age age;}public String toString(){return name,age;}}// 比較器
class CompareByName implements ComparatorStudent1
{public int compare(Student1 s1,Student1 s2){
// 这样写的方法 很好 简洁 int t s1.getName().compareTo(s2.getName()); return t 0?s1.getAge()-s2.getAge():t; } } class Demo17 { public static void main(String[] args) { // 原理二 //保证键唯一的原理比較方法的返回值为0 TreeMapStudent1,String tm new TreeMapStudent1,String(new CompareByName()); tm.put(new Student1(xiaobai,23),shanghai); tm.put(new Student1(wanghei,20),beijing); tm.put(new Student1(lisi,28),shenzhen); tm.put(new Student1(lisi,28),shenzhen); SetMap.EntryStudent1,String entry tm.entrySet(); for(IteratorMap.EntryStudent1,String it entry.iterator();it.hasNext();) { Map.EntryStudent1,String kv it.next(); Student1 key kv.getKey(); String value kv.getValue(); sop(key的地址是:value); } } public static void sop(Object obj) { System.out.println(obj); } }