This article brings you relevant knowledge about java, which mainly introduces the basic use of Map collection system and related content of commonly used APIs. Let’s take a look at it together. I hope it will be helpful to everyone.
Map collection overview and usage
Map collection is a two-column collection, each element contains two data.
The format of each element of the Map collection: key=value (key-value pair element).
Map collection is also called "key-value pair collection".
Map collection overall format :
Collection collection format:
[Element 1, Element 2, Element 3..]
The full format of the Map collection:
{key1=value1, key2=value2, key3=value3, ...}
##Map One of the usage scenarios of the collection: Shopping cart system
Analysis:
The four items provided by the shopping cart and the purchased quantity are required in the background Container storage. Each product object corresponds to a purchase quantity one by one. Consider the product object as the creation of the Map collection, and the purchase quantity as the value of the Map collection. For example:{Item 1=2, Item 2=3, Item 3 = 2, Item 4= 3}
Characteristics of the Map collection system
The most commonly used Map collection among the Map collections is HashMap. Focus on mastering HashMap, LinkedHashMap, and TreeMap. Other follow-up understanding.
Map collection system characteristics:
The characteristics of the Map collection are determined by the key. The keys of the Map collection are unordered, non-repeating, and non-indexed, and the values are not required (can be repeated). The values corresponding to the repeated keys at the end of the Map collection will overwrite the values of the previous repeated keys. The key-value pairs of the Map collection can be null.
Map collection implementation class features:
HashMap: The elements are unordered according to the key, no duplication, no index, and no requirements on the value. (Consistent with the Map system)public static void main(String[] args) { // 创建一个HashMap对象 Map<string> maps = new HashMap(); // 向集合添加元素 maps.put("桌子", 2); maps.put("凳子", 10); maps.put("桌子", 10); // 键一样会覆盖前面的 maps.put(null, null); // 键值对可以为null // 输出集合, 可以发现是无序的 System.out.println(maps); // {null=null, 凳子=10, 桌子=10}}</string>LinkedHashMap: The elements areordered according to the key, no duplication, no index, and no value requirements.
public static void main(String[] args) { // 创建一个LinkedHashMap对象 // Map<string> maps = new HashMap(); Map<string> maps = new LinkedHashMap(); // 向集合添加元素 maps.put("桌子", 2); maps.put("凳子", 10); maps.put("桌子", 10); // 键一样会覆盖前面的 maps.put(null, null); // 键值对可以为null // 输出集合, 是有序的 System.out.println(maps); // {桌子=10, 凳子=10, null=null}}</string></string>TreeMap: The elements are sortedaccording to the key , without duplication, without index, and the value is not required.
public static void main(String[] args) { // 创建一个HashMap对象 // Map<string> maps = new HashMap(); // Map<string> maps = new LinkedHashMap(); Map<string> maps = new TreeMap(); // 向集合添加元素 maps.put("ddd", 2); maps.put("bbb", 10); maps.put("ddd", 3); maps.put("aaa", 5); maps.put("ccc", 1); // 输出集合, 元素按照键进行排序 System.out.println(maps); // {aaa=5, bbb=10, ccc=1, ddd=3}}</string></string></string>
Map collection commonly used API
Map collection:
Map is the ancestor interface of double-column collections, and its functions can be inherited and used by all double-column collections.
Map API is as follows:
Description | |
---|---|
Add element | |
According to the key, delete key-value pair elements | |
Remove all key-value pair elements | |
Determine whether the set contains the specified key | |
Determine whether the set contains the specified value | |
Judge whether the set is empty | |
The length of the set, that is The number of key-value pairs in the collection |
remove method deletes elements based on keyspublic static void main(String[] args) { // 创建Map集合对象 Map<string> maps = new HashMap(); // 添加元素 maps.put("华为", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); System.out.println(maps); // {iPhone=6, 生活用品=15, 华为=10, 小米=5}}</string>
clear method, clears the collection elementspublic static void main(String[] args) { // 创建Map集合对象 Map<string> maps = new HashMap(); // 添加元素 maps.put("华为", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); // 删除元素 maps.remove("小米"); System.out.println(maps); // {iPhone=6, 生活用品=15, 华为=10}}</string>
containsKey() method, determines whether the specified key is includedpublic static void main(String[] args) { // 创建Map集合对象 Map<string> maps = new HashMap(); // 添加元素 maps.put("华为", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); // 清空元素 maps.clear(); System.out.println(maps); // {}}</string>
containsValue method, determines whether it contains the specified valuepublic static void main(String[] args) { // 创建Map集合对象 Map<string> maps = new HashMap(); // 添加元素 maps.put("华为", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); // 判断是否包含指定键 System.out.println(maps.containsKey("华为")); // true System.out.println(maps.containsKey("魅族")); // false}</string>
isEmpty, determines whether the collection is emptypublic static void main(String[] args) { // 创建Map集合对象 Map<string> maps = new HashMap(); // 添加元素 maps.put("华为", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); // 判断是否包含指定值 System.out.println(maps.containsValue(6)); // true System.out.println(maps.containsValue(99)); // false}</string>
size method, collection The number of elementspublic static void main(String[] args) { // 创建Map集合对象 Map<string> maps = new HashMap(); // 添加元素 maps.put("华为", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); // 判断集合是否为空 System.out.println(maps.isEmpty()); // false}</string>
Extension method: putAll merges other collections. If the merge encounters duplicate keys, it will be mergedpublic static void main(String[] args) { // 创建Map集合对象 Map<string> maps = new HashMap(); // 添加元素 maps.put("华为", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); // 返回集合元素的个数 System.out.println(maps.size()); // 4}</string>
java video tutorialpublic static void main(String[] args) { Map<string> map1 = new HashMap(); map1.put("java", 1); map1.put("C语言", 2); Map<string> map2 = new HashMap(); map2.put("python", 4); map2.put("linux", 7); // 合并两个集合 map1.putAll(map2); System.out.println(map1); // {{python=4, java=7, C语言=2}}</string></string>Recommended learning: "
The above is the detailed content of Basic usage and common APIs of Map collection system in Java. For more information, please follow other related articles on the PHP Chinese website!

JVM'sperformanceiscompetitivewithotherruntimes,offeringabalanceofspeed,safety,andproductivity.1)JVMusesJITcompilationfordynamicoptimizations.2)C offersnativeperformancebutlacksJVM'ssafetyfeatures.3)Pythonisslowerbuteasiertouse.4)JavaScript'sJITisles

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunonanyplatformwithaJVM.1)Codeiscompiledintobytecode,notmachine-specificcode.2)BytecodeisinterpretedbytheJVM,enablingcross-platformexecution.3)Developersshouldtestacross

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java implementation "write once, run everywhere" is compiled into bytecode and run on a Java virtual machine (JVM). 1) Write Java code and compile it into bytecode. 2) Bytecode runs on any platform with JVM installed. 3) Use Java native interface (JNI) to handle platform-specific functions. Despite challenges such as JVM consistency and the use of platform-specific libraries, WORA greatly improves development efficiency and deployment flexibility.

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Zend Studio 13.0.1
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
