当前位置:网站首页>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 
边栏推荐
- BGP third experiment report
- idm服务器响应显示您没有权限下载解决教程
- What is an esp/msr partition and how to create an esp/msr partition
- Static comprehensive experiment
- sql-lab (54-65)
- leetcode刷题:二叉树21(验证二叉搜索树)
- H3C HCl MPLS layer 2 dedicated line experiment
- 爱可可AI前沿推介(7.7)
- 对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
- leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
猜你喜欢

SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)

OSPF exercise Report

Idea 2021 Chinese garbled code

Processing strategy of message queue message loss and repeated message sending

【深度学习】图像多标签分类任务,百度PaddleClas

Solutions to cross domain problems

The left-hand side of an assignment expression may not be an optional property access. ts(2779)

Customize the web service configuration file

ES底层原理之倒排索引

"Series after reading" my God! It's so simple to understand throttling and anti shake~
随机推荐
牛客网刷题网址
Realize all, race, allsettled and any of the simple version of promise by yourself
leetcode刷题:二叉树21(验证二叉搜索树)
Cryptography series: detailed explanation of online certificate status protocol OCSP
The left-hand side of an assignment expression may not be an optional property access. ts(2779)
数据库安全的重要性
Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
通讯协议设计与实现
Tutorial on the principle and application of database system (011) -- relational database
About IPSec
【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移
2022-07-07日报:GAN发明者Ian Goodfellow正式加入DeepMind
Using stack to convert binary to decimal
Tutorial on principles and applications of database system (010) -- exercises of conceptual model and data model
【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
Sorting, dichotomy
广州市召开安全生产工作会议
Common knowledge of one-dimensional array and two-dimensional array
数据库系统原理与应用教程(008)—— 数据库相关概念练习题
Vxlan 静态集中网关