当前位置:网站首页>Two column set (map set)
Two column set (map set)
2022-06-21 06:49:00 【naoguaziteng】
Objective
record
2.Map Interface and Collection Different interfaces
Two .HashMap aggregate ( Element uniqueness , disorder )
2.HashMap And Hashtable The difference between
3、 ... and .LinkedHashMap aggregate ( Element uniqueness , Orderly )
Four .TreeMap aggregate ( Element uniqueness , Ordered and sortable )
One .Map aggregate 
1. summary
Data for key value correspondence ,Java For our convenience , Gives us another set called Map Data specially storing key value correspondence .Map A set is also called a two column set .Map Interface ----->Map<K,V> Medium K Is the key , The type of key maintained by the mapping ,V yes Index value , The type of the mapping value .
2.Map Interface and Collection Different interfaces
Map Interface is a different from Conllection Interface interface , There is no connection between them . There are also big differences !
- Map It's a double column ,Collection It's a separate list
- Map The key is unique ,Collection The subsystem of Set Is the only one.
- Map The data structure of the collection is valid for keys , Independent of value ;Collection The data structure of a collection is valid for elements
3.Map Set system

4. Key correspondence
A key can only correspond to one value , Same key , It's worth overriding . And the data structure of all double column sets is only related to the key , It has nothing to do with value .
import java.util.HashMap;
public class MapTest {
public static void main(String[] args) {
HashMap<Integer, String> hm=new HashMap();
hm.put(1001," Zhang San ");
hm.put(1002," Li Si ");
hm.put(1003," Wang Wu ");
hm.put(1001," Liu Liu ");
System.out.println(hm.get(1001)); // Liu Liu
}
}5.Map Methods in sets
- put(); Add key values to the collection ( This actually has another function : Replace . If the key is stored for the first time , Just store the elements directly , return null. If the key doesn't exist for the first time , Just replace the previous value with the value , Returns the previous value )
- clear(); Remove all key value pair elements
- remove(Object key); Delete key value pair elements according to key , And return the value
- containsKey(Object key); Determines whether the set contains the specified key
- containsValue(Object value); Determines whether the set contains the specified value
- isEmpty(); Determines if the set is empty
- entrySet(); Returns the of a key value pair Set aggregate
- get(Object key); Get value from key
- keySet(); Get the set of all keys in the set , Put in Set aggregate
- values(); Get the set of all values in the set , Put in Collection aggregate
- size(); Returns the logarithm of key value pairs in a set, that is, the length of the set
6.Map Traversal of the set
- Key search (get(key))
- Get the entire key value pair object (Node)
- forEach Method
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
public class MapTest {
public static void main(String[] args) {
//map Traversal of the set
HashMap<String, String> hm = new HashMap<>();
hm.put(" I ", " Like to draw ");
hm.put(" you ", " I like playing table tennis ");
hm.put(" He ", " I like listening to music ");
hm.put(" it ", " I like to eat dried fish ");
// The way 1: Key search
Set<String> keySet = hm.keySet();
for (String key : keySet) {
String value = hm.get(key);
System.out.println(key + "===" + value);
}
// The way 2: Get all key value pairs , A collection of objects
Set<Map.Entry<String, String>> entries = hm.entrySet();
for (Map.Entry<String, String> node : entries) {
String key = node.getKey();
String value = node.getValue();
System.out.println(key + "====" + value);
}
System.out.println("====================================");
// The way 3: forEach()
hm.forEach(new BiConsumer<String, String>() {
@Override
public void accept(String key, String value) {
System.out.println(key + "====" + value);
}
});
}
}
Two .HashMap aggregate ( Element uniqueness , disorder )
1.HashMap brief introduction
HashMap The data structure is a hash table ( Array + Linked list + Red and black trees ), Element uniqueness , disorder .(HashMap Key uniqueness , It is rewritten by keys hashCode and equals() Method to guarantee , If you do not override , The uniqueness of the key cannot be guaranteed .)HashMap Collection allows inserting null key null value .
2.HashMap And Hashtable The difference between
- HashMap Can be stored null value null key ; Hashtable He key or value , It's not allowed to be null value .
- HashMap Thread unsafe , Efficient ; Hashtable Thread safety , Low efficiency .
3、 ... and .LinkedHashMap aggregate ( Element uniqueness , Orderly )
The underlying data structure is linked list and hash table , The elements are ordered and unique . The order of elements is guaranteed by the linked list data structure , Uniqueness by Hash table data structure guarantee . ( Keep in mind that : Map The data structure of a collection is only related to keys )
import java.util.LinkedHashMap;
public class LinkedHashMapTest {
public static void main(String[] args) {
LinkedHashMap<String, String> lh = new LinkedHashMap<>();
lh.put(" I ", " Like to draw ");
lh.put(" you ", " I like playing table tennis ");
lh.put(" He ", " I like listening to music ");
lh.put(" it ", " I like to eat dried fish ");
System.out.println(lh);
}
}
Four .TreeMap aggregate ( Element uniqueness , Ordered and sortable )
TreeMap The data structure of the key is a red black tree , It can ensure the sorting and uniqueness of keys . Sorting is divided into natural sorting and comparator sorting , The general default is natural sorting . Threads are not safe .
About sorting and TreeSet The assembly is the same . It's just that the collections created are different ! ( To learn about sorting, click here )
package com.xingyun.test;
import java.util.Comparator;
import java.util.TreeMap;
import java.util.function.BiConsumer;
public class TreeMapTest {
public static void main(String[] args) {
//1. Natural ordering
// Sort by age
TreeMap<Student,String> treeMap = new TreeMap<>(); // Space parameter structure
treeMap.put(new Student(" Zhang San ", 23)," love your life ");
treeMap.put(new Student(" Li Si ", 27)," love JAVA");
treeMap.put(new Student(" Lau Andy ", 21)," Love football ");
treeMap.put(new Student(" Ouyang Zhenhua ", 28)," Love painting ");
treeMap.put(new Student(" Zhang San ", 23)," Love music ");
treeMap.forEach(new BiConsumer<Student, String>() {
@Override
public void accept(Student student, String s) {
System.out.println(student.getAge()+"==="+student.getName()+"==="+s);
}
});
/*
21=== Lau Andy === Love football
23=== Zhang San === Love music
27=== Li Si === love JAVA
28=== Ouyang Zhenhua === Love painting
*/
// Comparator sort
TreeMap<Student,String> treeMap2 = new TreeMap<>(new Comparator<Student>() {
// Anonymous inner class
@Override
public int compare(Student s1, Student s2) {
// Sort by name length
int num = s1.getName().length() - s2.getName().length();
// If the length of the name is the same, it depends on the content of the name
int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
// All the same , Sort by age
int num3 = num2 == 0 ? s1.getAge() - s2.getAge() : num2;
return num3;
}
});
treeMap2.put(new Student(" Zhang San ", 23)," love your life ");
treeMap2.put(new Student(" Li Si ", 27)," love JAVA");
treeMap2.put(new Student(" Lau Andy ", 21)," Love football ");
treeMap2.put(new Student(" Ouyang Zhenhua ", 28)," Love painting ");
treeMap2.put(new Student(" Zhang San ", 23)," Love music ");
treeMap2.forEach(new BiConsumer<Student, String>() {
@Override
public void accept(Student student, String s) {
System.out.println(student.getName()+"==="+student.getAge()+"==="+s);
}
});
/*
Zhang San ===23=== Love music
Li Si ===27=== love JAVA
Lau Andy ===21=== Love football
Ouyang Zhenhua ===28=== Love painting
*/
}
}
( Xiaobian is also trying to learn more ! I'll share it later !)

Hope to be helpful to friends !!!!
边栏推荐
- 异常的相关介绍
- 152-Solana入门(十六)- 创建MetaplexNFT
- [middle order traversal of binary tree based on stack] middle order traversal of binary tree + stack, spatial complexity of O (H)
- onnx转tensorrt学习笔记
- 五层参考模型各层总结
- College entrance examination
- Dynamic planning exercises (II)
- [notes for personal use] detailed steps for connecting MyEclipse to MySQL database
- 一文搞懂this指向
- JPA的基本使用
猜你喜欢

Sqlmap tool

Mysql database foundation: connection query

Argo CD usage

leetcode数据库mysql题目(难度:简单)

The database has the problem of user changing password

That's great. MySQL's summary is too comprehensive

Modbus Poll v9.9.2 Build 1690 Modbus测试工具单文件版

第4篇:从硬件/源码视角看JVM内存模型

Issue 6: which mainstream programming language should college students choose

海明码校验【简单详细】
随机推荐
第6期:大学生应该选择哪种主流编程语言
Leetcode database mysql topic (difficulty: simple)
154 Solana distribution token
TweenMax不规则几何图形背景带动画js特效
第8期:云原生—— 大学生职场小白该如何学
Pyg tutorial (6): customizing the messaging network
【查询数据表中第三行数据】
leetcode数据库mysql题目(难度:简单)
数据可视化实战:数据处理
Argo CD 使用
第4篇:从硬件/源码视角看JVM内存模型
[query the data in the third row of the data table]
使用cell ranger进行单细胞转录组定量分析
【JDBC從入門到實戰】JDBC基礎通關教程(全面總結上篇)
Sqlmap tool
机器学习之数据归一化(Feature Scaling)
出现ConcurrentModificationException这个异常报错,怎么处理?
(各种规律数的编程练习)输出范围内的素数,一个整数的分解质因数,两个数的最大公约数和最小公倍数以及水仙花数和完数等等
工作那点事
Small program [phase I]