当前位置:网站首页>Day-18 hash table, generic

Day-18 hash table, generic

2022-07-07 12:36:00 Xiaobai shelter

1.Set

1.1 HashSet Use
Common method operation

        set.add(1);
    	 set.add("asdha");
    	 set.remove("asdha");
    	 System.out.println(set.size());
    	 set.isEmpty();
    	 for(Object object:set){
    
    		 
    	 } Insert the code chip here 

2. Hash table
2.1 summary
The hash table structure can be understood as the first node of the linked list stored in the array , For preservation k and v Key value pair operation
hash Algorithm : Is a secure encryption mechanism , You can convert variable length data into fixed length data , And there is no guarantee of its uniqueness , Also known as hash conflict
stay java The middle point is hashCode Method
Generate multiple times for an object hash value , The value must be the same , Multiple objects can also generate the same hash value , It's called hash conflict

k Do not repeat ,v Can be repeated
Add process :
1. According to the... To be added key, Call it the hashCode Method , Generate hash value
2. Through a certain algorithm , according to hash Value generates the subscript of the array
3. Judge whether the subscript is , Is there any data , If there is no data, save the key value pair mapping relationship to the array
4. If the subscript is in , There's data , Call key Of equals Method , Compare with all the corresponding data , If it's not equal , Then add it to the tail of the linked list
5. If the corresponding linked list , adopt equals When comparing methods , Found the same data , that key No more , however value Value will replace the original value value ,
Through the addition process, we know , Will automatically call the hashCode and equals, So when saving custom types , Attention should be paid to method overrides
stay 1.8 There is a new change in , In order to improve the query efficiency , The optimization of red black tree and capacity expansion is introduced , Because the linked list has poor query performance , So if the number of linked lists in each array is greater than or equal to 7, The linked list will be converted into a red black tree , The default initialization capacity of the array is 16

stay java in There is no concept of hash table , Package the scattered list into HashMao and HashTable

2.2HashSet
When we use HashSet When , In fact, it is equal to using again HashMap When adding data , Even though it's called HashSet Of add Method , But the essence is to call map Of put Method
Ps : stay map in ,put Is an add operation , and map in What needs to be saved is k and v The mapping relationship , So in set There is a variable in that holds value Value
So let's go on set When adding , It's just the operation map Medium key,value We no longer care about

3 Map
3.1 summary
Map Is chaotic , And save K-V Key value pair mapping , among K Can't repeat ,V repeatable
HashMap: At the bottom is a hash table
TreeMap: At the bottom are red and black trees , Elements must be sorted according to certain rules
The mapping relationship : Such as goods and purchase quantity , Or statistics
“ajdajsodadkada;dlka’” Count the number of each character , Use HashMap For storage , Character do K, How many times do V

3.2HashMap

Common operations

// establish map
    	HashMap map=new HashMap();
    	// add to K-V
    	map.put("A", "one");
    	map.put("B", "two");
    	map.put("C", "three");
    	map.put(65, 100);
    	map.put("A", "2222");
    	//key repeat , Don't add ,value Replace 
    	map.put("A","222");
    	// Support K and V all null, But it doesn't make sense 
    	map.put(null, null);
    	// Number 
    	System.out.println(map.size());
    	//get: according to K obtain V Value 
    	System.out.println(map.get("A"));
    	// To determine whether or not to include a key
    	System.out.println(map.containsKey(65));
    	// To determine whether or not to include a value
    	System.out.println(map.containsValue("one"));
    	// according to key Delete the mapping relationship , Return the corresponding value value 
    	map.remove(65);
    	// Get all value, And put it in the collection to return 
    	Collection values=map.values();
    	for(Object object:values){
    
    		System.out.println(object);
    	}
    	System.out.println("=======");
    	//keySet: Get all key, Package to set Object and return 
    	Set keys=map.keySet();
    	for(Object object:keys){
    
    		System.out.println(object+":"+map.get(object));
    	}
    	// hold map Convert to set
    	//Entry Class , Save the K and V Two variables , hold map Medium k and v Convert to entry Class 
    	// So we just need to save entry object , It's like saving k and v
    	Set set=map.entrySet();
    	for(Object object:set){
    
    		//C=three
    		System.out.println(object);
    		// Convert to entry type 
    		Entry entry=(Entry) object;
    		// obtain K and V
    		System.out.println(entry.getKey()+":"+entry.getValue());
    	}
    }

3.3 TreeMap
characteristic :TreeMap The saved elements will be sorted according to certain rules , At the bottom are red and black trees

4. Generic
4.1 summary
Generic : During the compilation process , Check whether the data type matches
Generic : Must be a reference type , Basic types cannot be used
advantage : Unified data types , Reduced type conversions
shortcoming : Only one type can be saved
4.2 Usage mode

//  Generics are not used 
    		List list = new ArrayList();
    		//  Anything can be put 
    		list.add(new A());
    		list.add(1);
    		//  When I pick it up , Get is Object
    		for (Object object : list) {
    
    			//  Use requires judgment and downward transformation 
    			if (object instanceof A) {
    
    				A a = (A) object;
    				a.m1();
    			}
    		}
    		
    		//  Use generics   Regulations   In this collection   Save only A type 
    		List<A> list2 = new ArrayList<A>();
    		list2.add(new A());
    		//  Add other   Report errors , And it is a compilation error 
    		// list2.add(2);
    		
    		//  When you use it , What you get is A type , There will be no type conversion exception 
    		//  Because as long as it can run , There must be A
    		for (A a : list2) {
    
    			a.m1();
    		}
    		
    		//  Generic   Cannot write basic type , Only reference types can be written 
    		//  If you want to save basic types , You need to write the corresponding packing class type 
    		// List<int> list3 = new ArrayList<int>();
    		List<Integer> list4 = new ArrayList<Integer>();
    		
    		//  If   Need to save a variety of data , You don't need to use generics 
    		//  But if you need to save the same type of data , Be sure to use generics , It's easy to operate 
    		
    		// set and map Use generics 
    		Set<String> set = new HashSet<String>();
    		Map<String, Integer> map = new HashMap<String, Integer>();
    	}
    }
    class A {
    
    	public void m1(){
    
    		System.out.println("m1 Yes ");
    	}

4.3 Custom generics
Define generics , Using Capital Letters A-Z Express , Everything is the same , It's just placeholders , But some characters also have some special meanings
E:element Generally speaking, it represents elements , And the data in the collection We call it element , So generics that appear in collections generally use E Express
K:key The key
V:value Indicated value K and V It usually appears in the hash table (Map)
T:Type It means a java type
N: Express Number
?: Indicates the type of uncertainty
* If generics are specified , But the generic type is not specified where it is used , The default is Object type
5. Interview questions
subject
 Insert picture description here
answer
 Insert picture description here

原网站

版权声明
本文为[Xiaobai shelter]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130617040472.html