当前位置:网站首页>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
answer
边栏推荐
猜你喜欢
Tutorial on principles and applications of database system (007) -- related concepts of database
解密GD32 MCU产品家族,开发板该怎么选?
Idea 2021 Chinese garbled code
The IDM server response shows that you do not have permission to download the solution tutorial
JS to convert array to tree data
leetcode刷题:二叉树23(二叉搜索树中的众数)
BGP third experiment report
Connect to blog method, overload, recursion
[statistical learning method] learning notes - logistic regression and maximum entropy model
Airserver automatically receives multi screen projection or cross device projection
随机推荐
Customize the web service configuration file
数据库安全的重要性
MPLS experiment
数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题
Niuke website
【统计学习方法】学习笔记——支持向量机(下)
编译 libssl 报错
leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
SQL Lab (46~53) (continuous update later) order by injection
通讯协议设计与实现
[pytorch practice] image description -- let neural network read pictures and tell stories
About web content security policy directive some test cases specified through meta elements
【统计学习方法】学习笔记——第四章:朴素贝叶斯法
[statistical learning method] learning notes - logistic regression and maximum entropy model
ENSP MPLS layer 3 dedicated line
gcc 编译报错
leetcode刷题:二叉树21(验证二叉搜索树)
ES底层原理之倒排索引
TypeScript 接口继承