Map interface
The Map interface is a two-column collection. Each element of it contains a key object key and a value object Value. There is a correspondence between the key object and the value object, which is called mapping. When accessing elements from the Map collection, as long as the Key is specified, the corresponding Value can be found.
Common methods of Map
void put(Object key,Object value)//Associate the specified value with the specified key in the map
Object get(Object key) //Returns the value mapped to the specified key. If this map does not contain the mapping relationship of the key, returns null
boolean containsKey(Object key)//If this map contains the mapping relationship of the specified key, returns true
boolean containsValue(Object value)//If the mapping maps one or more keys to the specified value at this time, return true
Set keySet()//Return the Set view of the values contained in this mapping
Collection< ;V>values()//Returns a Collection view of the values contained in this map
Set>entrySet()//Returns a Set view of the mapping relationships contained in this map
HashMap collection
The HashMap collection is an implementation class of the Map interface. It is used to store key-value mapping relationships, but it must ensure that there are no duplicate keys.
package 集合类; import java.util.HashMap; import java.util.Map; public class Long { public static void main(String[] args) { Map map=new HashMap(); map.put("1","lilong"); map.put("2","xiaolong"); map.put("3","dalong"); System.out.println("1:"+map.get("1")); System.out.println("2:"+map.get("2")); System.out.println("3:"+map.get("3")); } }
Run results
package 集合类; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Li { public static void main(String[] args) { Map map=new HashMap(); map.put("1","lilong"); map.put("2","xiaolong"); map.put("3","dalong"); Set keySet= (Set) map.keySet(); Iterator it= keySet.iterator; while(it.hasNext()){ Object key=it.next(); Object value=map.get( key); System.out.println(key+":"+value); } } }Running results
package 集合类; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Xiao { public static void main(String[] args) { Map map=new HashMap(); map.put("1","lilong"); map.put("2","xiaolong"); map.put("3","dalong"); Set entrySet= (Set) map.entrySet(); Iterator it= entrySet.iterator; while(it.hasNext()){ Map.Entry entry=(Map.Entry) (it.next()); Object key=entry.getKey(); Object value=entry.getValue(); System.out.println(key+":"+value); } } }Run results
## First call the entrySet() method of the Map object to obtain the Set collection of all mappings stored in the Map. This collection stores the Map .Entry type element. Each Map.Entry object represents a key-value pair in the Map. Then iterate the Set collection to obtain each mapping object, and call the getKey() method and getValue() method of the mapping object to obtain the key and value. value.
The Map collection also provides a values() method, through which you can directly obtain the Collection that stores all values in the Map.
package 集合类; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class LiXiao { public static void main(String[] args) { Map map=new HashMap(); map.put("1","lilong"); map.put("2","xiaolong"); map.put("3","dalong"); Collection values=map.values(); Iterator it=values.iterator(); while(it.hasnext()){ Object value=it.next(); System.out.println(value): } } }
Running results
Get a Collection containing all the values in the Map by calling the value() method of the Map, and then iterate out each item in the collection value.
The order in which the elements are iterated out of the HashMap collection is inconsistent with the order in which they are stored. To make the two orders consistent, you can use the LinkdedHashMap class provided in Java, which can use a doubly linked list to maintain the relationship between internal elements. , so that the order of Map element iteration is consistent with the order of storage.
import java.util.Map; public class Long { public static void main(String[] args) { Map map=new LinkedHashMap(); map.put("1","lilong"); map.put("2","xiaolong"); map.put("3","dalong"); Set keySet= (Set) map.keySet(); Iterator it= keySet.iterator; while(it.hasNext()){ Object key=it.next(); Object value=map.get( key); System.out.println(key+":"+value); } } }
Running results
##Properties collection
package 集合类;
import java.util.Enumeration;
import java.util.Properties;
public class Xiao {
public static void main(String[] args) {
Properties p=new Properties();
p.setProperty("Backgroup-color","red");
p.setProperty("Font-size","14px");
p.setProperty("Language","chinese");
Enumeration names=p.propertyNames();
while(names.hasMoreElements()){
String key=(String) names.nextElement();
String value=p.getProperty(key);
System.out.println(key+"="+value);
}
}
}
Run results In the Properties class, two dedicated methods are provided for string access, setProperty() and getProperty(). The setProperty() method is used to add the value and key of the configuration item to the Properties collection. Then get an Enumeration object containing all keys by calling the propertyNames() method of Properties, and then get the value corresponding to the key by calling the getProperty() method when traversing all keys.
The above is the detailed content of Java Map collection usage example analysis. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
