当前位置:网站首页>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
- sql-lab (54-65)
- [statistical learning method] learning notes - support vector machine (I)
- 编译 libssl 报错
- 【深度学习】图像多标签分类任务,百度PaddleClas
- On valuation model (II): PE index II - PE band
- [pytorch practice] use pytorch to realize image style migration based on neural network
- 数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题
- File upload vulnerability - upload labs (1~2)
- EPP+DIS学习之路(2)——Blink!闪烁!
猜你喜欢
leetcode刷题:二叉树20(二叉搜索树中的搜索)
[statistical learning method] learning notes - support vector machine (Part 2)
What if does not match your user account appears when submitting the code?
Tutorial on principles and applications of database system (010) -- exercises of conceptual model and data model
SQL head injection -- injection principle and essence
leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
The left-hand side of an assignment expression may not be an optional property access. ts(2779)
解决 Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
Cookie
[pytorch practice] write poetry with RNN
随机推荐
BGP actual network configuration
Cryptography series: detailed explanation of online certificate status protocol OCSP
Financial data acquisition (III) when a crawler encounters a web page that needs to scroll with the mouse wheel to refresh the data (nanny level tutorial)
leetcode刷题:二叉树23(二叉搜索树中的众数)
Common knowledge of one-dimensional array and two-dimensional array
EPP+DIS学习之路(2)——Blink!闪烁!
Tutorial on principles and applications of database system (009) -- conceptual model and data model
Realize all, race, allsettled and any of the simple version of promise by yourself
opencv的四个函数
什么是ESP/MSR 分区,如何建立ESP/MSR 分区
编译 libssl 报错
OSPF exercise Report
【统计学习方法】学习笔记——第五章:决策树
Preorder, inorder and postorder traversal of binary tree
NGUI-UILabel
广州市召开安全生产工作会议
Tutorial on the principle and application of database system (008) -- exercises on database related concepts
Routing strategy of multi-point republication [Huawei]
IPv6 experiment
30. Feed shot named entity recognition with self describing networks reading notes