Basic usage and common APIs of Map collection system in Java
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)LinkedHashMap: The elements arepublic 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>Copy after loginordered according to the key, no duplication, no index, and no value requirements.
TreeMap: The elements are sortedpublic 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>Copy after loginaccording 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>Copy after login
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>Copy after login
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>Copy after login
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>Copy after login
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>Copy after login
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>Copy after login
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>Copy after login
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>Copy after login
java video tutorialRecommended learning: "public 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>Copy after login
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.
