网站建设 数据分析wordpress固态链接
- 作者: 五速梦信息网
- 时间: 2026年04月20日 07:54
当前位置: 首页 > news >正文
网站建设 数据分析,wordpress固态链接,网站开发任务完成情况,wordpress空间清理目录 HashTable基本介绍 Hashtable和HashMap对比 Properties Properties基本介绍 应该如何选择集合 TreeSet源码分析 HashTable基本介绍 1)存放的元素是键值对#xff1a;即K-V 2)hashtable的键和值都不能为null#xff0c;否则会抛出NullPointerException 3)hashTab…目录 HashTable基本介绍 Hashtable和HashMap对比 Properties Properties基本介绍 应该如何选择集合 TreeSet源码分析 HashTable基本介绍 1)存放的元素是键值对即K-V 2)hashtable的键和值都不能为null否则会抛出NullPointerException 3)hashTable 使用方法基本上和HashMap一样 4)hashTable 是线程安全的(synchronized)hashMap是线程不安全的 5)简单看下底层结构 代码演示 注意事项 1.不能加入null 无论是key 还是 value 否则会报空指针异常 package idea.chapter14.map;import java.util.Hashtable;SuppressWarnings({all}) public class HashTableExercise {public static void main(String[] args) {Hashtable table new Hashtable();//oktable.put(john, 100); //ok//HashTable的键和值都不能为null否则会报空指针异常//不能加入null 无论是key 还是 value 否则会报空指针异常//table.put(null, 100); //异常 NullPointerException//table.put(john, null);//异常 NullPointerExceptiontable.put(lucy, 100);//oktable.put(lic, 100);//oktable.put(lic, 88);//替换table.put(hello1, 1);table.put(hello2, 1);table.put(hello3, 1);table.put(hello4, 1);table.put(hello5, 1);table.put(hello6, 1);System.out.println(table);//简单说明一下Hashtable的底层//1. 底层有数组 Hashtable$Entry[] 初始化大小为 11//2. 临界值 threshold 8 11 * 0.75//3. 扩容: 按照自己的扩容机制来进行即可.//4. 执行 方法 addEntry(hash, key, value, index); 添加K-V 封装到Entry//5. 当 if (count threshold) 满足时就进行扩容//6. 扩容机制是 把原来的大小乘2在加1 int newCapacity (oldCapacity 1) 1; 的大小扩容.} }Hashtable和HashMap对比 版本线程安全同步效率允许null键null值HashMap1.2不安全可以Hashtable1.0安全不可以 Properties Properties基本介绍 1.Properties类继承自Hashtable类并且实现了Map接口也是使用一种键值对的形式来保存数据。 2.他的使用特点和Hashtable类似 3.Properties 还可以用于 从xxx.properties文件中加载数据到Properties类对象并进行读取和修改 4.说明工作后 xxx.properties文件通常作为配置文件会在IO流的时候讲解 代码演示 package idea.chapter14.map;import java.util.Properties;SuppressWarnings({all}) public class Properties_ {public static void main(String[] args) {//1. Properties 继承了 Hashtable//2. 可以通过 k-v 存放数据当然key 和 value 不能为 nullProperties properties new Properties();//因为Properties继承了HashTable 所以Properties的key和value都不能为null否则会报空指针异常//properties.put(null, abc);//抛出 空指针异常//properties.put(abc, null); //抛出 空指针异常properties.put(john, 100);//k-vproperties.put(lucy, 100);properties.put(lic, 100);properties.put(lic, 88);//如果有相同的key value被替换System.out.println(properties properties);//通过k 获取对应值System.out.println(properties.get(lic));//88//删除properties.remove(lic);System.out.println(properties properties);//修改properties.put(john, 约翰);System.out.println(properties properties);} } 应该如何选择集合 在开发中选择什么集合实现类主要取决于业务操作特点然后根据集合实现类特性进行选择分析如下 1)先判断存储的类型(一组对象[单列]或一组键值对[双列]) 2)一组对象[单列]Collection接口 允许重复 List 增删多LinkedList [底层维护了一个双向链表] 改查多ArrayList[底层维护 Object类型的可变数组] 不允许重复Set 无序HashSet[底层是HashMap维护了一个哈希表 即数组链表红黑树)] 排序TreeSet 插入和取出顺序一致LinkedHashSet维护数组双向链表 3)一组键值对[双列]Map 键无序HashMap[底层是哈希表jdk7数组链表jdk8数组链表红黑树] 键排序TreeMap 键插入和取出顺序一致LinkedHashMap 读取文件 Properties TreeSet源码分析 代码演示 源码分析 //源码分析 /*
- 构造器把传入的比较器(comparator)对象赋给了 TreeSet的底层的 TreeMap的属性this.comparatorpublic TreeMap(Comparator? super K comparator) {this.comparator comparator;}2. 在 调用 treeSet.add(tom), 在底层会执行到Comparator? super K cpr comparator;//把我们传入的匿名对象赋给cpr因为我们传入了一个匿名内部类 所以comparator不为空 就会进入 do while循环if (cpr ! null) {//cpr 就是我们的匿名内部类(对象)do {parent t;cmp cpr.compare(key, t.key);//动态绑定到我们的匿名内部类(对象)compareif (cmp 0)t t.left;else if (cmp 0)t t.right;else //如果相等即返回0,这个Key就没有加入return t.setValue(value);} while (t ! null);}/ package idea.chapter14.set_;import java.util.Comparator; import java.util.TreeSet;/** TreeSet源码分析*/ SuppressWarnings({all}) public class TreeSet_ {public static void main(String[] args) {//1. 当我们使用无参构造器创建TreeSet时仍然是无序的//2. 希望添加的元素是按照字符串大小来排序//3. 使用TreeSet 提供的一个构造器可以传入一个比较器(匿名内部类)并指定排序规则//4. 看源码//源码分析/1. 构造器把传入的比较器(comparator)对象赋给了 TreeSet的底层的 TreeMap的属性this.comparatorpublic TreeMap(Comparator? super K comparator) {this.comparator comparator;}2. 在 调用 treeSet.add(tom), 在底层会执行到Comparator? super K cpr comparator;//把我们传入的匿名对象赋给cpr因为我们传入了一个匿名内部类 所以comparator不为空 就会进入 do while循环if (cpr ! null) {//cpr 就是我们的匿名内部类(对象)do {parent t;cmp cpr.compare(key, t.key);//动态绑定到我们的匿名内部类(对象)compareif (cmp 0)t t.left;else if (cmp 0)t t.right;else //如果相等即返回0,这个Key就没有加入return t.setValue(value);} while (t ! null);}/// TreeSet treeSet new TreeSet();//使用一个有参数的构造器传入一个比较器就可以按照我们指定的规则来进行排序TreeSet treeSet new TreeSet(new Comparator() {Overridepublic int compare(Object o1, Object o2) {//下面 调用String的 compareTo方法进行字符串大小比较//要求加入的元素按照长度大小排序//return ((String) o2).compareTo((String) o1);//底层会有do while循化依次进行比较 根据我们全入的方法而定return ((String) o1).length() - ((String) o2).length();}});//添加数据.treeSet.add(jack);treeSet.add(tom);//3treeSet.add(sp);treeSet.add(a);treeSet.add(abc);//3因为我们是按照字符串的长度进行比较因此abc不会添加成功System.out.println(treeSet treeSet);} }
- 上一篇: 网站建设 事项seo网站推广杭州
- 下一篇: 网站建设 太原韩语网站建设
