Day 17 Map java-based and generic

Here to thank the teacher Lu

FIG appreciated that a simple HashMap (top)

  1. Map: Map
    1. Concept: Map: is used to hold the key to the collection container
    2. The role:
    to solve the structure of this relationship needs to appear in pairs
    key (key): Quality is a data
    value (value): nature also a data
    key-value pair: is a pair (and the two are different), a pair of generally have a relationship
    such as:
    1, such as QQ account number + password: imagine the scene - you need to collect a set of 100 people QQ number and password;
    2, bank accounts and passwords
    3, the game account and password
    4, you collect names + name (the couple's relationship) girlfriend of
    5, coordinates (the X-, the y-)
    6, ...

     3. Map的继承体系 是一个独立的体系,跟Collection一毛关系都没有
      Map   接口
      HashMap   :基于hashCode(),底层是Entry键值对的数组,数组中装装的是       键值对,
               会根据添加的key的hashCode值 模 当前数组的length(默认是16),   
              (掌握)	而得到数组的下标,进而保存键值对。如果key模length后,得到的下标是相等的,就会发生hash碰撞(哈希冲突),这时候再继续判断key的equals方法,
              如果equals判断为true,就会将原来的键值对覆盖,
     	      如果是false,就会在该下标处的原来键值对后面形成  链表,如果链表长度达到8个,就形成	红黑树。
     	  	  当删除键值对后剩余的数量小于等于6的时候,就重新将红黑树,退化为链表。
     	  					(jdk1.8)put添加/remove删除
     	  		
     	  	key判断键是否重复和HashSet判断重复一样:1. hashCode  2.equals()
     	    HashMap底层是Entry(键值对)的数组,并且维护了链表和红黑树
     	    HashTable   : 就是一个线程安全的HashMap,不能存放null键和null值
     	  				Properties:特殊的HashTable(是HashTable子类),只不过key-value都是String类型,一般用来做
     			  		配置文件:(解决硬编码问题的文件)
     			  			1. .xml
     			  			2. .properties
     			  			3. .ini/.init
     			  			4. .yml (SprintBoot)
     			  			
     	    ConcurrentHashMap   : 就是一个线程安全且效率更高的的HashTable,理论上是HashTable的16倍
     	    LinkedHashMap: 就是一个内部维护了一个链表的HashMap,可以保证存入和取出顺序
     	    TreeMap    : 判断key重复的规则和TreeSet一样
     		TreeMap中的键(key)要么是具有自然排序能力(需要key实现Comparable接口,重写CompareTo方法);
     		或者TreeMap中提供一个比较器Comparetor实现类对象
     		如果key是自然或者定制排序,返回值是0表示,key是重复的,如果是正数,则按照key的升序排序,否则是降序
    
  2. HashMap underlying principle also source folder of images
    Here Insert Picture Description
    5. traverse three ways of HashMap :( master)

     	1. Set<Map.Entry<K,V>> entrySet() 返回所有键值对的Set集合。  
     	
     	2. Set<K> keySet()   返回所有键的Set集合。  
     	
     	3. Collection<V> values()   返回所有值的Collection集合
     	
     	步骤:
     		1. 根据需求调用上面某一个方法,得到(键值对Entry或者键key)Set或者Collection(value)集合
     		2. 遍历第一步中的Set或者Collection集合
     			1. foreach
     			2. 单向迭代器
     			
     	遍历选择:
     		如果是既需要key也需要value,最好用entrySet,其他场景根据情况
     	
        Map使用选择:(掌握)
     	1、Map中,HashMap具有超高的访问速度,如果我们只是在Map 中插入、删除和定位元素,
     		而无关线程安全或者同步问题,HashMap 是最好的选择。
     	2、如果考虑线程安全或者写入速度的话,可以使用HashTable,JDK8后建议使用ConcurrentHashMap替代HashTable,既能获取超高的访问速度,又能保证线程安全
     	3、如果想按怎么存的顺序怎么取,比如队列形式,排队,那么使用LinkedHashMap
     	4、如果需要让Map按照key进行升序或者降序排序,那就用TreeMap 	
    
  3. Properties categories: often used for the configuration file (master)
    Properties files do not support Chinese, so, in written documents to the Chinese see the hexadecimal format
    file comments with the #, you can display Chinese content through plug-ins comment

     1. 概念:就是一个特殊的HashTable,key和value都是String类型,常用来做配置文件
     2. 作用:
     		解决了Java编程中的硬编码问题。
    
     3. 使用:一定要使用Properties自己的方法,才能使key和value都是String类型
     	3.1. 将内存中的数据,写出到硬盘
     		void list(PrintStream out) 将此属性列表打印到指定的输出流。  
     		void list(PrintWriter out) 将此属性列表打印到指定的输出流。  
     		
     		存放值方法: setProperty(String key,String value);
    
     	3.2. 将硬盘中的数据,读取到内存
     		void load(InputStream inStream) 从输入字节流读取属性列表(键和元素对)。  
     		void load(Reader reader) 以简单的线性格式从输入字符流读取属性列表(关键字和元素对)。 
     		
     		取值方法: 
     			1. getProperty(String key);
     			2. getProperty(String key,String defaultValue);
     	
        4. 注意:
     	    1. 今天涉及到IO的知识,只管用,不用管其他的
     	    2. key和value不要写中文
     	    3. 不支持中文,文件中注释用#,可以通过插件显示中文注释内容
    
  4. Generic:
    1. concept: it is a placeholder in the design category, occupied the position
    is in the design category (interfaces, etc.), there is no provision to specifically what types of parameters,
    in the instance or when calling reinjecting specific type tells the compiler what type it is.

     2. 作用:
     		1. 帮助程序员校验代码(用泛型约束容器中的元素类型),限制类型的使用
     		2. 避免强制转换
     		3. 提高程序的扩展性
    
     3. 使用:
     		1. 泛型类
     			语法:直接在类名后加上<数据类型>,一般情况下数据类型用字母代替
    
     		2. 泛型方法
     			语法:在修饰符后,返回值前加上<数据类型>
     		
     		3. 泛型上限和下限
     				T super Number: 表示T最小类型取值到Number类型
     				T extends Number: 表示T最大类型取值到Number类型
     				<? super T> 表示通配符,可以匹配任意类型
    
     		4. 泛型嵌套
     			Set<Map.Entry<K,V>> entrySet() 返回所有键值对的Set集合。
     				
     4. 注意事项:
     		1.泛型数据类型只能是引用类型
     		2. = 两边<>中的数据必须完全一致,包括顺序
    
     		类型擦除(自己查)PECS原则
    
     5. 常见字母含义:
     		E element 元素,一般用在集合等容器类中
     		T type	类型
     		K key	键
     		V value	值
     		R result 返回值
    
Published 14 original articles · won praise 1 · views 248

Guess you like

Origin blog.csdn.net/weixin_45061669/article/details/104784092