Home Java javaTutorial Detailed explanation and example code of java HashMap

Detailed explanation and example code of java HashMap

Feb 03, 2017 pm 05:28 PM

java HashMap

/*
* Map集合的特点
* 将键映射值的对象,一个映射不能包含重复的值;每个键最多只能映射到一个值
* 
* Map集合和Collection集合的区别?
* Map集合存储元素是成对出现的,Map集合的键是唯一的,就是可重复的。可以把这个理解为:夫妻对
* Collection集合存储元素是单独出现的,Collection的儿子Set是唯一的,List是可重复的,可以把这个理解为:光棍
* 
* 注意:
* Map集合的数据结构值针对键有效,限值无效
* Collection集合的数据结构是针对元素有效
* 
* Map集合的功能概述:
* 1:添加功能
* V put(K key,V value);//添加元素
* 如果键是第一次存储,就直接存储元素,返回null
* 如果键不是第一次存储,就用值把以前的值替换掉,返回以前的值
* 
* 2:删除功能
* void clear();//移除所有的键值对元素
* V remove(Object key);//根据键删除键值对元素,并把值返回
* 
* 3:判断功能
* boolean containsKey(Object key);//判断集合是否包含指定的键
* boolean containsValue(Object value);//判断集合是否包含指定的值
* boolean isEmpty();//判断集合是否为空
* 
* 4:获取功能
* set<Map,Entry<E,V>> entrySet();获取键值对的对象集合
* V get(Object key);//根据键获取值
* Set<K> keySet();//获取集合中所有键的集合
* Collection<V> values();//获取集合中所有值的集合
* 
* 5:长度功能
* int size();//返回集合中的键值对的对数
* */
Copy after login

Traversal of Map collection

Method 1, query value based on key

Get the collection of all keys

Traverse the collection of keys , get each key

Query the value based on the key

Method 2, query the key and value based on the key-value pair object

Get the collection of all key-value pair objects

Traverse the collection of key-value pair objects and obtain the object of each key-value pair

Query the key and value based on the key-value pair object

Method 1, Query the value based on the key

/*
 
* Map集合的遍历,根据键查询值
* 
* 思路:
* A:获取所有的键
* B:遍历键的集合,获取得到每一个键
* C:根据键查询值
* */
Copy after login
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
  
/*
 * Map集合的遍历,根据键查询值
 *
 * 思路:
 * A:获取所有的键
 * B:遍历键的集合,获取得到每一个键
 * C:根据键查询值
 * */
  
public class IntegerDemo {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
  
    Map<String, String> map = new HashMap<String, String>();
  
    map.put("hello", "world");
    map.put("java", "c++");
    map.put("sql", "os");
  
    System.out.println(map);
  
    // A:获取所有的键
    Set<String> set = map.keySet();
  
    // B:遍历键的集合,获取得到每一个键
    for (String key : set) {
      // C:根据键查询值
      String value = map.get(key);
      System.out.println(key + "---" + value);
    }
  }
}
Copy after login

Method 2, query the key and value based on the object of the key-value pair

/*
* Map集合的遍历,根据对象查询键和值
*
* 思路:
* A:获取所有的键值对对象的集合
* B:遍历键值对对象的集合,得到每一个键值对的对象
* C:获取键和值
* */
Copy after login
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
  
/*
 * Map集合的遍历,根据对象查询键和值
 *
 * 思路:
 * A:获取所有的键值对对象的集合
 * B:遍历键值对对象的集合,得到每一个键值对的对象
 * C:获取键和值
 * */
  
public class IntegerDemo {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
  
    Map<String, String> map = new HashMap<String, String>();
  
    map.put("hello", "world");
    map.put("java", "c++");
    map.put("sql", "os");
  
    System.out.println(map);
  
    // A:获取所有的键值对对象的集合
    Set<Map.Entry<String, String>> set = map.entrySet();
  
    // B:遍历键值对对象的集合,得到每一个键值对的对象
    for (Map.Entry<String, String> me : set) {
      // C:获取键和值
      String key = me.getKey();
      String value = me.getValue();
      System.out.println(key + "---" + value);
    }
  }
}
Copy after login

/*
* 1:HashMap和Hashtable的区别?
* HashMap线程不安全,效率高,允许null键和null值
* Hashtable线程安全,效率低,不允许null键和null值
*
* 2:List,Set,Map等接口是否都继承于Map接口?
* List,Set不是继承自Map接口,它们继承自Collection接口
* Map接口本身就是一个顶层接口
* */
import java.util.HashMap;
import java.util.Hashtable;
  
public class IntegerDemo {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
  
    HashMap<String, String> hm = new HashMap<String, String>();
    Hashtable<String, String> ht = new Hashtable<String, String>();
  
    hm.put("hello", "world");
    hm.put("java", "c++");
    hm.put(null, "sql");
  
    ht.put("hello", "world");
    ht.put("java", "c++");
    ht.put(null, "sql");// Exception in thread "main"
              // java.lang.NullPointerException
  }
}
Copy after login

Thanks for reading, I hope this helps everyone, and thank you for your support of this site!

For more java HashMap detailed explanations and example code related articles, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

See all articles