当前位置:网站首页>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
边栏推荐
- [deep learning] image multi label classification task, Baidu paddleclas
- leetcode刷题:二叉树27(删除二叉搜索树中的节点)
- Tutorial on the principle and application of database system (011) -- relational database
- Minimalist movie website
- SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
- Inverted index of ES underlying principle
- Using stack to convert binary to decimal
- About web content security policy directive some test cases specified through meta elements
- 解密GD32 MCU产品家族,开发板该怎么选?
- 【PyTorch实战】用RNN写诗
猜你喜欢
[statistical learning methods] learning notes - Chapter 5: Decision Tree
[pytorch practice] image description -- let neural network read pictures and tell stories
【PyTorch实战】用RNN写诗
Several methods of checking JS to judge empty objects
[statistical learning method] learning notes - logistic regression and maximum entropy model
什么是ESP/MSR 分区,如何建立ESP/MSR 分区
OSPF exercise Report
静态Vxlan 配置
ps链接图层的使用方法和快捷键,ps图层链接怎么做的
ENSP MPLS layer 3 dedicated line
随机推荐
30. Feed shot named entity recognition with self describing networks reading notes
【从 0 开始学微服务】【03】初探微服务架构
SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
解密GD32 MCU产品家族,开发板该怎么选?
普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备
The left-hand side of an assignment expression may not be an optional property access. ts(2779)
Solutions to cross domain problems
Static comprehensive experiment
Sort out the garbage collection of JVM, and don't involve high-quality things such as performance tuning for the time being
idm服务器响应显示您没有权限下载解决教程
gcc 编译报错
利用棧來實現二進制轉化為十進制
The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
Static vxlan configuration
Utiliser la pile pour convertir le binaire en décimal
ps链接图层的使用方法和快捷键,ps图层链接怎么做的
Connect to blog method, overload, recursion
GCC compilation error
leetcode刷题:二叉树27(删除二叉搜索树中的节点)