当前位置:网站首页>map and set - natural search and lookup semantics
map and set - natural search and lookup semantics
2022-08-11 04:27:00 【mmmenxj】
Binary search tree BST: TreeSet/TreeMap
Hash table: HashSet/HashMap
set | Store unique key values and use set collection for deduplication |
map | The key = value key-value pair is stored, and the corresponding value is found according to the key |
One, map classification
In a hashMap, the insertion order of elements is independent of the storage order.
If you need the same insertion order and save order, use LinkedMap.
1. In the hashMap, if the key is the same, repeat the assignment, and the last time is equivalent to the update operation
In 2.hashMap, when the key value is different, the value can be different
3. Both the key and value of the hashMap can be empty.
The key of the Treemap cannot be empty, otherwise a null pointer exception will occur
Summary:
hashMap | TreeMap | LinkedHashMap | |
Structure | Hash table + red-black tree-based structure | Based on red-black trees | A linked list is violated on the basis of hashMap |
Does the save order relate to the element insertion order | Irrelevant | Irrelevant | Related, in insertion order |
Can key and value be null? | Anything | key is not allowed, value is allowed | Anything |
When used | Element must be subclass of Comparable |
Second, set collection: deduplication
Iterate over the set collection and use the for-each loop directly
As long as it is a subclass of the Iterble interface, it can be traversed directly using the for-each loop.
When traversing the map collection, you need to convert the map to a set for for-each traversal.
for(Map.entry
The difference between 1.== and equals:
==Compare the two values for equality
If the comparison is between two basic types, it is whether they hold the same value.
If two reference types are being compared, the value stored by the reference type is just an address.
Theequals method is used to compare whether two objects want to be equal. At this time, the conditions for equality need to be defined by our users.
2. What is the difference between comparable and compator?
When a class implements the comparable interface, it means that the class has the ability to be compared, and the class itself has the ability to be compared.
compator is a method that we write to compare when a class cannot be compared, for example:
JDK's priority queue, the class passed in by PriorityQueue itself implements the comparable interface ->Freq or passes in a comparator,
PriorityQueue
PriorityQueue
See only one output, generally use set
What is the difference between set and list?set cannot contain duplicate elements
Binary search tree: (generally, the value is not considered equal, the elements are not repeated)
1. is a binary tree (each node has at most two nodes)
2. For the node values of the nodes in this tree, all node values of the left subtree Judgment binary search tree: In order traversal of the tree, you can get an ascending set. The process of finding elements of a binary search tree is actually a binary search (logN) -> binary search tree BST Seeing that logN associates with "tree". The newly added elements in the BST must be added to the leaf nodes.public void add(int val){root = add(root,val);}public TreeNode add(TreeNode root, int val){//Insert a new node val into the BST rooted at root//create new nodeTreeNode newNode = new TreeNode(val);if(root == null){//The current value is the root node to be insertedsize++;return newNode;}if(val < root.val){// insert in the left treeroot.left = add(root.left ,val);}else{//At this time, it is larger than the root noderoot.right = add(root.right,val);}return root;}
边栏推荐
猜你喜欢
Self-research capability was recognized again, and Tencent Cloud Database was included in the Forrester Translytical report
Read the article, high-performance and predictable data center network
Jetson Orin平台4-16路 GMSL2/GSML1相机采集套件推荐
Provincial level of Echart maps, as well as all prefecture-level download and use
es-head plugin insert query and conditional query (5)
【人话版】WEB3将至之“权益的游戏”
机器学习是什么?详解机器学习概念
How to learn machine learning?machine learning process
Differences and connections between distributed and clustered
Interchangeable Measurement Techniques - Geometric Errors
随机推荐
The priority queue
《卫星界》刊评“星辰大海”计划:孙宇晨为太空旅游带来新的机遇
【FPGA】SDRAM
Map中的getOrDefualt方法
es-head plugin insert query and conditional query (5)
1815. Get the maximum number of groups of fresh donuts state compression
校园兼职平台项目反思
【组成原理 九 CPU】
.NET自定义中间件
2022新员工公司级安全教育基础培训(118页)
使用jackson解析json数据详讲
map和set--天然的搜索和查找语义
二叉堆的基础~
Add PRODUCT_BOOT_JARS and classes to provide jar packages to applications
[FPGA] Design Ideas - I2C Protocol
The FTP error code list
洛谷P4847 银河英雄传说V2
jwsManager服务接口实现类-jni实现
洛谷P4324 扭动的回文串
Callable实现多线程